Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2.  
  3. Handler h = new Handler() {
  4.  
  5. @Override
  6. public void handleMessage(Message msg) {
  7. if (msg.what == 1){
  8. goodsList.get(0);
  9.  
  10.  
  11.  
  12.  
  13. }
  14.  
  15.  
  16.  
  17. }
  18. };
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27. ArrayList<Goods> goodsList = new ArrayList<>();
  28.  
  29. @Override
  30. protected void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.activity_main);
  33.  
  34. goodsList.add(new Goods("Apple", "50"));
  35. goodsList.add(new Goods("Orange", "30"));
  36. goodsList.add(new Goods("Milk", "20"));
  37. goodsList.add(new Goods("Suger", "25"));
  38. goodsList.add(new Goods("Chokolate", "25"));
  39.  
  40. ListView listView = (ListView) findViewById(R.id.listView);
  41. listView.setAdapter(new MyAdapter(this, goodsList));
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50. }
  51.  
  52. public class Goods {
  53. String goods;
  54. String count;
  55.  
  56. public Goods(String goods, String count) {
  57. this.goods = goods;
  58. this.count = count;
  59. }
  60.  
  61.  
  62.  
  63. public String getGoods() {
  64. return goods;
  65. }
  66.  
  67. public void setGoods(String goods) {
  68.  
  69. this.goods = goods;
  70. }
  71. public String getCount(){
  72. return count;
  73. }
  74. public void setCount(String count){
  75. this.count = count;
  76. }
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. class MyAdapter extends ArrayAdapter<Goods> {
  86.  
  87. public MyAdapter(Context context, ArrayList<Goods> objects) {
  88. super(context, R.layout.list_item, objects);
  89. }
  90.  
  91. @Override
  92. public View getView(int position, View convertView, ViewGroup parent) {
  93. ViewHolder holder;
  94. View rowView = convertView;
  95. if (rowView == null) {
  96. LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  97. rowView = inflater.inflate(R.layout.list_item, parent, false);
  98. holder = new ViewHolder();
  99. holder.textView = (TextView) rowView.findViewById(R.id.TextOfList);
  100. holder.textViewCount = (TextView) rowView.findViewById(R.id.textCount);
  101. rowView.setTag(holder);
  102. } else {
  103. holder = (ViewHolder) rowView.getTag();
  104. }
  105.  
  106. Goods goods = getItem(position);
  107. holder.textView.setText(goods.getGoods());
  108. holder.textViewCount.setText(goods.getCount());
  109.  
  110.  
  111. return rowView;
  112. }
  113.  
  114. class ViewHolder {
  115.  
  116. public TextView textView;
  117. public TextView textViewCount;
  118.  
  119. }
  120.  
  121.  
  122. }
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement