我已经从sqlite数据库填充了我的自定义列表视图。列表视图自定义行包含一个imageview,textview和按钮。

时间:2017-03-19 23:10:12

标签: java android sqlite listview android-studio

我想在按下该行的按钮时对该行执行删除操作。当我通过我们自己的id传递时,删除查询工作正常。我在onclick方法中获取行的id时遇到问题请帮助

这是我的代码

package com.example.vasu.virtualfridge;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class ContactImageAdapter extends ArrayAdapter<Item>{
    Context context;
    int layoutResourceId;
    // BcardImage data[] = null;
    ArrayList<Item> data=new ArrayList<Item>();
    public ContactImageAdapter(Context context, int layoutResourceId, ArrayList<Item> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(final int position, final View convertView, ViewGroup parent) {
        View row = convertView;
        ImageHolder holder = null;

        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new ImageHolder();
            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
            holder.b = (Button)row.findViewById(R.id.button2);
            row.setTag(holder);
        }
        else
        {
            holder = (ImageHolder)row.getTag();
        }

        Item picture = data.get(position);
        holder.txtTitle.setText(picture._name);
        //convert byte to bitmap take from contact class

        byte[] outImage=picture._Image;
        ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
        Bitmap theImage = BitmapFactory.decodeStream(imageStream);
        holder.imgIcon.setImageBitmap(theImage);
        holder.b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int id = getPosition(getItem(position));
               //Object toRemove = data.get(id);
                DatabaseHandler db = new DatabaseHandler(context);
                db.deleteContact(1);
                data.remove(position);
                notifyDataSetChanged();


                Toast.makeText(context,"Removed",Toast.LENGTH_LONG).show();
            }
        });
        return row;

    }

    static class ImageHolder
    {
        ImageView imgIcon;
        TextView txtTitle;
        Button b;
    }
}

2 个答案:

答案 0 :(得分:0)

可能看起来你想要获得点击的行id。因为有一个getItemAt( position )函数。在parent上调用它。希望它有帮助!

答案 1 :(得分:0)

对您的代码和适配器模态类进行以下更改:

  1. 如果已经完成,则在Item.java模态类中添加id字段,然后忽略。
  2. 从Sqlite DB获取数据时设置此id字段。这应该是你的主键。
  3. Item picture = data.get(position);代码移至if(row == null)上方。
  4. 在初始化holder.b
  5. 后添加此代码holder.b.setTag(picture);
  6. 更改按钮点击监听器,如下所示

    holder.b.setOnClickListener(new View.OnClickListen(){@Override public void onClick(View view) {
    
    Item item = (Item)view.getTag();
    DatabaseHandler db = new DatabaseHandler(context);
    db.deleteContact(item.getId());//item.getId() refers to your row id that you have fetched from Sqlite
    data.remove(position);
    notifyDataSetChanged();
    
    
    Toast.makeText(context,"Removed",Toast.LENGTH_LONG).show();});