Hello friends ,in this post i will show you how to avod list items from being duplicate.
Here is the code:
File name: MainActivity.java
Here is the code:
File name: MainActivity.java
import android.app.Activity;
.
.
.
.
.
public class MainActivity extends Activity{
Listview listview;
Customlist cl;
ArrayList<Student> title=new ArrayList<Student>();
Student s=new Student();
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);listview=findViewById(R.id.ListView1);
s.setName("Android");title.add(s);also use setList methodcl.setList(title);cl=new Customlist(this,title);listview.setAdapter(title);}
Create custom inner class
class Customlist extends BaseAdapter { Context mContext; String data; ArrayList<Student> mtitle=new ArrayList<Student>(); public Customlist(Context mContext,ArrayList<Student> mtitle){ this.mContext=mContext; this.mtitle=mtitle; } public void setList(ArrayList<Student> mtitle){ this.mtitle=mtitle; notifyDataSetChanged(); } @Override public int getCount() { return mtitle.size(); } @Override public Object getItem(int position) { return mtitle.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View v=convertView; Holder h=null;
if(v==null){ LayoutInflater mLayoutInflater=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v=mLayoutInflater.inflate(R.layout.listitem,null); h.name=(TextView)v.findViewById(R.id.tv_name); v.setTag(h);
}else{
h=(Holder)v.getTag();
}
name.setText(mtitle.get(position).getName())); return v; }static class Holder{
TextView name;}
}
}
Last create Student class:
Filename :Student.java
public class Student{
String name;
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
}
No comments:
Post a Comment