我试图在列表视图中获取所有TextView值。
我有一个ListView,没有N个列表项。
所有列表项都有增加和减少按钮。
如果单击增加按钮,则总数(TextView)值将增加。如果我单击减少按钮,总数(TextView)将减少。
现在,我想从列表中获取所有总计数值。
这是我的代码:
holder.button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { View parentView = (View) v.getParent(); String getString = ((TextView) parentView.findViewById(R.id.counter)).getText().toString(); String totalAmtString = ((TextView) parentView .findViewById(R.id.text1)).getText().toString(); int totAmount = Integer.parseInt(totalAmtString); count = Integer.parseInt(getString); count++; tot_amt = totAmount * count; countString = String.valueOf(count); String TotAmt = String.valueOf(tot_amt); ((TextView) parentView.findViewById(R.id.counter)) .setText(countString); ((TextView) parentView.findViewById(R.id.totalPrice)).setText(TotAmt); for(int i=0;i>data.size();i++){ // Here I want to get the all TotAmt every time i click Increase button. } } });
请让我有任何想法在列表视图中获取所有TextView值。
随函附上我的完整代码:
public class ListViewExample extends Activity { ListView list; CustomAdapter adapter; public ListViewExample CustomListView = null; public ArrayListCustomListViewValuesArr = new ArrayList (); New_SQLController sqlcon; static TextView totalAmout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.database_list); sqlcon = new New_SQLController(this); InsertToDB(); BuildTable(); CustomListView = this; Resources res = getResources(); list = (ListView) findViewById(R.id.list); totalAmout=(TextView)findViewById(R.id.textView1); adapter = new CustomAdapter(CustomListView, CustomListViewValuesArr, res); list.setAdapter(adapter); } private void InsertToDB(){ sqlcon.open(); sqlcon.insertToProductTable("IMAGE","Idly","20","2","MorAndEve"); sqlcon.insertToProductTable("IMAGE","Doasi","40","1","MorAndEve"); sqlcon.insertToProductTable("IMAGE","Poori","30","1","Mor"); sqlcon.close(); } private void BuildTable() { sqlcon.open(); Cursor c = sqlcon.readEntry(); int rows = c.getCount(); int cols = c.getColumnCount(); c.moveToFirst(); for (int i = 0; i 我的适配器类
public class CustomAdapter extends BaseAdapter implements OnClickListener { private Activity activity; private ArrayList data; private static LayoutInflater inflater = null; public Resources res; ListModel tempValues = null; ListModel tempValuesNew = null; int i = 0; static int count = 0; static int tot_amt = 0; String countString = "0"; ImageButton btn; ViewHolder holder; static int OverAllTotal = 0; String overAllString = "0"; int pos; public CustomAdapter(Activity a, ArrayList d, Resources resLocal) { activity = a; data = d; res = resLocal; inflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { if (data.size() <= 0) return 1; return data.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public static class ViewHolder { public TextView text; public TextView text1; public TextView textWide; public TextView tvCounter; public ImageView image; ImageButton button; ImageButton decreesButton2; } public View getView(int position, View convertView, ViewGroup parent) { View vi = convertView; pos = position; if (cOnvertView== null) { vi = inflater.inflate(R.layout.tabitem, null); btn = (ImageButton) vi.findViewById(R.id.button1); holder = new ViewHolder(); holder.text = (TextView) vi.findViewById(R.id.text); holder.text1 = (TextView) vi.findViewById(R.id.text1); holder.image = (ImageView) vi.findViewById(R.id.list_image); holder.tvCounter = (TextView) vi.findViewById(R.id.counter); holder.button = (ImageButton) vi.findViewById(R.id.button1); holder.decreesButton2 = (ImageButton) vi.findViewById(R.id.button2); vi.setTag(holder); } else holder = (ViewHolder) vi.getTag(); if (data.size() <= 0) { holder.text.setText("No Data"); } else { tempValues = null; tempValues = (ListModel) data.get(position); holder.text.setText(tempValues.getProductName()); holder.text1.setText(tempValues.getPrice()); holder.image.setImageResource(res.getIdentifier( "com.list.listviewexample:drawable/" + tempValues.getImage(), null, null)); vi.setOnClickListener(new OnItemClickListener(position)); holder.button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { View parentView = (View) v.getParent(); String getString = ((TextView) parentView.findViewById(R.id.counter)).getText().toString(); String totalAmtString = ((TextView) parentView .findViewById(R.id.text1)).getText().toString(); int totAmount = Integer.parseInt(totalAmtString); count = Integer.parseInt(getString); count++; tot_amt = totAmount * count; countString = String.valueOf(count); String TotAmt = String.valueOf(tot_amt); ((TextView) parentView.findViewById(R.id.counter)) .setText(countString); ((TextView) parentView.findViewById(R.id.totalPrice)).setText(TotAmt); int TempTotAmt = totAmount * count; OverAllTotal = OverAllTotal + tot_amt; overAllString = String.valueOf(OverAllTotal); ListViewExample.setTotalAmount(overAllString); for(int i=0;i>data.size();i++){ } } }); holder.decreesButton2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { View parentView = (View) v.getParent(); String getString = ((TextView) parentView .findViewById(R.id.counter)).getText().toString(); String totalAmtString = ((TextView) parentView .findViewById(R.id.text1)).getText().toString(); int totAmount = Integer.parseInt(totalAmtString); count = Integer.parseInt(getString); if (count > 0) count--; countString = String.valueOf(count); tot_amt = totAmount * count; String TotAmt = String.valueOf(tot_amt); ((TextView) parentView.findViewById(R.id.totalPrice)) .setText(TotAmt); ((TextView) parentView.findViewById(R.id.counter)) .setText(countString); } }); } return vi; } @Override public void onClick(View v) { } private class OnItemClickListener implements OnClickListener { private int mPosition; OnItemClickListener(int position) { mPosition = position; } @Override public void onClick(View arg0) { ListViewExample sct = (ListViewExample) activity; sct.onItemClick(mPosition); } } }`
1> New Develope..:所以,最终我找到了解决方案:
在我的
ListViewExample
课堂上,我添加了以下方法。public void getAllValues() { View parentView = null; int temp = 0; int getTotal = 0; int totQuan = 0; int quan = 0; for (int i = 0; ilastListItemPosition) { return listView.getAdapter().getView(pos, null, listView); } else { final int childIndex = pos - firstListItemPosition; return listView.getChildAt(childIndex); } }