//********* base adapter *****
Listview里面按钮点击设置TextView里面的值,但文本视图重复值返回,当我滚动TextView位置更改时,我已经附加了我的基本适配器代码,请帮助解决。
package com.example.test;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
public class listadapter extends BaseAdapter{
Context context;
ArrayList<Listdata> listitem;
public listadapter(Context context, ArrayList<Listdata> listitem) {
this.context = context;
this.listitem = listitem;
}
/*private view holder class*/
public class ViewHolder {
TextView itemname;
TextView itemrate;
TextView itemcount;
TextView itemtotal;
Button add;
Button sub;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return listitem.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return listitem.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return listitem.indexOf(getItem(position));
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder ;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listitem, null);
holder = new ViewHolder();
holder.itemname=(TextView)convertView.findViewById(R.id.itemname);
holder.itemrate=(TextView)convertView.findViewById(R.id.itemrate);
holder.itemcount=(TextView)convertView.findViewById(R.id.count);
holder.itemtotal=(TextView)convertView.findViewById(R.id.total);
holder.add=(Button)convertView.findViewById(R.id.add);
holder.sub=(Button)convertView.findViewById(R.id.minus);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Listdata ld=(Listdata) getItem(position);
holder.itemname.setText(ld.getItemname());
int a=ld.getItemrate();
holder.itemrate.setText(Integer.toString(a));
holder.add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Listdata ld=listitem.get(position);
ld.itemcount++;
holder.itemcount.setText(String.valueOf(ld.itemcount));
}
});
holder.sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
});
return convertView;
}
}
答案 0 :(得分:2)
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}