2012-02-17 81 views
10

我有EditText在其中的列表項,我不知道會有多少物品。當我在EditText中輸入一些文本時,出現問題,然後向下滾動ListView,在我再次向上滾動後,我的第一個EditText中沒有文本,或者其他EditText中有一些文本從ListViewAndroid:EditText在ListView中滾動時丟失內容?

我試過TextWatcher,並將數據保存到數組,但問題是ListView中返回的視圖位置並不總是正確的,所以我從數組中丟失了一些數據。

請幫忙。

這裏是我的代碼:

public class EfficientAdapter extends BaseAdapter { 

    private LayoutInflater mInflater; 
    public String[] Current; 
    ArrayList<String> MeterName, PreviousReading, Current_Reading; 
    JSONArray getArray_Meter_Reading; 

    public EfficientAdapter(Context context, JSONArray getArray_Meter_Reading) { 
     mInflater = LayoutInflater.from(context); 
     this.getArray_Meter_Reading = getArray_Meter_Reading; 
     MeterName = new ArrayList<String>(); 
     PreviousReading = new ArrayList<String>(); 
     for (int i = 0; i < getArray_Meter_Reading.length(); i++) { 
      try { 
       String Meter_Name = getArray_Meter_Reading.getJSONObject(i) 
         .getString("MeterName").toString(); 
       String previous_Meter_Reading = getArray_Meter_Reading 
         .getJSONObject(i).getString("PrevMeterReading") 
         .toString(); 
       MeterName.add(Meter_Name); 
       PreviousReading.add(previous_Meter_Reading); 

       // Meter[i]=MeterName.get(i); 
       // Previous[i]=PreviousReading.get(i); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 

    public int getCount() { 

     return getArray_Meter_Reading.length(); 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 

     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.meter_reading_list, null); 
      holder = new ViewHolder(); 
      holder.adp_MeterName = (TextView) convertView 
        .findViewById(R.id.txt_Meter_Name); 
      holder.adp_Previous = (TextView) convertView 
        .findViewById(R.id.txt_Previous); 
      holder.adp_Current = (EditText) convertView 
        .findViewById(R.id.ed_Current); 

      holder.adp_Current.addTextChangedListener(new TextWatcher() { 

       public void onTextChanged(CharSequence s, int start, 
         int before, int count) { 

       } 

       public void beforeTextChanged(CharSequence s, int start, 
         int count, int after) { 
        // TODO Auto-generated method stub 

       } 

       public void afterTextChanged(Editable s) { 

        Current[holder.ref] = s.toString(); 

       } 
      }); 
      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.ref = position; 
     holder.adp_MeterName.setText(MeterName.get(position)); 
     holder.adp_Previous.setText(PreviousReading.get(position)); 
     // holder.adp_Current.setHint(MeterName.get(position)); 

     // holder.adp_Current.setText(PreviousReading.get(position)); 

     return convertView; 
    } 

    class ViewHolder { 
     TextView adp_MeterName, adp_Previous; 
     EditText adp_Current; 
     int ref; 

    } 

} 

回答

8

試試這個:

public class EfficientAdapter extends BaseAdapter { 

private LayoutInflater mInflater; 
public String[] Current; 
ArrayList<String> MeterName, PreviousReading, Current_Reading; 
JSONArray getArray_Meter_Reading; 
public static HashMap<Integer,String> myList=new HashMap<Integer,String>(); 

public EfficientAdapter(Context context, JSONArray getArray_Meter_Reading) { 
    mInflater = LayoutInflater.from(context); 
    this.getArray_Meter_Reading = getArray_Meter_Reading; 
    MeterName = new ArrayList<String>(); 
    PreviousReading = new ArrayList<String>(); 

    for (int i = 0; i < getArray_Meter_Reading.length(); i++) { 
     try { 
      String Meter_Name = getArray_Meter_Reading.getJSONObject(i) 
        .getString("MeterName").toString(); 
      String previous_Meter_Reading = getArray_Meter_Reading 
        .getJSONObject(i).getString("PrevMeterReading") 
        .toString(); 
      MeterName.add(Meter_Name); 
      PreviousReading.add(previous_Meter_Reading); 

      // Meter[i]=MeterName.get(i); 
      // Previous[i]=PreviousReading.get(i); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    // initialize myList 
    for(int i=0;i<JSON_ARRAY_LENGTH;i++) 
    { 
     myList.put(i,""); 
    } 
} 

public int getCount() { 

    return getArray_Meter_Reading.length(); 
} 

public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    final ViewHolder holder; 
    final int pos=position; 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.meter_reading_list, null); 
     holder = new ViewHolder(); 
     holder.adp_MeterName = (TextView) convertView 
       .findViewById(R.id.txt_Meter_Name); 
     holder.adp_Previous = (TextView) convertView 
       .findViewById(R.id.txt_Previous); 
     holder.adp_Current = (EditText) convertView 
       .findViewById(R.id.ed_Current); 


     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 
    holder.adp_Current.addTextChangedListener(new TextWatcher() { 

      public void onTextChanged(CharSequence s, int start, 
        int before, int count) { 

      } 

      public void beforeTextChanged(CharSequence s, int start, 
        int count, int after) { 
       // TODO Auto-generated method stub 

      } 

      public void afterTextChanged(Editable s) { 

       Current[holder.ref] = s.toString(); 
       myList.put(pos,s.toString.trim()); 
      } 
     }); 
    holder.ref = position; 
    holder.adp_MeterName.setText(MeterName.get(position)); 
    holder.adp_Previous.setText(PreviousReading.get(position)); 
    // holder.adp_Current.setHint(MeterName.get(position)); 

    // holder.adp_Current.setText(PreviousReading.get(position)); 

    holder.adp_Current.setText(myList.get(position)); 

    return convertView; 
} 

class ViewHolder { 
    TextView adp_MeterName, adp_Previous; 
    EditText adp_Current; 
    int ref; 

} 

} 

在這裏,我已經包括一個HashMap對象,將密切關注,如果含有的EditText值或not.And當您滾動通過調用它的getView方法以及與每個edittext關聯的文本,它將被再次呈現。

在這段代碼中,當你第一次加載列表視圖時,所有的edittext都將沒有文本。只要你輸入了一些文本,它會在myList中註明。所以當你再次渲染列表時,你的文本將被阻止。

還有一件事,你應該在外部實現textwatcher if(convertView == null).. else .. block.That更好的做法!

+0

它是什麼不同? – 2012-02-17 13:00:26

+0

你看過我建議的代碼嗎?它首先通過代碼包含一個HashMap對象there.go! – Hiral 2012-02-17 13:01:24

+1

我面對的是什麼問題是,當在第一個edittext中鍵入一些值,我向下滾動到列表中,顯示第一個edittext中的值是一些其他edittext框。可以爲最近三天面臨這個問題的即時通訊提供解決方案。 – 2012-02-17 13:03:30

9

我和我正在做的項目有完全相同的問題。我發現的所有解決方案都使用textChangeListener提及,但是由於列表的性質以及在每個行視圖中使用EditText,這是非常低效的。我採用了另一種方法,使用EditText.setOnFocusChangeListener()

我將此附加到getView方法中的每個EditText。這種效果很好的原因是,當用戶切換到不同的編輯文本或滾動時會調用它,所以當前行離開屏幕導致EditText失去焦點。

在onFocusChange(查看V,布爾hasFocus)聽衆中我簡單地說:

if (!hasFocus) { 
       EditText et = (EditText) v.findViewById(R.id.groupAmount);     
       data.get(position).put("dueAmount", et.getText().toString().trim()); 
      } 

在我的例子,我存儲輸入值到我的數據用於填充每個getView調用的意見,但是這應該仍然顯示如何訪問用戶輸入的數據。只要確保您嘗試使用此存儲的數據並嘗試在相應的位置填充editText。

希望這會有所幫助。

發佈我的全適配器這裏參考

public class memberlistadapter extends BaseAdapter { 
private LayoutInflater mInflater; 
public ArrayList<Hashtable<String, String>> data; 
ViewHolder viewHolder; 
public static HashMap<Integer,String> myList=new HashMap<Integer,String>(); 

public memberlistadapter(Context context) { 
    mInflater = LayoutInflater.from(context); 
} 

public memberlistadapter(Activity activity, ArrayList<Hashtable<String, String>> objects) { 
    super(); 
    mInflater = activity.getLayoutInflater(); 
    //this.activity = activity; 
    this.data = objects; 
    Log.d("data", data.toString()); 
} 

public void clear() { 
    this.data.clear(); 
    viewHolder = null; 
} 

public void setData(ArrayList<Hashtable<String, String>> data) { 
    this.data = data; 
} 

public int getCount() { 
    return data.size(); 
} 

public Object getItem(int item) { 
    return data.get(item); 
} 

public long getItemId(int position) { 
    return position; 
} 

public View getView(final int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.member_amount_child, null); 
     viewHolder = new ViewHolder(); 

     viewHolder.nameText = (TextView) convertView.findViewById(R.id.grp_amt_child); 
     viewHolder.amountText = (EditText) convertView.findViewById(R.id.groupAmount); 
     viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.memberCheckNotif); 

     convertView.setTag(viewHolder); 

    } else { 
     viewHolder = (ViewHolder) convertView.getTag(); 
    } 
    //Log.d("data at "+position, data.get(position).toString()); 
    String amt = data.get(position).get("dueAmount"); 
    String name = data.get(position).get("name"); 
    String check = data.get(position).get("reminder"); 

