2012-03-01 72 views
0

昨天我問了這個問題,但是我寫得比我想象的這個問題複雜得多。我很困惑,我在這裏發現了一些非常類似的問題,但他們都沒有解決我的問題。所以我有一個listview,每個listview都包含一個圖像,一個textview和一個edittext,但只有edittext很重要。 如果我點擊textview,我想記錄它的內容進行測試。我的問題是,如果用戶改變它,然後改變我登錄同樣的事情。所以在模擬器中改變它,而不是使用setText,它什麼都不做。我有一個數據庫,在每個listview中都有一個edittext(holder.quant)。如果我改變它,Edittext內容不刷新

類MyAdapter擴展ArrayAdapter {

LayoutInflater inflat; 
    ViewHolder holder; 
    public MyAdapter(Context context, int textViewResourceId, 
      ArrayList<EachRow> objects) 
    { 

     super(context, textViewResourceId, objects); 
     // TODO Auto-generated constructor stub 
     inflat=LayoutInflater.from(context); 
    } 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     if(convertView==null) 
     { 
      convertView=inflat.inflate(R.layout.row_checkox, null); 
      holder=new ViewHolder(); 
      holder.textView=(TextView)convertView.findViewById(R.id.textView111); 
      holder.image=(ImageView)convertView.findViewById(R.id.imageView111); 



      holder.quant = (EditText)convertView.findViewById(R.id.quantity); 


      holder.image.setOnClickListener(CustomList.this); 
      convertView.setTag(holder); 
     } 
     holder=(ViewHolder) convertView.getTag(); 



     holder.textView.setOnClickListener(new View.OnClickListener() { 


      @Override 
      public void onClick(View v) { 

       TextView tv = (TextView) v ; 
      String text_of_clicked_textview = tv.getText().toString(); 

      final String[] ddata = text_of_clicked_textview.split(" "); //split the result using the spaces (so you could obtain the name, hotness and the other string you use) 


       final long yy = Long.parseLong(ddata[0]); 



       info.open(); 
       info.updateQuan(yy, holder.quant.getText().toString()); 
       Log.i("tagitagi", holder.quant.getText().toString()); 
       info.close(); 
      } 
     }); 



     EachRow row= getItem(position); 
     EachRow row2= getItem2(position); 
     holder.quant.setText(row2.text); 

     holder.textView.setText(row.text); 
     NullPointerException 
     holder.image.setTag(position); 
     holder.quant.setTag(position); 

     return convertView; 
    } 

    @Override 
    public EachRow getItem(int position) { 
     // TODO Auto-generated method stub 
     return list.get(position); 
    } 
    public EachRow getItem2(int position) { 
     // TODO Auto-generated method stub 
     return list2.get(position); 
    } 

    private class ViewHolder 
    { 
     TextView textView; 
     ImageView image; 
     EditText quant; 


    } 
} 

private class EachRow 
{ 
    String text; 
    boolean checkBool; 
} 

我試過addTextChangedListener,但是當我使用的setText它給了我一個錯誤。那麼問題是什麼?

在此先感謝!

回答

0

我設法弄清楚如何讓它工作。 holder.quant.gettext()。toString不起作用,即使我點擊其中一行的textview,也無法記錄textview的內容。持有人以某種方式阻止它,我會一直記錄相同的TextView ...

所以,我需要創建一個新的方法:

public String getTextOfSelectedRow(int position) { 
         return list.get(position).text; 
     } 

列表中包含行,爲.text來源於方法:

private class EachRow 
    { 
     String text; 
     boolean checkBool; 
    } 

和保持器的定義:

public View getView(final int position, View convertView, ViewGroup parent) { 
      // TODO Auto-generated method stub 

      if(convertView==null) 
      { 
       convertView=inflat.inflate(R.layout.row_checkox, null); 
       holder=new ViewHolder(); 
       holder.textView=(TextView)convertView.findViewById(R.id.textView111); 
       holder.image=(ImageView)convertView.findViewById(R.id.imageView111); 

       holder.quant = (EditText)convertView.findViewById(R.id.quantity); 
      holder.button = (Button)convertView.findViewById(R.id.dialogQuan); 
       holder.image.setOnClickListener(CustomList.this); 
       convertView.setTag(holder); 
      } 
      holder=(ViewHolder) convertView.getTag(); 



      holder.button.setOnClickListener(new View.OnClickListener() {.... 

在此之後的onClick方法所以:

kmultip = getTextOfSelectedRow(position).toString(); 

所以,我可以參考holder.quant或holder.textview或任何連續的東西。我仍然不知道爲什麼簡單的holder.quant.getText()。toString不起作用,但是通過這種方式,所以引用行的位置將使其工作。

0

讓我明白這一點。如果你點擊TextView,你想記錄EditText的內容?

爲什麼沒有在TextView的聽者的onclick方法只要致電:

Log.d("EditText Content", holder.quant.getText().toString()); 

你並不需要一個OnTextChangeListener用於簡單地抓在EDITTEXT內容。

+0

Ajcodez,這就是發生在這裏,我不明白你。它在最後一行 – 2012-03-01 20:08:38

+0

問題是當我修改模擬器中的內容並記錄它時,我得到的數據相同 – 2012-03-01 20:15:14

+0

當你改變EditText中的文本並點擊記錄它時,你會得到相同的結果。也許你的持有人有問題? – AJcodez 2012-03-02 05:05:04

0

first - 只有當它是新視圖時,您才用內容填充視圖。這僅適用於您將所有列表內容都設爲靜態的情況。你確定?

秒 - 你沒有使用ArrayAdapter的功能。看here