Advertisement
Guest User

hardik

a guest
Sep 28th, 2012
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.15 KB | None | 0 0
  1. package com.Listview;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import android.app.Activity;
  6. import android.app.AlertDialog;
  7. import android.content.Context;
  8. import android.content.DialogInterface;
  9. import android.content.Intent;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.LayoutInflater;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.view.ViewGroup;
  16. import android.widget.BaseAdapter;
  17. import android.widget.Button;
  18. import android.widget.CheckBox;
  19. import android.widget.CompoundButton;
  20. import android.widget.ListView;
  21. import android.widget.RadioButton;
  22. import android.widget.TextView;
  23. import android.widget.Toast;
  24. import android.widget.CompoundButton.OnCheckedChangeListener;
  25.  
  26. public class ListVIewDemoActivity extends Activity implements OnClickListener {
  27. ListView listview;
  28. Button btnok;
  29. private int temp = -1;
  30. private int temp1 = -1;
  31. ArrayList<String> items = new ArrayList<String>();
  32. int array[] = { 100, 200, 300 };
  33. long total = 0;
  34. ArrayList<String> pnames = new ArrayList<String>();
  35. ArrayList<String> selectedpnames = new ArrayList<String>();
  36. ArrayList<String> pricelist = new ArrayList<String>();
  37.  
  38. /** Called when the activity is first created. */
  39. @Override
  40. public void onCreate(Bundle savedInstanceState) {
  41. super.onCreate(savedInstanceState);
  42. setContentView(R.layout.main);
  43.  
  44. listview = (ListView) findViewById(R.id.listview);
  45. btnok = (Button) findViewById(R.id.btnok);
  46. btnok.setOnClickListener(this);
  47.  
  48. }
  49.  
  50. @Override
  51. protected void onResume() {
  52. // TODO Auto-generated method stub
  53. super.onResume();
  54. items.clear();
  55. items.add(100 + "");
  56. items.add(200 + "");
  57. items.add(300 + "");
  58.  
  59. items.add(400 + "");
  60. items.add(500 + "");
  61. items.add(600 + "");
  62. pnames.clear();
  63. pnames.add("Hardik");
  64. pnames.add("zane");
  65. pnames.add("Hardik");
  66.  
  67. pnames.add("zane");
  68. pnames.add("Hardik");
  69. pnames.add("zane");
  70.  
  71. listview.setAdapter(new ListAdapter(this, this));
  72.  
  73. }
  74.  
  75. private class ListAdapter extends BaseAdapter {
  76. LayoutInflater inflater;
  77. ViewHolder viewHolder;
  78. Activity activity;
  79.  
  80. public ListAdapter(Context context, Activity act) {
  81. inflater = LayoutInflater.from(context);
  82. this.activity = act;
  83. }
  84.  
  85. public int getCount() {
  86. return items.size();
  87. }
  88.  
  89. public Object getItem(int position) {
  90. return position;
  91. }
  92.  
  93. public long getItemId(int position) {
  94. return position;
  95. }
  96.  
  97. public View getView(final int position, View convertView,
  98. ViewGroup parent) {
  99. if (convertView == null) {
  100.  
  101. convertView = inflater.inflate(R.layout.list_row, null);
  102. viewHolder = new ViewHolder();
  103.  
  104. viewHolder.pname = (TextView) convertView
  105. .findViewById(R.id.txtname);
  106. viewHolder.price = (TextView) convertView
  107. .findViewById(R.id.txtprice);
  108. viewHolder.chk = (CheckBox) convertView
  109. .findViewById(R.id.checkbox);
  110. // viewHolder.txt_con1 = (TextView) convertView
  111. // .findViewById(R.id.txt_contact1);
  112.  
  113. convertView.setTag(viewHolder);
  114.  
  115. } else {
  116. viewHolder = (ViewHolder) convertView.getTag();
  117. }
  118.  
  119. viewHolder.pname.setText(pnames.get(position));
  120. viewHolder.price.setText(items.get(position));
  121. viewHolder.chk
  122. .setOnCheckedChangeListener(new OnCheckedChangeListener() {
  123.  
  124. @Override
  125. public void onCheckedChanged(CompoundButton buttonView,
  126. boolean isChecked) {
  127. // TODO Auto-generated method stub
  128. selectedpnames.add(pnames.get(position));
  129. pricelist.add(items.get(position));
  130. total += Integer.parseInt(items.get(position));
  131. System.out.println("Checked");
  132. if (isChecked) {
  133.  
  134. if (temp != -1) {
  135.  
  136. CheckBox tempbtn = (CheckBox) activity
  137. .findViewById(temp);
  138.  
  139. if (tempbtn != null) {
  140.  
  141. tempbtn.setChecked(false);
  142.  
  143. }
  144.  
  145. }
  146.  
  147. temp = buttonView.getId();
  148.  
  149. }
  150. selectedpnames.remove(pnames.get(position));
  151. pricelist.remove(items.get(position));
  152. total -= Integer.parseInt(items.get(position));
  153. }
  154.  
  155. });
  156. if (position == temp) {
  157.  
  158. viewHolder.chk.setChecked(true);
  159.  
  160. selectedpnames.add(pnames.get(position));
  161. pricelist.add(items.get(position));
  162. } else {
  163. viewHolder.chk.setChecked(false);
  164. }
  165.  
  166. return convertView;
  167.  
  168. }
  169. }
  170.  
  171. private class ViewHolder {
  172. TextView pname;
  173. TextView price;
  174. CheckBox chk;
  175.  
  176. }
  177.  
  178. @Override
  179. public void onClick(View v) {
  180. // TODO Auto-generated method stub
  181.  
  182. switch (v.getId()) {
  183. case R.id.btnok:
  184. for (int i = 0; i < selectedpnames.size(); i++) {
  185. Log.i("selected product names---", "selected product names---"
  186. + selectedpnames.get(i));
  187.  
  188. System.out.println("selected product names---" + " "
  189. + selectedpnames.get(i));
  190. }
  191. for (int i = 0; i < pricelist.size(); i++) {
  192. Log.i("selected product prices---", pricelist.get(i));
  193.  
  194. System.out.println("selected product prices---" + " "
  195. + pricelist.get(i));
  196. }
  197. Toast.makeText(ListVIewDemoActivity.this,
  198. "Your total amount is --" + total, Toast.LENGTH_LONG)
  199. .show();
  200.  
  201. break;
  202.  
  203. default:
  204. break;
  205. }
  206.  
  207. }
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement