我有一个使用自定义 ListAdapter 构建的 listView。我需要访问列表视图中的所有子项目,以便在 ListView 的每个项目中获取 TextView 的所有文本。
private void calcTotalSales ()
{
View v;
TextView tx;
String abc="";
ListView myList = (ListView) findViewById(R.id.todaySalesListView);
for (int i = 0; i < myList.getChildCount(); i++)
{
v = (View) myList.getChildAt(i);
tx = (TextView) v.findViewById(R.id.quantityTB);
Log.d ("Text", tx.getText().toString());
}
}
加入我的 listAdapter :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
final int _position = position;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.activity_today_sales_list_view, null);
holder = new ViewHolder();
holder.txt_itemName = (TextView) convertView.findViewById(R.id.itemNameTB);
holder.txt_itemPrice = (TextView) convertView.findViewById(R.id.priceTB);
holder.txt_itemQuantity = (TextView) convertView.findViewById(R.id.quantityTB);
holder.txt_plusBtn = (Button) convertView.findViewById(R.id.addBtn);
holder.txt_minusBtn = (Button) convertView.findViewById(R.id.minusBtn);
holder.txt_plusBtn.setTag(holder);
holder.txt_minusBtn.setTag(holder);
convertView.setTag(holder);
holder.txt_plusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Set Text of TextView
}
});
holder.txt_minusBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Set Text of TextView
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txt_itemName.setText(itemDetailsrrayList.get(position).getItemName());
holder.txt_itemPrice.setText("RM "+itemDetailsrrayList.get(position).getItemCost());
return convertView;
}
这是我面临的问题:
- 我无法访问列表视图中的第一项
- 对项目的访问不一致(意味着有时成功,有时失败)
任何人都可以帮忙吗?