gpt4 book ai didi

android - 在 ListView 中更改 TextView 的文本时出现问题

转载 作者:行者123 更新时间:2023-11-29 16:04:00 25 4
gpt4 key购买 nike

我是这个 android 开发的新手,在 listview 中设置 textview 的文本时遇到问题。

问题:

Activity 1 中的 ListView

======================
型号
====================
颜色
====================
风格
====================
制作
====================

当我在 ListView 中单击“模型” View 时。下面显示

Activity 2 中的 ListView

======================
丰田
====================
捷豹
====================
本田
====================
斯布鲁
====================

Activity 1 的结果

======================
型号
====================
颜色 -------------- 丰田
====================
风格
====================
制作
====================

我在找什么:

======================
型号 -------------- 丰田
====================
颜色
====================
风格
====================
制作
====================

这里是初始 Activity 。当我在 ListView 中单击“模型”时,将显示另一个 Activity 以及汽车模型列表。 (使用 startactivtiyforresult() 调用新 Activity )

在第二个 Activity 中,当我单击任何模型名称时,它会关闭当前 Activity 并将所选模型名称传递给第一个 Activity 。此模型名称在 OnActivityResult 方法中设置为 TextView

我的自定义适配器

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"));
}
}

fragment 的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;
}

第二个 Activity

@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;

}

希望这个解释对您有所帮助。请帮助我。

最佳答案

尝试从适配器外部直接更改列表项内的 TextView 的文本是不可靠的。一方面, View 将不匹配适配器的支持数据。此外,您的适配器不会尝试清除辅助 TextView 的任何现有内容,因此当您的 ListView 回收其 View 时,您将遇到很多像这样的异常问题。

因此删除所有引用 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;
}

关于android - 在 ListView 中更改 TextView 的文本时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21121063/

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