1

我的ListView填充正确,但由于某种原因添加和删除是古怪的,并不能正常工作!难道我做错了什么?我是否正确创建自定义ArrayAdapter?

设置的东西,在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

您的“添加项目”和“删除项目”代码是相同的。一个错字? – naikus 2010-09-01 04:58:33

+0

好抓!更新。 – 2010-09-01 05:06:47

+0

我刚刚花了大部分时间与类似的问题作斗争。已经得出了ArrayAdapter只是被破坏的结论,至少对于自定义类型。 – 2011-11-19 00:41:04

回答

0

代码似乎是好的。你可以尝试在你删除使用此?:

if("Remove".equals(item.getTitle())) { 
    //.... 
} 

编辑:

我只注意到致电deserializeQuotes()你解串行“报价”的对象。你是否重写了Quote对象的boolean equals(Object)方法?当你反序列化,创建的对象不是“相同”,也就是说,它们是新的对象共有,如果你有没有覆盖equals方法:因为它不会找到任何引用

quotesAdapter.remove(quotes.get(info.position)); 

将失败对象在列表中删除。

你能检查吗?

+0

更新了删除代码。等于更正确。不幸的是,删除仍然无法正常工作。 – 2010-09-01 05:32:50

+0

@Sheehan Alam我已经更新了我的答案,看看它是否有帮助。 – naikus 2010-09-01 05:54:21

+0

我over-rode equals()但是我不在反序列化或序列化中的任何地方使用它。任何指针? – 2010-09-01 17:28:21

相关问题