从BaseAdapter

时间:2016-06-30 04:03:55

标签: java android gridview arraylist

我有多个阵列。我把它发送给GridView。所以这是一个例子:

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

  • 阵列B:汽车,小刀,笔记本电脑,风扇

我有两个按钮。有加号和减号。 因此,当我按下+ / - 按钮时,数组A中的值会自动改变。例如,我将car(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&amp; holder.cmdPlus按钮。

以下是截图:

enter image description here

所以当我按下“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));

2 个答案:

答案 0 :(得分:1)

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

方法1

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

private ArrayList<ListItem> listData;

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

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

方法2

将其从适配器中暴露出来。

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 :(得分:0)

如果要检索activity中的arraylist值(out BaseAdapter),可以从Activity创建一个接口。

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

在您的活动中实现此界面

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);**
        }
    });