5 Replies - 509 Views - Last Post: 11 September 2017 - 03:05 AM

#1 bjroberts52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-August 17

How to make listview adapter?

Posted 06 August 2017 - 08:01 AM

I am working with Sketchware, so please forgive any coding inaccuracies. I am trying to learn though, so please help.

I trying to make Custom ListView Adapter, but I'm getting an error when I try to run it.

"The method getPackageName() is undefined for type new ArrayAdapter<String>(){}"

Here is my code:
GridView gv=new GridView(MainActivity.this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
gv.setLayoutParams(lp);
ln.addView(gv);ArrayAdapter<String>adapter=new ArrayAdapter<String>(getApplicationContext (),R.layout.bg_grid,title_ar){
@Override public View getView(int position,View convertView,ViewGroup parent){
if(convertView==null){convertView=getLayoutInflater().inflate(R.layout.bg_grid,parent,false);}
TextView title=(TextView)convertView.findViewById(R.id.tv_title);
title.setText(title_ar.get(position));
TextView body=(TextView)convertView.findViewById(R.id.tv_body);
body.setText(desc_ar.get(position));
String imagename = "note";
int res = getResources().getIdentifier(imagename, "drawable", main.getPackageName());
ImageView icon= (ImageView)findViewById(R.id.iv_icon);
icon.setImageResource(res);;
return convertView;}};
gv.setAdapter(adapter);


How can I get it to work?

Is This A Good Question/Topic? 0
  • +

Replies To: How to make listview adapter?

#2 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 563
  • Posts: 2,302
  • Joined: 13-June 14

Re: How to make listview adapter?

Posted 06 August 2017 - 10:56 AM

I'm not an Android developer and so can't really help with your problem, but the code you posted is extremely difficult to read. You really need to make good use of whitespace and indentation. See, e.g. this for a guide to coding conventions in Java.
Was This Post Helpful? 0
  • +
  • -

#3 bjroberts52  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-August 17

Re: How to make listview adapter?

Posted 06 August 2017 - 06:42 PM

Well solved one problem to create a new one. Now I have no idea how to make my listview clickable. If anyone has any ideas on how I can make my list clickable please let me know how.
Was This Post Helpful? 0
  • +
  • -

#4 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 279
  • Joined: 23-September 15

Re: How to make listview adapter?

Posted 11 September 2017 - 01:53 AM

View Postbjroberts52, on 07 August 2017 - 01:42 AM, said:

Now I have no idea how to make my listview clickable. If anyone has any ideas on how I can make my list clickable please let me know how.

Assume lv is your listView
lv.setOnItemClickListener(new OnItemClickListener() {

   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       // When clicked perform some action...
   }
 });

P/S: Open a new thread if you have another question to ask.

This post has been edited by wseng92: 11 September 2017 - 01:54 AM

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • lashings of ginger beer
  • member icon

Reputation: 6338
  • View blog
  • Posts: 25,555
  • Joined: 12-December 12

Re: How to make listview adapter?

Posted 11 September 2017 - 02:02 AM

View Postwseng92, on 11 September 2017 - 08:53 AM, said:

P/S: Open a new thread if you have another question to ask.

Just to note ;) that we'd rather keep related questions in the same thread, unless it is a completely different project or topic area.
Was This Post Helpful? 0
  • +
  • -

#6 wseng92  Icon User is offline

  • D.I.C Regular

Reputation: 9
  • View blog
  • Posts: 279
  • Joined: 23-September 15

Re: How to make listview adapter?

Posted 11 September 2017 - 03:05 AM

Noted ! :rolleyes:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1