我的ListView正确填充,但由于某些原因,添加和删除是古怪的,并且无法正常工作!我做错了吗?
在OnCreate()
中进行设置listView = (ListView) findViewById(R.id.ListView);
registerForContextMenu(listView);
deserializeQuotes();
if(quotes == null || quotes.size() == 0){
quotes = new ArrayList<Quote>();
//populateDefaultQuotes();
//serializeQuotes();
//getQuotesFromYQL();
}
this.quotesAdapter = new QuoteAdapter(this, R.layout.mainrow, quotes);
listView.setAdapter(this.quotesAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
deserializeQuotes();
Quote myQuote = quotes.get(position);
Toast toast = Toast.makeText(getApplicationContext(), myQuote.getName(), Toast.LENGTH_SHORT);
toast.show();
}
});
引用适配器私有类
private class QuoteAdapter extends ArrayAdapter<Quote> {
private ArrayList<Quote> items;
public QuoteAdapter(Context context, int textViewResourceId,
ArrayList<Quote> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainrow, null);
}
Quote q = items.get(position);
if (q != null) {
TextView nameText = (TextView) v.findViewById(R.id.nameText);
TextView priceText = (TextView) v.findViewById(R.id.priceText);
TextView changeText = (TextView) v.findViewById(R.id.changeText);
if (nameText != null) {
nameText.setText(q.getSymbol());
}
if (priceText != null) {
priceText.setText(q.getLastTradePriceOnly());
}
if (changeText != null) {
changeText.setText(q.getChange());
}
}
return v;
}
}
从列表中删除项目(此操作无效)
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Remove"){
deserializeQuotes();
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
quotesAdapter.remove(quotes.get(info.position));
quotesAdapter.notifyDataSetChanged();
serializeQuotes();
}
else {
return false;
}
return true;
}
将项目添加到列表中(此工作)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
deserializeQuotes();
this.quotesAdapter = new QuoteAdapter(this, R.layout.mainrow, quotes);
quotesAdapter.notifyDataSetChanged();
listView.setAdapter(quotesAdapter);
}
}
以下是我序列化和反序列化的方法
private void serializeQuotes(){
FileOutputStream fos;
try {
fos = openFileOutput(Constants.FILENAME, Context.MODE_PRIVATE);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(quotes);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
private void deserializeQuotes(){
try{
FileInputStream fis = openFileInput(Constants.FILENAME);
ObjectInputStream ois = new ObjectInputStream(fis);
quotes = (ArrayList<Quote>) ois.readObject();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
答案 0 :(得分:0)
代码似乎没问题。您可以尝试在删除中使用此功能吗?:
if("Remove".equals(item.getTitle())) {
//....
}
修改强>
我刚刚注意到你通过调用deserializeQuotes()
来反序列化“Quote”对象。你是否覆盖了Quote对象的boolean equals(Object)
方法?反序列化时,创建的对象不是“相同的”,即它们完全是新对象,如果你没有覆盖equals方法:
quotesAdapter.remove(quotes.get(info.position));
将失败,因为它不会在列表中找到要删除的任何Quote对象。
你能检查一下吗?