list view item repeated in android?
i use the following code to list few items from array , while scrolling the list view items is append more with same data exit in array i don`t know what mistake i made.
Anyone pointed out where i made the mistake.
private static String array_spinner_subcategories[];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.sub_categories);
setSubCat();
ListView sub_categories=(ListView)findViewById(R.id.sub_catlist);
sub_categories.setAdapter(new EfficientAdapter(this));
sub_categories.setAdapter(adapter);
sub_categories.setOnItemClickListener(subcatlistener);
cr=getContentResolver();
}
public String[] setSubCat(){
recordDB=new Viddatabase(this);
db=recordDB.getWritableDatabase();
array_spinner_subcategories=recordDB.subcategoriesList(db);
recordDB.close();
return array_spinner_subcategories;
}
private OnItemClickL开发者_C百科istener subcatlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
System.gc();
Toast.makeText(SubCategories.this,array_spinner_subcategories[position],
Toast.LENGTH_LONG).show();
}
};
private static class EfficientAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public EfficientAdapter(Context context) {
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return array_spinner_subcategories.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.albumlist, null);
holder = new ViewHolder();
holder.subCategories = (TextView) convertView.findViewById(R.id.albumDetails);
holder.subCategories.setText(array_spinner_subcategories[position]);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
static class ViewHolder {
TextView subCategories;
}
}
Move the holder.subCategories.setText(array_spinner_subcategories[position]);
line in getView()
method below the else
block
In your getView method, you are only setting data in the view when you create a new view.
The ListView recycles views, so you will very likely be passed in a a view to reuse, which is why it is referred to as a convertview.
You need to be calling setText on the view every time this is called, otherwise you are just handing back the convertview unchanged and thus you are getting the same values repeated.
精彩评论