Yug*_*esh 1 android android-listview
在我的应用程序中使用自定义列表视图与文本视图,编辑文本和按钮.当我单击"0"位置的按钮,并在"0"位置更改文本视图值.当我向下滚动列表时 - 查看"0"中的值.位置文本视图已更改为初始状态.
我的基础适配器类
public class sample extends BaseAdapter{
public ArrayList<HashMap<String, String>> list;
Activity activity;
public sample(Activity activity,ArrayList<HashMap<String, String>> list) {
super();
this.activity = activity;
this.list = list;
}
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
class ViewHolder {
Button order;
TextView item_name, order_qty;
EditText et_quantity;
}
public View getView(final int position, View convertView,final ViewGroup parent) {
// TODO Auto-generated method stub
final ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_custom, null);
holder = new ViewHolder();
holder.order = (Button) convertView.findViewById(R.id.order);
holder.item_name = (TextView) convertView.findViewById(R.id.item_name);
holder.order_qty = (TextView) convertView.findViewById(R.id.order_count);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try{
HashMap<String, String> map = list.get(position);
holder.item_name.setText(map.get("name"));
//Log.v("Available or not", ""+map.get("available"));
}catch(Exception e){
e.printStackTrace();
}
holder.order.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
//here am click the button to change order count text value
int qty = Integer.valueOf(""+holder.order_qty.getText().toString());
qty++;
holder.order_qty.setText(""+String.valueOf(qty));
}
});
return convertView;
}
}
Run Code Online (Sandbox Code Playgroud)
向下滚动自定义列表视图时,我不知道为什么文本值更改为初始状态.任何人都知道请帮我解决此问题
问题是,您没有为每一行使用不同的视图持有者,并且在回收列表项视图时,您的持有者会被改组.
你可以看到这只是简单地向final int position你的持有者添加一个字段,在哪里存储getView位置参数; 调试,你会看到发生了什么.
因此,您无法使用持有人存储您的数量值
此外,您没有使用变量list来存储数量值.这段代码对我有用:
public View getView(final int position, View convertView,final ViewGroup parent) {
final ViewHolder holder;
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.order_custom, null);
holder = new ViewHolder();
holder.order = (Button) convertView.findViewById(R.id.order);
holder.item_name = (TextView) convertView.findViewById(R.id.item_name);
holder.order_qty = (TextView) convertView.findViewById(R.id.order_count);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
try{
HashMap<String, String> map = list.get(position);
holder.item_name.setText(map.get("name"));
holder.order_qty.setText(map.get("quantity"));
//Log.v("Available or not", ""+map.get("available"));
}catch(Exception e){
e.printStackTrace();
}
holder.order.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
//here am click the button to change order count text value
int qty = Integer.valueOf(list.get(position).get("quantity"));
qty++;
holder.order_qty.setText(""+String.valueOf(qty));
list.get(position).put("quantity", qty+"");
}
});
return convertView;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6608 次 |
| 最近记录: |