【问题标题】:Retrieve ArrayList value from GridView at BaseAdapter从 BaseAdapter 的 GridView 中检索 ArrayList 值
【发布时间】:2016-06-30 04:03:55
【问题描述】:

我有多个数组。我将它发送到 GridView。所以这就是例子:

  • 数组 A:5,3,2,6

  • 阵列 B:汽车、刀具、笔记本电脑、风扇

我有两个按钮。有加号和减号。 因此,当我按下 + / - 按钮时,数组 A 中的值会自动发生变化。例如,我将汽车 (5) 的值更改为 (2),因此数组 A 中的值变为:2,3,2,6

所以问题是我想再次将其置于 ArrayList 状态。这是我的代码:

public class CustomGridView4 extends BaseAdapter {
    private ArrayList<ListItem> listData;
    private LayoutInflater layoutInflater;
    private Context context;
    private int count = 0;
    private DBHelper myDb;
    public CustomGridView4(Context context, ArrayList<ListItem> listData) {
        this.listData = listData;
        layoutInflater = LayoutInflater.from(context);
        this.context = context;
        myDb = new DBHelper(context);
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.afterlogin_product_gridview, null);
            holder = new ViewHolder();
            holder.headlineView = (TextView) convertView.findViewById(R.id.nama_produk);
            holder.teaserView = (TextView) convertView.findViewById(R.id.harga);
            holder.imageView = (ImageView) convertView.findViewById(R.id.img_produk);
            holder.cmdMinus = (Button) convertView.findViewById(R.id.btn_min);
            holder.cmdPlus = (Button) convertView.findViewById(R.id.btn_plus);
            holder.qty = (TextView) convertView.findViewById(R.id.lbl_qty);
            holder.layout1 = (LinearLayout) convertView.findViewById(R.id.layout1);
            holder.satuan = (TextView) convertView.findViewById(R.id.satuan);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final ListItem newsItem = listData.get(position);
        String satuan = "/ " + newsItem.getSatuan().toString();
        String harga = newsItem.getReporterName().toString();
        Integer qtys = newsItem.getQuantity();
        holder.headlineView.setText(newsItem.getHeadline().toUpperCase());
        holder.teaserView.setText(harga);
        holder.satuan.setText(satuan);
        holder.qty.setText(String.valueOf(qtys));
        String a = newsItem.getUrl();

        if (qtys == 0)
        {
            holder.layout1.setBackgroundResource(R.drawable.border_radius);
        }
        else
        {
            holder.layout1.setBackgroundResource(R.drawable.border_radius_gridview);
        }

        holder.cmdPlus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count = newsItem.getQuantity();
                count++;
                newsItem.setQuantity(count);
                System.out.println("ASD : " + newsItem.getQuantity());
                holder.qty.setText("" + newsItem.getQuantity());
            }
        });

        holder.cmdMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count = newsItem.getQuantity();
                if (count == 0) {
                    holder.qty.setText("0");
                    newsItem.setQuantity(0);
                } else {
                    count--;
                    newsItem.setQuantity(count);
                    holder.qty.setText("" +newsItem.getQuantity());
                }
            }
        });

        if (holder.imageView != null) {
            //new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
            File file = new File(Environment.getExternalStoragePublicDirectory(".BlanjaPasar/Product"), a);
            Picasso
                    .with(context)
                    .load(file)
                    .fit()
                    .noFade()
                    .into(holder.imageView);
        }

        holder.qty.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!holder.qty.getText().toString().matches("0"))
                {
                    holder.layout1.setBackgroundResource(R.drawable.border_radius_gridview);
                }
                else
                {
                    holder.layout1.setBackgroundResource(R.drawable.border_radius);
                }
            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView headlineView;
        TextView teaserView;
        ImageView imageView;
        TextView satuan,qty;
        Button cmdPlus,cmdMinus;
        LinearLayout layout1;
    }

}

操作在 holder.cmdMinus 和 holder.cmdPlus 按钮中。

这是截图:

所以当我按下“Tambahkan”按钮时,我想再次获取 arraylist 值。这可能吗?

更新:

这是 ArrayList 来自我的活动的地方:

public ArrayList<ListItem> getListData() {
        ArrayList<ListItem> listMockData = new ArrayList<ListItem>();

        for (int i = 0; i < jArray; i++) {
            ListItem newsData = new ListItem();
            newsData.setKode(kode_prdct[i]);
            newsData.setUrl(img_prdct[i]);
            newsData.setHeadline(nama_prdct[i]);
            newsData.setReporterName(harga_prdct[i]);
            newsData.setSatuan(satuan_prdct[i]);
            newsData.setQuantity(qty_prdct[i]);
            listMockData.add(newsData);
        }
        return listMockData;
    }

这就是我调用gridview的方式:

 ArrayList<ListItem> listData = getListData();
        product_gridview.setAdapter(new CustomGridView4(AfterLogin_HistoryDetail.this, listData));

【问题讨论】:

    标签: java android gridview arraylist


    【解决方案1】:

    有两种方法可以做到这一点。

    方法一

    使 listData 成为活动中的类变量。

    private ArrayList<ListItem> listData;
    

    因为 listData 是通过引用传递的,所以您访问的是同一个副本。

    listData = getListData();
    product_gridview.setAdapter(new CustomGridView4(AfterLogin_HistoryDetail.this, listData));
    

    方法二

    从适配器公开它。

    public class CustomGridView4 extends BaseAdapter {
        // Your code
        public ArrayList<ListItem> getListItems() {
            return listData;
        }
    }
    

    您还必须使适配器成为类变量。

    private CustomGridView4 adpater;
    
    ArrayList<ListItem> listData = getListData();
    adpater = new CustomGridView4(AfterLogin_HistoryDetail.this, listData)
    product_gridview.setAdapter(adpater);
    

    【讨论】:

    • 如果我使用方法1,我如何获得数量值?我是否需要再次创建类似于 getListData() 的方法?
    • @LeonardFebrianto 假设您知道自己想获得的职位。 listData.get(i).getQuantity() 将工作。如果您再次使用getListData(),它将创建一个新列表,而不是适配器中的列表。
    • 好的,我明白了。谢谢
    【解决方案2】:

    如果你想在 Activity 中检索 arraylist 值(从 BaseAdapter 中取出),你可以从 Activity 创建一个接口。

    public interface OnChangeValueListener{
        void getValue(Object object);
    }
    

    在你的activity中实现这个接口

    MainActivity extends Activity implements OnChangeValueListener...
    
    @Override
    public void getValue(Object value){
      //get your value
    }
    
    public CustomGridView4(Context context, ArrayList<ListItem> listData, **YourListener**){
      this.onChangeValueListener = YourListener;
    }
    
    holder.cmdMinus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                count = newsItem.getQuantity();
                if (count == 0) {
                    holder.qty.setText("0");
                    newsItem.setQuantity(0);
    
                } else {
                    count--;
                    newsItem.setQuantity(count);
                    holder.qty.setText("" +newsItem.getQuantity());
    
    
                }
    
                **this.onChangeValueListener.getValue(value);**
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多