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

相關問題