Android-从自定义ListView删除项目



有人知道如何从自定义列表视图中删除项目吗?我目前正在研究Android条形码扫描仪,该扫描仪将扫描的项目名称存储在库存中,并以其价格存储。我还希望用户单击它(但行不通(时将其删除。

这是我的代码,这似乎很混乱,因为我仍然是Android的初学者。

XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="in.aurora.android_barcode_scanner.Inventory"
    android:focusableInTouchMode="false"
    android:clickable="false"
    android:focusable="false">
    <ListView
        android:id="@+id/listView"
        android:layout_width="365dp"
        android:layout_height="495dp"
        tools:layout_editor_absoluteX="10dp"
        tools:layout_editor_absoluteY="8dp"
        android:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"/>
</android.support.constraint.ConstraintLayout>

实施:

public void display() {
    ShoppingListAdapter adapter = new ShoppingListAdapter(this,
        R.layout.adapter_view_layout, shoppingList);
    lv = (ListView) findViewById(R.id.listView);
    lv.setAdapter(adapter);
}
@Override
public void onItemClick(AdapterView parent, View view, final int position, long id) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Remove this item?");
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            shoppingList.remove(position);
            display();
        }
    });
    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
        }
    });
    builder.show();
}

适配器:

public class ShoppingListAdapter extends ArrayAdapter<Product> {
    private static final String TAG = "ShoppingListAdapter";
    private Context mContext;
    int mResource;
    public ShoppingListAdapter(Context context, int resource, ArrayList<Product> objects)
    {
        super(context, resource, objects);
        mContext = context;
        mResource = resource;
    }
    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        String name = getItem(position).getName();
        double price = getItem(position).getPrice();
        String convertPrice = Double.toString(price);
        Product product = new Product(name, price);
        LayoutInflater inflater  = LayoutInflater.from(mContext);
        convertView = inflater.inflate(mResource, parent, false);
        TextView productName = (TextView) 
        convertView.findViewById(R.id.textView1);
        TextView productPrice = (TextView) 
        convertView.findViewById(R.id.textView2);
        productName.setText(name);
        productPrice.setText(convertPrice);
        return convertView;
    }
}

将此方法放在适配器中:

public void remove(Product product) {
    objects.remove(product);
    notifyDataSetChanged();
}

然后这样称呼:

Product product = shoppingList.get(position);
adapter.remove(product);
adapter.notifyDataSetChanged();

还确保您的adapter类具有getCountgetItem方法:

@Override
public int getCount() {
    if(objects== null){
        return 0;
    }else {
        return objects.isEmpty()? 0 : objects.size();
    }
}

@Override
public Product getItem(int position) {
    return objects.get(position);
}

使您的适配器成为活动类中的字段

喜欢:

public class MainActivity extends AppCompatActivity {
    private ShoppingListAdapter mAdapter;

以及您的OnCreate方法内部创建shoppingList和适配器实例然后开始从列表中添加和删除项目,并在要在listView Call mAdapter.notifyDataSetChanged()

中应用更改时

喜欢:

builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        shoppingList.remove(position);
        mAdapter.notifyDataSetChanged();
    }
});

最新更新