删除列表视图中单击的项目 Android

Deleting the clicked item in the listview Android

我的代码java。我从意图中获取数据作为数组。我正在使用 arraylist、listview 和自定义适配器。我可以使用列表视图显示传入的数据。我希望我点击的项目被删除。 customadapter 中我的删除按钮“deleteshop”的名称。我该怎么做?

我的代码;

        final ListView list = findViewById(R.id.list);

        final ArrayList<SubjectData> arrayList = new ArrayList<SubjectData>();

        final String[] cartList = getIntent().getStringArrayExtra("saleData");

        arrayList.add(new SubjectData("", ""));

        final int cartListLength = cartList.length;
        int counter = 0;
        String lastItem = "";


        for (String e : cartList) {
            counter += 1;
            if (e == "" || e == null) {
                lastItem = cartList[counter-3];
                break;
            }

            String productPhoto = "";

            switch (e) {
                case "1 PC GREEN COLA x 10.00 TL":
                    productPhoto = "cc";
                    break;

                default:
                    productPhoto = "";
                    break;            }

          
        arrayList.add(new SubjectData(e,  productPhoto));;
        }
        arrayList.remove(arrayList.size()-1);
        arrayList.remove(arrayList.size()-1);
        arrayList.add(new SubjectData("*** GRAND TOTAL TL:" + lastItem + " ***",  "arrowgreen"));
        arrayList.add(new SubjectData("",  ""));
        arrayList.add(new SubjectData("",  ""));


        final CustomAdapter customAdapter = new CustomAdapter(this, arrayList);
        list.setAdapter(customAdapter);



主题数据模型class:


    String SubjectName;
    String Image;

    public SubjectData(String subjectName, String image) {
        this.SubjectName = subjectName;
        this.Image = image;
    }



自定义适配器;


class CustomAdapter implements ListAdapter {
    ArrayList<SubjectData> arrayList;
    Context context;
    public CustomAdapter(Context context, ArrayList<SubjectData> arrayList) {
        this.arrayList=arrayList;
        this.context=context;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

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

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

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

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final SubjectData subjectData=arrayList.get(position);
        if(convertView==null){
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView=layoutInflater.inflate(R.layout.list_row, null);
        
            TextView tittle=convertView.findViewById(R.id.title);
            ImageView imag=convertView.findViewById(R.id.list_image);
            ImageView 
            deleteshop=convertView.findViewById(R.id.deleteshop);

            deleteshop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "Deneme", 
            Toast.LENGTH_SHORT).show();
                }
            });
             
            tittle.setText(subjectData.SubjectName);
            Resources resources = context.getResources();
            final int resourceId = resources.getIdentifier(subjectData.Image, "drawable",
                    context.getPackageName());

           

            imag.setImageResource(resourceId);

          

        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return arrayList.size();
    }


    @Override
    public boolean isEmpty() {
        return false;
    }

列表视图设计;


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:padding="5dip">

    <LinearLayout
        android:id="@+id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:padding="3dip">

        <ImageView
            android:id="@+id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip" />

    </LinearLayout>

    <TextView
        android:id="@+id/title"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:gravity="center"
        android:textColor="#040404"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" />


    <ImageView
        android:layout_gravity="center"
        android:id="@+id/deleteshop"
        android:src="@drawable/negative"
        android:layout_width="25dp"
        android:layout_height="25dp" ></ImageView>



</LinearLayout>

如果你想从你的列表视图中删除点击的项目,那么你可以使用这段代码。我测试并顺利删除它,在删除它的更新列表之后。

    CustomAdapter customAdapter = new CustomAdapter(this,R.layout.list_row, arrayList);
    list.setAdapter(customAdapter);
    // after setting adapter put this code 
    // and update your ui again using set adapter
    Log.e("Custom",""+arrayList.size());
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            arrayList.remove(i);
            list.setAdapter(customAdapter);
        }
    });

这不是最佳解决方案,而是另一种选择,假设如果您想通过单击 deletebutton 更新适配器,请先创建一个界面,这里是示例

public interface MyInterface{
public void deleteItem(int pos);
}

现在在你的主 activity 中像这样使用 MainActivity extends AppCompatActivity implements MyInterface 并放置你的接口方法 public void deleteItem(int pos){ arrayList.remove(pos); list.setAdapter(customAdapter); } 之后从您的适配器调用此方法,如下所示

 deleteshop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "Delete",
                        Toast.LENGTH_SHORT).show();
                 //Actually change your list of items here
                if (context instanceof SampleList) {
                    ((MainActivity)context).deleteItem(position);
                }
            }
        });