2010-12-05 27 views
0

我在ListActivity中遇到EditText字段存在問題。列表中的EditText未按照應有的方式工作

該代碼符合所有權利,但功能很奇怪,在第一個字段中輸入並隱藏鍵盤後,文本出現在另一個編輯字段中。

幫助我與我的邏輯問題

package com.example.helloandroid; 

import android.app.ListActivity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.EditText; 
import android.widget.TextView; 

public class AddComp extends ListActivity { 


static final int DATE_DIALOG_ID = 0; 

     private class EfficientAdapter extends BaseAdapter { 
      private LayoutInflater mInflater; 
      private String[] attitude_names; 
      private String[] attitude_values; 

      public EfficientAdapter(Context context) { 
       mInflater = LayoutInflater.from(context); 
       attitude_names = context.getResources().getStringArray(R.array.COMP_ATTITUDE_NAME); 
       attitude_values = new String[attitude_names.length]; 
      } 

      public Object getItem(int position) { 
       return position; 
      } 

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

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

       if (convertView == null) { 
        convertView = mInflater.inflate(R.layout.addcomp_attitude_row, null); 

        holder = new ViewHolder(); 
        holder.Attitude_Name = (TextView) convertView.findViewById(R.id.addcomp_att_name); 
        holder.Attitude_Value = (EditText) convertView.findViewById(R.id.addcomp_att_value); 

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

       holder.Attitude_Name.setText(attitude_names[position]); 
       holder.Attitude_Value.setHint(attitude_names[position]); 
       attitude_values[position] = holder.Attitude_Value.getText().toString(); 



       return convertView; 
      } 

      class ViewHolder { 
       TextView Attitude_Name; 
       EditText Attitude_Value; 
      } 

      @Override 
      public int getCount() { 
       return attitude_names.length; 
      } 
     } 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setListAdapter(new EfficientAdapter(this)); 

     setContentView(R.layout.addcomp); 
    } 
} 
+3

問題標題很有趣:)軟件做我們要做的事情,而不是我們希望它做的事情:) – khachik 2010-12-05 10:41:58

回答

3

問題通過在清單中添加Entry和使用TextWatcher解決了(這是必需的,因爲一個列表行的視圖被多次內部調用,意味着對於500個列表條目,程序只使用row.view-class的所有實例以提高效率) 因此,需要使用文本觀察器將更改的數據保存在一個額外的數據結構中,以用於實例陣列。

 private class EfficientAdapter extends BaseAdapter { 
      private LayoutInflater mInflater; 
      private String[] attitude_names; 
      public String[] attitude_values; 
      private String name; 

      public EfficientAdapter(Context context) { 
       mInflater = LayoutInflater.from(context); 
       attitude_names = context.getResources().getStringArray(R.array.COMP_ATTITUDE_NAME); 
       attitude_values = new String[attitude_names.length]; 
      } 

      public Object getItem(int position) { 
       return position; 
      } 

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

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

       if (convertView == null) { 
        convertView = mInflater.inflate(R.layout.addcomp_attitude_row, null); 

        holder = new ViewHolder(); 
        holder.Attitude_Name = (TextView) convertView.findViewById(R.id.addcomp_att_name); 
        holder.Attitude_Value = (EditText) convertView.findViewById(R.id.addcomp_att_value); 
        holder.Attitude_Value.addTextChangedListener(new TextWatcher() 
         { 
          public void afterTextChanged(Editable edt) 
          { 
           attitude_values[holder.ref] = edt.toString(); 
          } 

          public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {} 

          public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
           //attitude_values[ref] = Attitude_Value.getText().toString(); 
          } 
         }); 

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


       holder.ref=position; 
       holder.Attitude_Name.setText(attitude_names[position]); 
       holder.Attitude_Value.setHint(attitude_names[position]); 
       holder.Attitude_Value.setText(attitude_values[position]); 




       return convertView; 
      } 

      class ViewHolder { 
       TextView Attitude_Name; 
       EditText Attitude_Value; 
       int ref; 



      } 

      @Override 
      public int getCount() { 
       return attitude_names.length; 
      } 
     } 
0

我找到了原因怪聚焦 一個行爲已爲增值android:windowSoftInputMode="adjustPan"在項目清單 的活動,但是,從另一方面問題通常不止一個edidfield的值被改變現在還沒有解決

1

這將幫助你

``

private class EfficientAdapter extends BaseAdapter { 
      private LayoutInflater mInflater; 
      private String[] attitude_names; 
      public String[] attitude_values; 
      private String name; 
    public static HashMap<Integer,String> myList=new HashMap<Integer,String>(); 

      public EfficientAdapter(Context context) { 
       mInflater = LayoutInflater.from(context); 
       attitude_names = context.getResources().getStringArray(R.array.COMP_ATTITUDE_NAME); 
       attitude_values = new String[attitude_names.length]; 
      } 
    // initialize myList 
    for(int i=0;i<attitude_names.length;i++) 
    { 
     myList.put(i,""); 
    } 


      public Object getItem(int position) { 
       return position; 
      } 

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

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

       if (convertView == null) { 
        convertView = mInflater.inflate(R.layout.addcomp_attitude_row, null); 

        holder = new ViewHolder(); 
        holder.Attitude_Name = (TextView) convertView.findViewById(R.id.addcomp_att_name); 
        holder.Attitude_Value = (EditText) convertView.findViewById(R.id.addcomp_att_value); 
        holder.Attitude_Value.addTextChangedListener(new TextWatcher() 
         { 
          public void afterTextChanged(Editable edt) 
          { 
           myList.put(pos,s.toString.trim()); 
           attitude_values[holder.ref] = edt.toString(); 
          } 

          public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {} 

          public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
           //attitude_values[ref] = Attitude_Value.getText().toString(); 
          } 
         }); 

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


       holder.ref=position; 
       holder.Attitude_Name.setText(attitude_names[position]); 
       holder.Attitude_Value.setHint(attitude_names[position]); 
       holder.Attitude_Value.setText(myList.get(position)); 




       return convertView; 
      } 

      class ViewHolder { 
       TextView Attitude_Name; 
       EditText Attitude_Value; 
       int ref; 



      } 

      @Override 
      public int getCount() { 
       return attitude_names.length; 
      } 
     } 

在這裏,我已經包括一個HashMap對象,這將繼續對什麼的EditText包含value.And當您滾動列表視圖的眼睛,這將是通過調用g來再次渲染etView方法。

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

相關問題