+ 1
How to add item from custom adapter?
6 Answers
+ 1
can I get the codes? thx before
0
do class to your item then do constructor , getter and setter for this item , then do class extends from base adapter , then do array list in this class and make constructor with parameter array list
0
ok no problem , wait
0
Step1:add new layout cause add in it text and image or any item.
Step2:in main layout add listview and give it ID .
Step3:add new class to define the variables to connect it with text or âŠ, and make a constructor .
Step4:in main activity(Class): make public class and extends from BaseAdapter .
Step5: define an object from your class in array list , make a constructor and implement the functions .
Step6:define object from layoutinflater and view ;
In main activity (class):
ListView ls=(ListView)findViewById(R.id.list); ArrayList < Your Class >arrayList=new ArrayList <Your Class>();
arrayList.add(new Your Class ("Eslam","He is A Good Man"));
arrayList.add(new Your Class ("Mohamed","He is A Good Man"));
arrayList.add(new Your Class ("Yasser","He is A Good Man"));
ad arrayabd=new ad(arrayList); ls.setAdapter(arrayabd);
In BaseAdapter Class :
public class ad extends BaseAdapter{
ArrayList <your Class>arrayList=new ArrayList <Your Class>();
public ad(ArrayList< Your Class > arrayList) { this.arrayList = arrayList; }
@Override
public int getCount() { return arrayList.size(); }
@Override
public Object getItem(int position) { return arrayList.get(position); }
@Override
public long getItemId(int position) { return position; }
@Override
public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater layoutInflater=getLayoutInflater(); View view=layoutInflater.inflate(R.layout.row_item,null); TextView tname=(TextView)view.findViewById(R.id.name); TextView tdesc=(TextView)view.findViewById(R.id.desc); tname.setText(arrayList.get(position).name); tdesc.setText(arrayList.get(position).Desc); return view; } }
0
i hope that's help you
0
oke, thx so much.. it works