有谁知道如何从自定义ListView中删除项目?我目前正在开发一种Android条形码扫描仪,它将扫描的商品名称及其价格存储在库存中。我还希望在用户点击它时将其删除(但它不起作用)。
这是我的代码,可能看起来很乱,因为我还是Android的初学者。
<?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;
}
}
答案 0 :(得分:1)
将此方法放在适配器中:
public void remove(Product product) {
objects.remove(product);
notifyDataSetChanged();
}
然后这样称呼:
Product product = shoppingList.get(position);
adapter.remove(product);
adapter.notifyDataSetChanged();
还要确保您的adapter
课程有getCount
和getItem
方法:
@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);
}
答案 1 :(得分:0)
使适配器成为活动类
中的字段 像这样:public class MainActivity extends AppCompatActivity {
private ShoppingListAdapter mAdapter;
并在onCreate方法中创建shoppingList
和适配器实例
然后开始在列表中添加和删除项目,以及在ListView调用mAdapter.notifyDataSetChanged()
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
shoppingList.remove(position);
mAdapter.notifyDataSetChanged();
}
});