我希望通过单击列表视图的按钮获取列表视图的特定行项目,并将该项目保存在数据库中。我怎样才能做到这一点?

时间:2016-08-13 08:07:04

标签: android sqlite listview

我想点击列表视图的按钮获取特定的行数据并保存在数据库中!

如何通过点击按钮获取特定的行数据?

@JSONProperty

这是我的getview方法: -

public class CategoryAdapter extends BaseAdapter {
  private ArrayList<PojoCategory> mList;
  private Context mContext;
  private ViewHolder viewHolder;
  private GeneralUtilities generalUtilities;
  private DataBaseHandler dbh;


public CategoryAdapter(Context mContext, ArrayList<PojoCategory> mList) {

  this.mContext = mContext;
  this.mList = mList;
  generalUtilities = new GeneralUtilities(mContext);
  dbh = new DataBaseHandler(mContext);        
}


@Override
  public int getCount() {
     if (mList != null) {
        return mList.size();        
     }
  return 0;
  }

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

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

我想在点击此按钮时将listview的特定行项添加到数据库中: -

 @Override
    public View getView(final int position, View view, ViewGroup parent) {

      LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      if (view == null) {
          view = layoutInflater.inflate(R.layout.category, parent, false);

          viewHolder = new ViewHolder();
          viewHolder.Dish_name = (TextView) view.findViewById(R.id.nameofdish);
          viewHolder.dish_description = (TextView) view.findViewById(R.id.dishdescription);
          viewHolder.Dish_rate = (TextView) view.findViewById(R.id.rateofdish);
          viewHolder.item_image = (ImageView) view.findViewById(R.id.img_item);
          viewHolder.btnAddCart = (Button) view.findViewById(R.id.btn_add_cart);                   

      } else {        
          viewHolder = (ViewHolder) view.getTag();

      }

      view.setTag(viewHolder);
      final PojoCategory pojoCategory = (PojoCategory) getItem(position);
      viewHolder.Dish_name.setText(pojoCategory.getDish_name());
       viewHolder.dish_description.setText(pojoCategory.getdish_description());
            viewHolder.Dish_rate.setText(pojoCategory.getDish_rate());
            Glide.with(mContext).load(pojoCategory.getCategory_Image()).into(viewHolder.item_image);

    viewHolder.btnAddCart.setTag(position);

这是我的观点持有者类

viewHolder.btnAddCart.setOnClickListener(new View.OnClickListener() {
    @Override
       public void onClick(View v) {        
            dbh.insertData(pojoCategory.getDish_name(), pojoCategory.getDish_rate(), pojoCategory.getCategory_Image());
       }
      }
 });
 return view;

        }

我想添加行数据,如名称,数据库中特定行的价格,点击按钮添加到列表视图中的购物车。

0 个答案:

没有答案