gpt4 book ai didi

android - 无法更改listview android中的Edittext值

转载 作者:行者123 更新时间:2023-11-30 03:11:33 24 4
gpt4 key购买 nike

Hello Friends 下面是我的代码

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>

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

最佳答案

我解决了这个问题。

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

viewHolder.scores.setId(position);

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

所以我刚刚通过添加上面的行进行了更改,它对我来说工作正常。

关于android - 无法更改listview android中的Edittext值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20865464/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com