如何删除自定义列表视图行

时间:2019-04-21 10:08:19

标签: java android listview

我试图通过单击扩展Baseadapter的自定义列表视图适配器来删除一行或任何一行。我在Google上找到了许多解决方案,但没有一个对我有用。在此类的帮助下,我成功地生成了一个列表视图,但是无法删除一行或任何一行。 这是我的代码

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.BaseAdapter;

import com.brl.loverfreedatingapp.R;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class WhoLikedMeAdapter extends BaseAdapter {
    private Context context; //context
    private ArrayList<String> masterID = new ArrayList<String>();
    private ArrayList<String> slaveID = new ArrayList<String>();
    private ArrayList<String> masterName = new ArrayList<String>();
    private ArrayList<String> slaveName = new ArrayList<String>();
    private ArrayList<String> slaveUrl = new ArrayList<String>();

    public WhoLikedMeAdapter(Context context, ArrayList<String> masterID, ArrayList<String> slaveID, ArrayList<String> masterName, ArrayList<String> slaveName, ArrayList<String> slaveUrl) {
        this.context = context;
        this.masterID = masterID;
        this.slaveID = slaveID;
        this.masterName = masterName;
        this.slaveName = slaveName;
        this.slaveUrl = slaveUrl;
    }

    @Override
    public int getCount() {
        return masterID.size(); //returns total of items in the list
    }

    @Override
    public Object getItem(int position) {
        return masterID.get(position); //returns list item at the specified position
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // inflate the layout for each list row
        if (convertView == null) {
            convertView = LayoutInflater.from(context).
                    inflate(R.layout.wholikeme_row, parent, false);
        }




        CircleImageView profile_image = (CircleImageView) convertView.findViewById(R.id.profile_image);
        TextView name = (TextView)convertView.findViewById(R.id.name);
        ImageButton unlike = (ImageButton)convertView.findViewById(R.id.unlike);
        ImageButton like = (ImageButton)convertView.findViewById(R.id.like);

        name.setText(slaveName.get(position));

        //--

        if(slaveUrl.get(position).equals("")){
            Picasso.with(context)
                    .load(R.drawable.image_placeholder)
                    //.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                    .placeholder(R.drawable.image_placeholder)
                    .into(profile_image);
        }else {
            Picasso.with(context)
                    .load(slaveUrl.get(position))
                    //.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                    .placeholder(R.drawable.image_placeholder)
                    .into(profile_image);
        }



        //-------------

        like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {




                masterID.remove(position);// not working
                notifyDataSetChanged();


            }
        });

        unlike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                masterID.remove(position);// not working
                notifyDataSetChanged();

            }
        });

        // returns the view for the current row
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

找到了解决方案!我必须删除一行中的所有数据才能删除listview行。以前我只尝试使用1个数据(masterID)。

masterID.remove(position);
slaveID.remove(position);
masterName.remove(position);
slaveName.remove(position);
slaveUrl.remove(position);
notifyDataSetChanged();