Android ListView With 2 TextViews Per Item

折月煮酒 提交于 2019-12-07 15:57:21

问题


All the examples I have seen on the net contain only 1 TextView per item and they load data from an array. I don't understand how do I specify which data goes where. For example my item layout looks like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+id/receiver"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

How do I save data in the array so that it could be used in ListView with this layout?


回答1:


Here is the custom list adapter:

private class ListAdapter extends ArrayAdapter<RSSItem> {

        private List<Item> items;

        public ListAdapter(Context context, int textViewResourceId, List<Item> items) {
            super(context, textViewResourceId, items);
            this.items = items;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View v = convertView;

            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.list_item, null);
            }

            Item item = items.get(position);

            if (item != null) {             
                TextView title = (TextView) v.findViewById(R.id.tvTitle);
                TextView description = (TextView) v.findViewById(R.id.tvDescription);
                if (title != null) {
                    title.setText(item.getTitle());
                }
                if (description != null) {
                    description.setText(item.toString());
                }
            }

            return v;
        }
    }

this is an Item class, witch will holds your data:

public class Item {
    private String title = null;
    private String description = null;

    RSSItem() {
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getTitle() {
        return title;
    }

    public String getData() {
        return data;
    }
}

...and here is a class with which you can take all list data in some kind of array:

public class ListData {
    private int itemcount = 0;
    private List<Item> itemlist;

    ListData () {
        itemlist = new Vector<Item>();
    }

    int addItem(Item item) {
        itemlist.add(item);
        itemcount++;
        return itemcount;
    }

    Item getItem(int location) {
        return itemlist.get(location);
    }

    List<Item> getAllItems() {
        return itemlist;
    }

    int getItemCount() {
        return itemcount;
    }
}

In your main class (Activity) you should fill up properly your data.




回答2:


With Adapter you feed the listview with data. And when you feed in getView() method you add data to these TextViews. There thousands of examples on the net...

Here is one example (Improvements on these code can be done, ofcourse)

   @Override
    public View getView(int position, View convertView, ViewGroup parent) throws NullPointerException {
        ViewHold viewHold;
      if (convertView == null) 
      {
       LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
       convertView = vi.inflate(R.layout.temp2, null);
       viewHold = new ViewHold();           
       viewHold.go = (Button)convertView.findViewById(R.id.gotherefav);   
       viewHold.remove = (Button)convertView.findViewById(R.id.removefav);

       convertView.setTag(viewHold);
      }
      else
      {
          viewHold=(ViewHold)convertView.getTag();
      }


  String t = items.get(position);
  if (t != null) {

            TextView itemText = (TextView) convertView.findViewById(R.id.item); // here you add data from arrayLists   
         TextView itemText2 = (TextView) convertView.findViewById(R.id.item2);
itemText2.setText(t);

            itemText.setText(t);
            itemText.invalidate();


           }
  viewHold.go.setVisibility(View.GONE);
  viewHold.remove.setVisibility(View.GONE);


  return convertView;

    }
}


来源:https://stackoverflow.com/questions/6793999/android-listview-with-2-textviews-per-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!