我正在实现一个自定义ListView,它有一些文本字段(用于标题),下面是一个textview,当点击时,它会打开一个警告对话框,显示选项列表,当用户选择任何选项时,TextView文本设置为那个选择。到目前为止一切都运行良好但是当我滚动列表视图时,在该textview中选择的值只是更改为其他一些值。请帮我解决这个问题,如何使这个观点正确。为此,我将在下面显示两张图片
第一张图片,其中我选择了第一行文本作为一个,而另一行保持不变。
这是第二张图片,显示在向下滚动后向后滚动第一行textview刚刚更改,另一张图片用文本填充,即使我没有为它们选择任何值。
这是我的MainActivity Code
public class MainActivity extends Activity{
Controller control ;
ArrayList<MyDetails> detail;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test_speed);
detail=(ArrayList<MyDetails>) control.Table_Details.Data();
ListView listview = (ListView) findViewById(R.id.list_layout);
MyBaseAdapter adapter = new MyBaseAdapter(this, detail);
listview.setAdapter(adapter);
}
public void showDialog(final TextView option,final CharSequence[] items){
AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
builder.setTitle("Make your selection");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
// Do something with the selection
option.setText(items[item]);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
这是我的适配器代码
public class MyBaseAdapter extends BaseAdapter{
ArrayList<MyDetails> list;
Context context ;
public MyBaseAdapter(Context con, ArrayList<MyDetails> detail) {
this.context = con;
this.list =detail ;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view == null)
{
view = inflater.inflate(R.layout.adapter_layout, null);
}
TextView title= (TextView) view.findViewById(R.id.title);
final TextView data = (TextView) view.findViewById(R.id.data);
title.setText(list.get(position).title());
final CharSequence[] items = {
"One", "tow", "three", "four", "six"
};
data.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
((MainActivity) context).showDialog(data,items);
}
});
return view;
}
}
任何帮助都将受到高度赞赏。提前谢谢。
答案 0 :(得分:0)
每次在TextView
中呼叫getView()
时,都应通过查找ID Adapter
来解决您的问题。我可能错了,但我认为你应该尝试在浏览布局项目的同时获取视图,并且(在之后)将文本设置为它。
此外,使用ViewHolder
可能会提高性能,它只允许调用findViewById
一次,并且在滚动列表时每次调用getView()
时都要避免调用它。<登记/>
因此,请尝试使用ViewHolder
,如下所示:
public class MyBaseAdapter extends BaseAdapter {
// init the viewholder
protected class ViewHolder() {
TextView title, data;
}
@Override
public View getView(int position, View view, ViewGroup arg2) {
// create the var viewholder
ViewHolder holder;
// init the row once if it's null
if(view == null) {
// inflate the row and find views only the first time
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.adapter_layout, null);
// create the viewholder
holder = new ViewHolder();
// attach views with viewholder
holder.title = (TextView) view.findViewById(R.id.title);
holder.data = (TextView) view.findViewById(R.id.data);
// set a tag to retrieve it later
view.setTag(holder);
} else {
// reuse the row by getting the previous tag
holder = (ViewHolder) view.getTag();
}
// set the text to the views attached
title.setText(list.get(position).title());
...
return view;
}
}
此外,正如 Tukajo 在问题下方的评论中所说,最好将ItemClickListener
设置为整行,而不是在其中设置OnClickListener
。<登记/>
因此,在活动中setAdapter()
之后,请按以下步骤调用Dialog
:
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Show the dialog...
// (Not sure but you can try to retrieve the TextView like this:)
showDialog( ((TextView) v.findViewById(R.id.data)) , items);
// Then 'items' array needs to be in the Activity, outside the Adapter
}
});
希望这能解决您的问题。