Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- Handler h = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- if (msg.what == 1){
- goodsList.get(0);
- }
- }
- };
- ArrayList<Goods> goodsList = new ArrayList<>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- goodsList.add(new Goods("Apple", "50"));
- goodsList.add(new Goods("Orange", "30"));
- goodsList.add(new Goods("Milk", "20"));
- goodsList.add(new Goods("Suger", "25"));
- goodsList.add(new Goods("Chokolate", "25"));
- ListView listView = (ListView) findViewById(R.id.listView);
- listView.setAdapter(new MyAdapter(this, goodsList));
- }
- public class Goods {
- String goods;
- String count;
- public Goods(String goods, String count) {
- this.goods = goods;
- this.count = count;
- }
- public String getGoods() {
- return goods;
- }
- public void setGoods(String goods) {
- this.goods = goods;
- }
- public String getCount(){
- return count;
- }
- public void setCount(String count){
- this.count = count;
- }
- }
- class MyAdapter extends ArrayAdapter<Goods> {
- public MyAdapter(Context context, ArrayList<Goods> objects) {
- super(context, R.layout.list_item, objects);
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder;
- View rowView = convertView;
- if (rowView == null) {
- LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- rowView = inflater.inflate(R.layout.list_item, parent, false);
- holder = new ViewHolder();
- holder.textView = (TextView) rowView.findViewById(R.id.TextOfList);
- holder.textViewCount = (TextView) rowView.findViewById(R.id.textCount);
- rowView.setTag(holder);
- } else {
- holder = (ViewHolder) rowView.getTag();
- }
- Goods goods = getItem(position);
- holder.textView.setText(goods.getGoods());
- holder.textViewCount.setText(goods.getCount());
- return rowView;
- }
- class ViewHolder {
- public TextView textView;
- public TextView textViewCount;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement