【问题标题】:Deleting the clicked item in the listview Android删除listview Android中点击的项目
【发布时间】:2022-01-20 09:25:22
【问题描述】:

我的代码java。我从意图中获取数据作为数组。我正在使用 arraylist、listview 和自定义适配器。我可以使用 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);



SubjectData 模型类:


    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>

【问题讨论】:

  • 你的问题是不完整的,没有你的适配器和列表项设计你能提供吗?
  • @AjayKS 谢谢。我也加了

标签: android listview arraylist android-adapter listviewitem


【解决方案1】:

如果您想从列表视图中删除单击的项目,那么您可以使用此代码。我测试并顺利删除,在删除其更新列表后也删除。

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

现在在您的主要活动中使用这样的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);
                }
            }
        });

【讨论】:

  • 感谢您的成功。但我想按下并删除自定义适配器类中的一个按钮。按钮名称“删除商店”。我该怎么做
  • 然后你可以把arraylist.remove(position)放在deletebutton的点击监听器上并调用notifyDataSetChanged();或在删除后更新您的适配器。
  • arraylist.remove(position) 正在工作。但是 notifyDataSetChanged();不工作。没有这样的代码。我想这是因为我正在使用 ListAdapter。
  • 一个替代解决方案是从您设置适配器的主要活动中更新您的适配器。你可以使用方法。
  • 哪种方法?如果你能写代码我会很高兴
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 2011-02-03
  • 1970-01-01
  • 1970-01-01
  • 2012-06-06
相关资源
最近更新 更多