    viewHolder.nameText.setText(name); 
    try { 
    if (amt.length() > 0 && !amt.equalsIgnoreCase("0")) { 
     viewHolder.amountText.setText(amt); 
    } else { 
     viewHolder.amountText.setText(""); 
    } 
    } catch (Exception e) { 
     viewHolder.amountText.setText(""); 
    } 

    viewHolder.amountText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     public void onFocusChange(View v, boolean hasFocus) { 
      if (!hasFocus) { 
       EditText et = (EditText) v.findViewById(R.id.groupAmount); 
       data.get(position).put("dueAmount", et.getText().toString().trim()); 
      } 
     } 
    }); 

    try { 
    if (check.equalsIgnoreCase("true")) { 
     viewHolder.checkBox.setChecked(true); 
    } else { 
     viewHolder.checkBox.setChecked(false); 
    } 
    } catch (Exception e) { 
     viewHolder.checkBox.setChecked(false); 
    } 
    viewHolder.checkBox.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      if (((CheckBox) v).isChecked()) { 
       data.get(position).put("reminder", "True"); 
      } else { 
       data.get(position).put("reminder", "False"); 
      } 
     } 
    }); 
    if (position == 0) { 
     viewHolder.checkBox.setVisibility(View.GONE); 
     viewHolder.amountText.setVisibility(View.GONE); 
     viewHolder.nameText.setVisibility(View.GONE); 
    } else { 
     viewHolder.checkBox.setVisibility(View.VISIBLE); 
     viewHolder.amountText.setVisibility(View.VISIBLE); 
     viewHolder.nameText.setVisibility(View.VISIBLE); 
    } 

    return convertView; 
} 

static class ViewHolder { 
    TextView nameText; 
    EditText amountText; 
    CheckBox checkBox; 
} 
} 
+3

這並不能解決問題。如果您在「編輯文本」上放置了一些文本,然後滾動,則第一個編輯文本不會失去焦點,輸入會丟失。 – kiduxa 2013-06-09 19:05:06

+0

@kiduxa對EditText使用'addTextChangedListener'而不是'setOnClickListener'並將'vendorList.get(position).put(「preference」,et_preference.getText()。toString()。trim());'in ** afterTextChanged ** – 2016-02-17 13:23:58

相關問題