最近遇到一个问题,请各位高手帮看看,本人这个应该怎么解决。本人点击加号,减号的时候修改的全是最后一条的edittext的值,看adapter里面要怎么设置一下,请各位高手帮看看,谢谢;
以下为本人的adapter 看是不是要怎么解决;
package com.bwf.duobao.adapter;
import java.util.ArrayList;
import com.bwf.duobao.R;
import com.bwf.duobao.instance.goodsdetails.Result;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.db.annotation.Check;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
public class ListView_Adapter_ListFragment extends BaseAdapter {
private ArrayList<Result> Bill_datas;
private LayoutInflater inflater;
private BitmapUtils myBitmapUtils;
private ViewHolder holder;
public ListView_Adapter_ListFragment(ArrayList<Result> bill_datas,Context context) {
super();
Bill_datas = bill_datas;
inflater=LayoutInflater.from(context);
myBitmapUtils=new BitmapUtils(context);
}
@Override
public int getCount() {
return Bill_datas.size();
}
@Override
public Result getItem(int arg0) {
return Bill_datas.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if(arg1==null){
holder=new ViewHolder();
arg1=inflater.inflate(R.layout.activity_listfragment_adapter, null);
holder.cb=(CheckBox) arg1.findViewById(R.id.cbadapter);
holder.img=(ImageView) arg1.findViewById(R.id.img);
holder.tv1=(TextView) arg1.findViewById(R.id.tv1);
holder.tv2=(TextView) arg1.findViewById(R.id.tv2);
holder.add=(ImageView) arg1.findViewById(R.id.add);
holder.minus=(ImageView) arg1.findViewById(R.id.minus);
holder.et=(EditText) arg1.findViewById(R.id.et);
arg1.setTag(holder);
}else{
holder=(ViewHolder) arg1.getTag();
}
Result result = Bill_datas.get(arg0);
// 设置图片
myBitmapUtils.display(holder.img, result.getBigImgs().get(0));
holder.tv1.setText(result.getGoodsName());
holder.tv2.setText(“总需”+result.getTotalTimes()+”人次,还剩”+result.getRemainTimes()+”人次”);
holder.add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
holder.et.setText(Integer.parseInt(holder.et.getText().toString())+1+””);
}
});
holder.minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int num=Integer.parseInt(holder.et.getText().toString())-1;
if(num>=0){
holder.et.setText(Integer.parseInt(holder.et.getText().toString())-1+””);
}
}
});;
return arg1;
}
public class ViewHolder {
public CheckBox cb;
public ImageView img,add,minus;
public TextView tv1,tv2;
public EditText et;
}
public ViewHolder getHolder(){
return holder;
}
}
以下为本人的adapter 看是不是要怎么解决;
package com.bwf.duobao.adapter;
import java.util.ArrayList;
import com.bwf.duobao.R;
import com.bwf.duobao.instance.goodsdetails.Result;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.db.annotation.Check;
import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
public class ListView_Adapter_ListFragment extends BaseAdapter {
private ArrayList<Result> Bill_datas;
private LayoutInflater inflater;
private BitmapUtils myBitmapUtils;
private ViewHolder holder;
public ListView_Adapter_ListFragment(ArrayList<Result> bill_datas,Context context) {
super();
Bill_datas = bill_datas;
inflater=LayoutInflater.from(context);
myBitmapUtils=new BitmapUtils(context);
}
@Override
public int getCount() {
return Bill_datas.size();
}
@Override
public Result getItem(int arg0) {
return Bill_datas.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
if(arg1==null){
holder=new ViewHolder();
arg1=inflater.inflate(R.layout.activity_listfragment_adapter, null);
holder.cb=(CheckBox) arg1.findViewById(R.id.cbadapter);
holder.img=(ImageView) arg1.findViewById(R.id.img);
holder.tv1=(TextView) arg1.findViewById(R.id.tv1);
holder.tv2=(TextView) arg1.findViewById(R.id.tv2);
holder.add=(ImageView) arg1.findViewById(R.id.add);
holder.minus=(ImageView) arg1.findViewById(R.id.minus);
holder.et=(EditText) arg1.findViewById(R.id.et);
arg1.setTag(holder);
}else{
holder=(ViewHolder) arg1.getTag();
}
Result result = Bill_datas.get(arg0);
// 设置图片
myBitmapUtils.display(holder.img, result.getBigImgs().get(0));
holder.tv1.setText(result.getGoodsName());
holder.tv2.setText(“总需”+result.getTotalTimes()+”人次,还剩”+result.getRemainTimes()+”人次”);
holder.add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
holder.et.setText(Integer.parseInt(holder.et.getText().toString())+1+””);
}
});
holder.minus.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int num=Integer.parseInt(holder.et.getText().toString())-1;
if(num>=0){
holder.et.setText(Integer.parseInt(holder.et.getText().toString())-1+””);
}
}
});;
return arg1;
}
public class ViewHolder {
public CheckBox cb;
public ImageView img,add,minus;
public TextView tv1,tv2;
public EditText et;
}
public ViewHolder getHolder(){
return holder;
}
}
解决方案
10
重新clicklistenor
public class listviewClickListener implements OnClickListener { private int position;//自定义的clicklistener里保存item的position listviewClickListener(int pos) { position = pos; } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v.getId() == R.id.info_btn) { PlanStartItemData data = getItem(position); Toast.makeText(context, SportUtils.getSportRemarkByID(data.getSportId()),Toast.LENGTH_LONG).show(); return; } } }
然后getview里
your_btn.setOnClickListener(new listviewClickListener(arg0));
10
…..这个你改造一下就好了啊
public class listviewClickListener implements OnClickListener { private int position;//自定义的clicklistener里保存item的position,也就是对应的控件所在行 private EditText edit; listviewClickListener(int pos, EditText e) { position = pos; edit = e; } @Override public void onClick(View v) { // TODO Auto-generated method stub if (v.getId() == R.id.info_btn) { //这里处理button点击时,直接用position来确定是list里的第几项(也就是第几行) //todo edit处理 edit.setText("current row:"+position); return; } } } //然后getview函数里 holder.add.setOnClickListener(new listviewClickListener(arg0, holder.et));
10
Result对象中添加num属性,然后点击加减号对这个num操作并notifyDataSetChanged
10
在listview的adapter里定义一个全局变量num,然后在getview方法里写加号和减号的点击事件控制num++和num–,然后在editview.settext(num+“”);就好了