以下朋友以下是我的代码
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
上撰写文字,所以我知道如何解决它?
答案 0 :(得分:1)
我解决了这个问题。
我只是在getview
方法的代码中进行了一行更改,我只添加了一行
viewHolder.scores.setId(position);
因为在onTextchangelistner
我正在使用getId
访问位置,但在getview
我没有setid
setId(position)
,所以这就是ListView
的原因}特定EditText
项目ID未设置,我无法更改文本。
所以我只是通过添加上面的行进行更改,它对我来说很好。