简体   繁体   English

更改列表视图内的文本视图文本时出现的问题

[英]Issue when changing the text of textview inside listview

I'm new to this android development and having issues while setting the text of textview inside listview. 我是这个Android开发的新手,在设置listview内的textview文本时遇到问题。

Issue: 问题:

Listview in activity 1 活动1中的Listview

==================== ====================
Model 模型
==================== ====================
Color 颜色
==================== ====================
Style 样式
==================== ====================
Make 使
==================== ====================

When I click on "Model" view in listview. 当我在列表视图中单击“模型”视图时。 the below shows up 下面显示

Listview in activity 2 活动2中的Listview

==================== ====================
Toyota 丰田汽车
==================== ====================
Jaguar 美洲虎
==================== ====================
Honda 本田
==================== ====================
Suburu 苏布鲁
==================== ====================

Result In activity 1 结果在活动1中

==================== ====================
Model 模型
==================== ====================
Color -------------- Toyota 颜色--------------丰田
==================== ====================
Style 样式
==================== ====================
Make 使
==================== ====================

What I was looking for: 我在寻找什么:

==================== ====================
Model -------------- Toyota 型号--------------丰田
==================== ====================
Color 颜色
==================== ====================
Style 样式
==================== ====================
Make 使
==================== ====================

Here the initial activity. 这里是最初的活动。 when I click on "Model" inside listview another activity will be shown with list of car models. 当我在列表视图中单击“模型”时,将显示另一个活动以及汽车模型列表。 (new activity is called using startactivtiyforresult()) (使用startactivtiyforresult()调用新活动)

In the second activity when I click on any model name it closes this current activity and passes the selected model name to first activity. 在第二个活动中,当我单击任何模型名称时,它将关闭当前活动并将所选模型名称传递给第一个活动。 this model name is set to the textview in OnActivityResult method 该模型名称在OnActivityResult方法中设置为textview

My CustomAdapter 我的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 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 of the 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;
    }

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;

}

Hope this explanation is helpful. 希望这个解释对您有所帮助。 Please help me. 请帮我。

It is not reliable to try to directly change the text of a text view that's inside a list item from outside of the adapter. 尝试直接从适配器外部更改列表项内部的文本视图的文本是不可靠的。 For one thing, the view will not be matching the backing data of your adapter. 一方面,视图将与适配器的支持数据不匹配。 Also, your adapter does not attempt to clear any existing contents of the secondary text view, so you are going to run into a lot of unusual problems like this as your list view recycles its views. 另外,您的适配器不会尝试清除辅助文本视图的任何现有内容,因此当列表视图回收其视图时,您将遇到许多诸如此类的异常问题。

So get rid of all the lines referring to setDateTextView . 因此,请删除所有引用setDateTextView的行。 Instead, you need a custom class that represents all of the possible data for a list item, like this: 相反,您需要一个自定义类来表示列表项的所有可能数据,如下所示:

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

Then use this as the object type of your custom adapter: 然后将其用作自定义适配器的对象类型:

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

Finally, in your onActivityResult() , tell the adapter to change the value of the content array for the appropriate row. 最后,在onActivityResult() ,告诉适配器更改相应行的内容数组的值。 You'll have to set these variables up as member variables, but do it something like this: 您必须将这些变量设置为成员变量,但需要这样做:

switch (resultCode)
{
    case Activity.RESULT_OK:
        mListAdapter.getItem(mModelItemPosition).state = data.getStringExtra("response");
        mListAdapter.notifyDataSetChanged();
        break;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM