无法在listview android中更改Edittext值

时间:2014-01-01 04:10:13

标签: android listview android-edittext

以下朋友以下是我的代码

Main.Java

public class NewMain extends Activity {

ListView mListView;
DatabaseConnectionAPI mDatabaseConnectionAPI;
ArrayList<ParserCategory> mArrayList;
InteractiveListViewAdapter mInteractiveListViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mDatabaseConnectionAPI = new DatabaseConnectionAPI(
            getApplicationContext());
    try {
        mDatabaseConnectionAPI.createDataBase();
        mDatabaseConnectionAPI.openDataBase();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mArrayList = new ArrayList<ParserCategory>();
    mArrayList = mDatabaseConnectionAPI.getCategoryData();

    mListView = (ListView) findViewById(R.id.MyList);
    mInteractiveListViewAdapter=new InteractiveListViewAdapter(NewMain.this,R.layout.item,mArrayList);
    mListView.setAdapter(mInteractiveListViewAdapter);

}

public class ViewHolder {
    protected TextView text;
    protected EditText scores;
}

public class InteractiveListViewAdapter extends ArrayAdapter<ParserCategory>

{
    ArrayList<ParserCategory>mList;
    Activity context;

    public InteractiveListViewAdapter(Context context, int resource,ArrayList<ParserCategory> mArrayList) {
        super(context, R.layout.item,mArrayList);
        this.context=(Activity) context;
        mList=mArrayList;
    }

    @Override   
    public View getView(int position, View convertView, ViewGroup parent)
    {        
        View view = null;  
        if (convertView == null)
        {             
            LayoutInflater inflator = getLayoutInflater(); 

            view = inflator.inflate(R.layout.item, null); 

            final ViewHolder viewHolder = new ViewHolder(); 
            viewHolder.text = (TextView) view.findViewById(R.id.txt);   
            viewHolder.scores=(EditText) view.findViewById(R.id.ItemCaption);    
            viewHolder.scores.addTextChangedListener(new TextWatcher()
            {  
                public void onTextChanged(CharSequence s, int start, int before, int count) {      
                    ParserCategory element=(ParserCategory)viewHolder.scores.getTag();            
                    element.setCdesc(s.toString());        
                }        
                public void beforeTextChanged(CharSequence s, int start, int count,int after)
                {       
                }      
                public void afterTextChanged(Editable s)
                {                              
                }
            });         
            viewHolder.scores.setTag(mList.get(position));
            view.setTag(viewHolder);   
        } 
        else 
        {
            view = convertView; 
            ((ViewHolder) view.getTag()).scores.setTag(mList.get(position)); 
        }

        ViewHolder holder = (ViewHolder) view.getTag();    
        holder.text.setText(mList.get(position).getCname());    
        holder.scores.setText(mList.get(position).getCdesc());   
        return view;

    }
 }

 }

我的activity_main.xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
    android:id="@+id/MyList"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="@android:color/transparent"
    android:fadingEdge="none" >
   </ListView>

  </LinearLayout>

我的item.xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/txt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="dfd" />

<EditText
    android:id="@+id/ItemCaption"
    android:editable="true"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dip"
  >
</EditText>

当我运行上述代码时,我无法更改EditText中的ListView值。问题是,当我专注于listitem中的任何EditText时,我无法在EditText上撰写文字,所以我知道如何解决它?

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。

我只是在getview方法的代码中进行了一行更改,我只添加了一行

viewHolder.scores.setId(position);

因为在onTextchangelistner我正在使用getId访问位置,但在getview我没有setid setId(position),所以这就是ListView的原因}特定EditText项目ID未设置,我无法更改文本。

所以我只是通过添加上面的行进行更改,它对我来说很好。