2013-03-27 45 views
0

我一直在爲客戶/服務器應用程序工作幾個月,現在我的本科生最後一年的學位項目,我目前正在研究一個ListView,它應該顯示產品清單並允許用戶輸入代表庫存中該物品數量的值。我已經設法使View和Adapter看起來正確,但EditText引起了我的問題,我花了數小時搜索答案,並搜索了StackOverflow以查找類似的問題,但是我發現或者沒有應用或者尚未解決問題。ListView中的Android EditText - 錯誤/意外的結果

我幾乎可以肯定它是一個回收問題,但問題的根源似乎暗示了我,輸入EditText的值似乎被插入到我的散列表中的多個位置,而不僅僅是一個,並滾動列表快速導致即使不是全部視圖都顯示相同的值!這裏是我的Adapter類,我很樂意提供任何其他幫助的代碼,提前感謝你。

public class StockAdapter extends ArrayAdapter<Stock> { 

private LayoutInflater mLayoutInflater; 
Context context; 
int layoutResourceId; 
Stock data[] = null; 
Map<Integer, String> stockHashMap = new HashMap<Integer, String>(); 

public StockAdapter(Context context, int layoutResourceId, Stock[] data) 
{ 
    super(context, layoutResourceId, data); 
    mLayoutInflater = LayoutInflater.from(context); 
    this.context = context; 
    this.layoutResourceId = layoutResourceId; 
    this.data = data; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) 
{ 
    StockHolder holder = null; 

    if (convertView == null) 
    { 
     convertView = mLayoutInflater.inflate(layoutResourceId, null); 

     // Instantiate StockHolder object. 
     holder = new StockHolder(); 
     // Get views 
     holder.txtName = (TextView) convertView.findViewById(R.id.stockName); 
     holder.etValue = (EditText) convertView.findViewById(R.id.etValue); 
     convertView.setTag(holder); 
    } 
    else 
    { 
     holder = (StockHolder)convertView.getTag(); 
    } 
    // Get stock object from data set. 
    Stock stock = data[position]; 

    holder.txtName.setText(stock.Name); 

    // Put stock id in a variable - used for referencing the right stock item. 
    final int id = stock.Id; 
    String oldText = stockHashMap.get(id); 
    // Check if the value of oldText is null. 
    holder.etValue.setText(oldText == null ? "" : oldText); 

    // Create addTextChangedListener for this EditText 
    holder.etValue.addTextChangedListener(
      new TextWatcher(){ 
       public void afterTextChanged(Editable editable) { 
        stockHashMap.put(id, editable.toString()); 
       } 
       public void beforeTextChanged(CharSequence s, int start, int count, 
         int after) { 
        // TODO Auto-generated method stub 

       } 

       public void onTextChanged(CharSequence s, int start, int before, 
         int count) { 
        // TODO Auto-generated method stub 
       } 
      }); 
    return convertView; 
} 
static class StockHolder 
{ 
    int id; 
    TextView txtName; 
    EditText etValue; 
} 
} 

RE:馬特C的答案:增加了以下,但沒有更好的。

@Override 
public int getCount() 
{ 
    if (data == null) return 0; 
    return data.length; 
} 

@Override 
public Stock getItem(int position) 
{ 
    return data[position]; 
} 
+0

它似乎是正確的,請嘗試打印存儲在stockHashMap中的值以仔細檢查它是否髒。 – 2013-03-27 00:07:28

+0

我在'stockHashMap.get(id)'後面添加了一個兩個詳細的日誌語句,而在stockHashMap.put(id,editable.toString())後面又添加了兩個日誌語句:'打印出正在設置和拉入的內容HashMap和它看起來都很好;我注意到的是後者經常會將輸入的輸入設置成一個EditText到HashMap!中的兩個或三個ID:S – 2013-03-27 00:30:50

回答

0

我個人發現,如果我沒有在適配器覆蓋getCount功能我得到古靈精怪的原因不明的問題。在你的情況下,我想我會做一個空檢查,然後返回名爲「data」的Stock []數組的大小。我不能保證這會解決你的問題,但在這些情況下,對我來說,它總是「治癒」隨機奇怪。爲了完整起見,您可能還想覆蓋getItem

+0

無用恐懼,這是我添加的內容適配器: [SNIP:將代碼移到我的問題的底部以便閱讀。] – 2013-03-27 00:32:13

0

由於您有自定義的getView(),請嘗試擴展BaseAdapter而不是ArrayAdapter,這對您的情況來說要簡單得多。 ArrayAdapter最適合簡單的事情,比如每一行都是TextView ...

+0

不知道我該如何去做這件事,以及它對我有什麼好處,它是否對我有沉重的負擔?註釋? – 2013-03-27 00:44:51