热释光;博士
我不知道如何从对话框中更改我的arraylist数据集,以便它更新我的listview。我当前的解决方案崩溃,因为listview没有更新。我需要知道我的代码哪里出错了。
问题
我有一个活动,它有一个项目的listview,其中有一个arrayadapter将其连接到我的数据(arraylist)。当用户单击列表中的某个项目时,我会加载一个alertdialog,允许用户删除该项目。调试显示该项确实从数据中删除,对话框按预期关闭,但listview不更新,随后的单击会导致应用程序崩溃。
这个问题似乎与itemslist.notifydatasetchanged()有关;。我试着打适配器,但适配器本身无法解决。
我知道这是由于我的一个错误,但我找不到哪里我错了。
代码
scanneractivity.java文件
import android.content.Intent;
import android.graphics.pdf.PdfDocument;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class ScannerActivity extends AppCompatActivity {
Button delete, deleteAll, manual, export, newPull;
EditText input_main;
static int pulls = 001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scanner);
//Load some items for testing
final PullSheet pullSheet = new PullSheet();
pullSheet.createSheet(nameText, roleText, storeText, runitText);
pullSheet.addItem("12543", 5);
pullSheet.addItem("3526", 1);
pullSheet.addItem("00045", 3);
pullSheet.addItem("95462", 18);
pullSheet.addItem("1181", 53);
pullSheet.addItem("6543", 1);
final ArrayAdapter<PullItem> itemAdapter =
new ArrayAdapter<PullItem>(this, 0, pullSheet.SheetItems) {
@Override
public View getView(int position,
View convertView,
ViewGroup parent){
PullItem currentItem = getItem(position);//
if(convertView == null) {
convertView = getLayoutInflater()
.inflate(R.layout.item, null, false);
}
TextView itemSku =
(TextView)convertView.findViewById(R.id.item_sku);
TextView itemQty =
(TextView)convertView.findViewById(R.id.item_qty);
itemSku.setText(currentItem.sku);
itemQty.setText(String.valueOf(currentItem.qty));
return convertView;
}
};
final ListView itemsList = (ListView) findViewById(R.id.itemsList);
itemsList.setAdapter(itemAdapter);
itemsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> itemAdapter,
View view, int position, long rowId) {
final PullItem item = pullSheet.SheetItems.get(position);
final PullSheet sheet = pullSheet;
//load dialog
final AlertDialog.Builder mBuilder = new AlertDialog.Builder(ScannerActivity.this);
final View mView = getLayoutInflater().inflate(R.layout.dialog_interact, null);
//Declare dialog ui elements
Button mYes = (Button) mView.findViewById(R.id.btnYes);
Button mNo = (Button) mView.findViewById(R.id.btnNo);
TextView skuData = (TextView) mView.findViewById(R.id.skuData);
TextView qtyData = (TextView) mView.findViewById(R.id.qtyData);
skuData.setText(item.sku);
qtyData.setText(item.qty.toString());
mBuilder.setView(mView);
final AlertDialog dialog = mBuilder.create();
dialog.show();
mYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deletePullItem(sheet, item);
itemsList.notifyDataSetChanged();// This isn't working, "Cannot resolve method notifyDataSetChanged()"
dialog.dismiss();
}
});
mNo.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
});
}
public void deletePullItem(PullSheet sheet, PullItem item)
{
sheet.removeItem(item);
}
}
图纸.java
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;
public class PullSheet extends Application{
protected String pullerName;
protected String role;
protected String store;
protected String sheetId;
protected String runItId;
protected Long startTime = System.currentTimeMillis();
protected ArrayList<PullItem> SheetItems = new ArrayList<PullItem>();
public void createSheet(String _pullerName, String _role, String _store, String _runItId){
setPullerName(_pullerName);
setRole(_role);
setStore(_store);
setRunItId(_runItId);
setSheetId();
}
protected void addItem(String sku, Integer qty){
SheetItems.add(new PullItem(sku, qty));
}
protected void removeItem(PullItem item){
this.SheetItems.remove(item);
}
//Getters and setters
public String getPullerName(){ return pullerName; }
public String getRole(){
return role;
}
public String getStore(){
return store;
}
public String getRunItId(){
return runItId;
}
public String getSheetId(){
return sheetId;
}
public Long getStartTime(){
return startTime;
}
private void setPullerName(String _pullerName){ pullerName = _pullerName; }
private void setRole(String _role){ pullerName = _role; }
private void setStore(String _store){ store = _store; }
private void setRunItId(String _runItId){ runItId = _runItId; }
private void setSheetId(){
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
String dateString = formatter.format(new Date(startTime));
UUID uuid = UUID.randomUUID();
String randUUID = uuid.toString();
sheetId = dateString + "-" + this.pullerName + "-" + randUUID + "-" + runItId;
}
}
pullitem.java文件
public class PullItem {
protected String sku;
protected Integer qty;
public PullItem(String sku, Integer qty) {
setSku(sku);
this.qty = 1;
}
String getSku(){ return sku; }
Integer getQty(){ return qty; }
void setSku(String _sku){ sku = _sku; }
void setQty(Integer _qty){ qty = _qty; }
void incrementQty(){ qty += 1; }
}
解决方案
我需要别人告诉我哪里出了问题。我知道这与arrayadapter和listview有关,但我尝试的所有操作都只是将“无法解析”移到表达式的另一部分。
提前谢谢你的帮助
2条答案
按热度按时间rbpvctlc1#
试试这个:
f4t66c6m2#
notifyDatasetChanged()
是适配器方法,因此不能与一起使用ListView
但这应该行得通:使适配器成为一个成员变量(以您的方式示例化它),这样您就可以在系统中的任何地方访问它
Activity
:打个电话就行了
deletePullItem()
如果用户单击“确定”按钮:打电话给我
notifyDatasetChanged()
删除项目时: