我有一个带基础适配器的自定义列表视图。我试图增加和减少特别是一行textview值。它正在工作但单击按钮时所有行textview值都已更改。我怎么能特别改变一行textview值。请建议我。
public class BreakfastListAdapter extends BaseAdapter {
private Context context;
private String[] number;
private int[] imageid;
ImageView plus1, minus1;
TextView value1;
int a = 0;
public BreakfastListAdapter(Context c, String[] number, int[] imageid) {
context = c;
this.imageid = imageid;
this.number = number;
}
@Override
public int getCount() {
return number.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
plus1 = (ImageView) convertView.findViewById(R.id.plus);
minus1 = (ImageView) convertView.findViewById(R.id.minus);
value1 = (TextView) convertView.findViewById(R.id.value);
textView.setText(number[position]);
imageView.setImageResource(imageid[position]);
value1.setText(String.valueOf(a));
System.out.println(a + "dddddddddddddd");
plus1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
a = a + 1;
value1.setText(String.valueOf(a));
Toast.makeText(context, String.valueOf(a), 1000).show();
System.out.println("@@@@@@@@@@@@@@@@@@@@@@");
System.out.println(a);
notifyDataSetChanged();
}
});
return convertView;
}
}
答案 0 :(得分:1)
您需要保留每行的值,而不是所有行的值。
更改int a
中的int[] a
并保留每行的每个值。
您将拥有以下听众:
@Override
public void onClick(View v) {
a[position] = a[position] + 1;
value1.setText(String.valueOf(a[position]));
Toast.makeText(context, String.valueOf(a[position]), 1000).show();
System.out.println("@@@@@@@@@@@@@@@@@@@@@@");
System.out.println(a[position]);
notifyDataSetChanged();
}
以下是完整的代码:
public class BreakfastListAdapter extends BaseAdapter {
private Context context;
private String[] number;
private int[] imageid;
ImageView plus1, minus1;
TextView value1;
int a[];
public BreakfastListAdapter(Context c, String[] number, int[] imageid) {
context = c;
this.imageid = imageid;
this.number = number;
this.a = new int[number.length];
}
@Override
public int getCount() {
return number.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.text);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView1);
plus1 = (ImageView) convertView.findViewById(R.id.plus);
minus1 = (ImageView) convertView.findViewById(R.id.minus);
value1 = (TextView) convertView.findViewById(R.id.value);
textView.setText(number[position]);
imageView.setImageResource(imageid[position]);
value1.setText(String.valueOf(a[position]));
System.out.println(a[position] + "dddddddddddddd");
plus1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
a[position] = a[position] + 1;
value1.setText(String.valueOf(a[position]));
Toast.makeText(context, String.valueOf(a[position]), 1000).show();
System.out.println("@@@@@@@@@@@@@@@@@@@@@@");
System.out.println(a[position]);
notifyDataSetChanged();
}
});
return convertView;
}
}
答案 1 :(得分:0)
你应该将textText字符串设置为特定的行TextView。
每一行都是通过 convertView 引用的,所以我们将使用它
修改是:
plus1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
a = a + 1;
// this is change
convertView.value1.setText(String.valueOf(a));
Toast.makeText(context, String.valueOf(a), 1000).show();
System.out.println("@@@@@@@@@@@@@@@@@@@@@@");
System.out.println(a);
notifyDataSetChanged();
}
我们最后需要将一个值设置为 0 :
// reset a value at end of getView() method
a = 0;
这样当显示下一行时,值为0并从0开始递增
答案 2 :(得分:0)
尝试一下删除
a = a + 1;//because this is a global variable in the class
change value1.setText(""+(a+1));
如果这不是问题,则声明一个大小与listview大小相似的数组/ arraylist,并在constructer中填充arraylist,并将所有值设置为0.然后在getview()方法内部
int currentposition=position;
plus1.setTag(currentposition);
value1.setText(arraylist.get(currentposition));
plus1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int btnvalue=Integer.parseInt(plus1.getTag().toString());
arraylist.set(btnvalue,(Integer.parseInt(arraylist.get(btnvalue)))+1);
notifydatasetchanged();
}
});
答案 3 :(得分:0)
实施ViewHolder类以维护其小部件的单独副本,例如
ViewHolder{
TextView t1;
Imageview i1,i2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.secondadapter, null);
holder = new ViewHolder();
holder.t1 = (TextView) convertView.findViewById(R.id.text);
holder.i1 =convertView.findViewById(R.id.imageView1);
holder.i2 =convertView.findViewById(R.id.imageView1);
convertView.setTag(holder);
}else
{
ViewHolder holder = (ViewHolder) convertView.getTag();
}
holder.t1.setText(number[position]);
holder.i1.setImageResource(imageid[position]);
holder.t1.setText(String.valueOf(a));
System.out.println(a + "dddddddddddddd");
holdert1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
a = a + 1;
holder.t1.setText(String.valueOf(a));
Toast.makeText(context, String.valueOf(a), 1000).show();
System.out.println("@@@@@@@@@@@@@@@@@@@@@@");
System.out.println(a);
notifyDataSetChanged();
}
});
return convertView
}