我是这个Android开发的新手,在listview中设置textview的文本时遇到了问题。
问题:
====================
型号
====================
颜色
====================
风格
====================
制作
====================
当我点击listview中的“模型”视图时。以下显示
====================
丰田
====================
美洲虎
====================
本田
====================
Suburu
====================
====================
型号
====================
颜色--------------丰田
====================
风格
====================
制作
====================
====================
型号--------------丰田
====================
颜色
====================
风格
====================
制作
====================
这是最初的活动。当我在listview中单击“Model”时,将显示另一个活动,其中包含汽车模型列表。 (使用startactivtiyforresult()调用新活动)
在第二个活动中,当我点击任何模型名称时,它会关闭此当前活动并将选定的模型名称传递给第一个活动。此模型名称设置为OnActivityResult方法
中的textview我的CustomAdapter
class CustomAdapter extends ArrayAdapter<String> {
Context context;
String[] titles;
public CustomAdapter(Context context, String[] titles) {
super(context, R.layout.dashboard_row_layout, R.id.row_textview, titles);
this.context = context;
this.titles = titles;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.dashboard_row_layout, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
}
//((TextView)row.findViewById(R.id.row_textview)).setText(titles[position]);
else
{
holder = (ViewHolder)row.getTag();
}
holder.textview.setText(titles[position].toString());
return row;
}
}
class ViewHolder
{
TextView textview;
ViewHolder(View v)
{
textview = (TextView) v.findViewById(R.id.row_textview);
}
}
OnActivityResultMethod
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode)
{
case Activity.RESULT_OK:
//selectedModel = data.getStringExtra("response");
setDateTextView.setText(data.getStringExtra("response"));
}
}
片段的OnCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_vehicle_information, container, false);
SeparatedListAdapter adapter = new SeparatedListAdapter(this.getActivity());
adapter.addSection("Plate Information", new CustomAdapter(this.getActivity(),getResources().getStringArray(R.array.plateInformationArrayList)));
adapter.addSection("Vehicle Information", new CustomAdapter(this.getActivity(),getResources().getStringArray(R.array.vehicleInformationArrayList)));
((ListView)rootView.findViewById(R.id.veh_information_listView)).setAdapter(adapter);
//Click Listener for listview with custom view
((ListView)rootView.findViewById(R.id.veh_information_listView)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(((TextView)view.findViewById(R.id.row_textview)).getText().toString().equalsIgnoreCase("Expiration")){
DateDialog dialog = new DateDialog(getActivity(), (TextView)view.findViewById(R.id.statename_textView));
dialog.returnDateDialog().show();
}
else if(((TextView)view.findViewById(R.id.row_textview)).getText().toString().equalsIgnoreCase("Model")){
setDateTextView = (TextView)view.findViewById(R.id.statename_textView);
startActivityForResult(new Intent(getActivity(), VehicleInformationDetails.class), 1);
}
}
});
return rootView;
}
SecondActivity
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_searchable_listview, container, false);
ListView listview = (ListView)rootView.findViewById(R.id.searchable_listView);
MyAdapter adapter = new MyAdapter(context, array);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView textView = (TextView)view.findViewById(R.id.list_item_title);
Intent returnIntent = new Intent();
returnIntent.putExtra("response", textView.getText().toString());
getActivity().setResult(Activity.RESULT_OK, returnIntent);
getActivity().finish();
}
});
SetActionBarTitle(this.actionBarTitle);
return rootView;
}
希望这个解释很有帮助。请帮帮我。
答案 0 :(得分:4)
尝试直接更改来自适配器外部的列表项内的文本视图的文本是不可靠的。首先,视图将不匹配适配器的后备数据。此外,您的适配器不会尝试清除辅助文本视图的任何现有内容,因此您将遇到很多不寻常的问题,因为列表视图会回收其视图。
所以摆脱引用setDateTextView
的所有行。相反,您需要一个自定义类来表示列表项的所有可能数据,如下所示:
public class MyListItem {
public String title;
public String state;
public MyListItem(String title){
this(title, "");
}
public MyListItem(String title, String state){
this.title = title;
this.state = state;
}
}
然后将其用作自定义适配器的对象类型:
class CustomAdapter extends ArrayAdapter<MyListItem> {
public CustomAdapter(Context context, MyListItem[] listItems) {
super(context, R.layout.dashboard_row_layout, listItems);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder;
if(row==null)
{
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.dashboard_row_layout, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
}
else
{
holder = (ViewHolder)row.getTag();
}
holder.titleTextview.setText(getItem(position).title);
holder.stateTextview.setText(getItem(position).state);
return row;
}
}
class ViewHolder
{
TextView titleTextview;
TextView stateTextview;
ViewHolder(View v)
{
titleTextview= (TextView) v.findViewById(R.id.row_textview);
stateTextview= (TextView) v.findViewById(R.id.statename_textView);
}
}
最后,在onActivityResult()
中,告诉适配器更改相应行的内容数组的值。您必须将这些变量设置为成员变量,但请执行以下操作:
switch (resultCode)
{
case Activity.RESULT_OK:
mListAdapter.getItem(mModelItemPosition).state = data.getStringExtra("response");
mListAdapter.notifyDataSetChanged();
break;
}