I have a custom adapter inheriting from ArrayAdapter<String> . Now I can calmly add my text to the textview , which is in item -e listview . I want to add another textview to the item . The first textview filled with data from one data array, and the second, respectively, from another. After reading a few articles, I realized that for this you can use a HashMap , but how exactly to use it, so that it textview my textview I did not understand. Sorry in advance if something is not clear

Adapter code:

 public class MyListAdapter extends ArrayAdapter<String> implements UndoAdapter { private final Context mContext; String items[] = {"item1", "item2", "item3", "item4"}; public MyListAdapter(Context context) { mContext = context; for (int i = 0; i<items.length; i++){ add(items[i]); } } @Override public View getView(final int position, final View convertView, final ViewGroup parent) { View view = convertView; if (view == null) { view = LayoutInflater.from(mContext).inflate(R.layout.list_row_dynamiclistview, parent, false); } ((TextView) view.findViewById(R.id.textview1)).setText(getItem(position)); // textview1, куда я могу устанавливать свои значения. ((TextView) view.findViewById(R.id.textView2)).setText("Пока что случайный текст");//textview, куда я хочу устанавливать значения return view; } @NonNull @Override public View getUndoView(final int position, final View convertView, @NonNull final ViewGroup parent) { View view = convertView; if (view == null) view = LayoutInflater.from(mContext).inflate(R.layout.undo_row, parent, false); return view; } @NonNull @Override public View getUndoClickView(@NonNull final View view) { return view.findViewById(R.id.undo_row_undobutton); } @Override public long getItemId(final int position) { return getItem(position).hashCode(); } @Override public boolean hasStableIds() { return true; } } 

Thank)

    3 answers 3

    Use the Pair class. It is for this purpose made - to store couple of values.

    Completely your code will look like this:

     public class MyListAdapter extends ArrayAdapter<Pair<String,String>> implements UndoAdapter { private final Context mContext; public MyListAdapter(Context context) { mContext = context; add(new Pair<String,String>("item1","sub1")); add(new Pair<String,String>("item2","sub2")); add(new Pair<String,String>("item3","sub3")); add(new Pair<String,String>("item4","sub4")); } @Override public View getView(final int position, final View convertView, final ViewGroup parent) { View view = convertView; if (view == null) { view = LayoutInflater.from(mContext).inflate(R.layout.list_row_dynamiclistview, parent, false); } ((TextView) view.findViewById(R.id.textview1)).setText(getItem(position).first); // textview1, куда я могу устанавливать свои значения. ((TextView) view.findViewById(R.id.textView2)).setText(getItem(position).second);//textview, куда я хочу устанавливать значения return view; } @NonNull @Override public View getUndoView(final int position, final View convertView, @NonNull final ViewGroup parent) { View view = convertView; if (view == null) view = LayoutInflater.from(mContext).inflate(R.layout.undo_row, parent, false); return view; } @NonNull @Override public View getUndoClickView(@NonNull final View view) { return view.findViewById(R.id.undo_row_undobutton); } @Override public long getItemId(final int position) { return getItem(position).hashCode(); } @Override public boolean hasStableIds() { return true; } } 

      You do not need to use HashMap , but a simple model class, which will contain 2 String type fields, of which you will fill your TextView .
      It looks like this:

       class MyClass { public String firstText; public String secondText; } .......................... List<MyClass> myClassList = new ArrayList<MyClass>(); //заполнение myClassList .......................... public class MyListAdapter extends ArrayAdapter<MyClass> implements UndoAdapter { .......................... } 

        You need to use SimpleAdapter , an example , or inherit from BaseAdapter , transfer what you want and display as you want.

        • The fact is that I use animated listview, which does not work very well with BaseAdapter and SimpleAdapter. - Igor Gorokhov