Skip to content Skip to sidebar Skip to footer

2 Lines In A Listview Item

I would like to have two lines in every list view item. This is my code but, it's not working. public class nea extends ListActivity{ private List messages;

Solution 1:

In order to create a ListView with two lines of text on each item, I use a SimpleAdapter in the following way:

Create a List that will hold your data. Each Map entry will have a Key (First line) and a Value (Second line) of each one of the ListView Items.

List<Map<String, String>> data = newArrayList<Map<String, String>>();

For each pair we want to put in the ListView, create a HashMap and add it into the List declared before.

Map<String, String> datum = newHashMap<String, String>(2);
                datum.put("First Line", "First line of text");
                datum.put("Second Line","Second line of text");
                data.add(datum);

Note: if you want to add more than one row, then you need to create another object similar to datum above.

Then declare a SimpleAdapter. Note there the use of "First Line" and "Second Line" strings inside the constructor. This will tell the adapter how to use the strings of the Map declared before. Note too the use of the layout android.R.layout.simple_list_item_2 and the text id android.R.id.text1 and android.R.id.text2.

SimpleAdapter adapter = newSimpleAdapter(this, data,
                    android.R.layout.simple_list_item_2, 
                    new String[] {"First Line", "Second Line" }, 
                    newint[] {android.R.id.text1, android.R.id.text2 });

You then need to set adapter as the adapter of your ListView

ListViewlistView= (ListView) findViewById(R.id.theIdOfYourListView);
listView.setAdapter(adapter);

Solution 2:

I am just using inflating Twoline listitem xml layout.In the following example text1 is Header and text2 is subtitle.

publicclassCustomListAdapterextendsBaseAdapter {

    private String[] stringArray;
    private Context mContext;
    private LayoutInflater inflator;
    int checkbox;
    /**
     * 
     * @param context
     * @param stringArray
     */publicCustomListAdapter(Context  context, String[] stringArray) 
    {
        this.mContext=context;
        this.stringArray=stringArray;
        this.inflator= (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @OverridepublicintgetCount()
    {
        return stringArray.length;
    }

    @Overridepublic Object getItem(int position)
    {
        return position;
    }

    @OverridepubliclonggetItemId(int position)
    {
        return position;
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent)
    {

        final MainListHolder mHolder;
        Viewv= convertView;
        if (convertView == null)
        {
            mHolder = newMainListHolder();
            v = inflator.inflate(android.R.layout.two_line_list_item, null);
            mHolder.txt1= (TextView) v.findViewById(android.R.id.text1);
            mHolder.txt2= (TextView) v.findViewById(android.R.id.text2);
            v.setTag(mHolder);
        } 
        else
        {
            mHolder = (MainListHolder) v.getTag();
        }
        mHolder.txt1.setText(stringArray[position]);
        mHolder.txt2.setText(stringArray[position]);

        /*mHolder.txt.setTextSize(12);
        mHolder.txt.setTextColor(Color.YELLOW);
        mHolder.txt.setPadding(5, 5, 5, 5);*///mHolder.image.setImageResource(R.drawable.icon);return v;
    }
    classMainListHolder 
    {
        private TextView txt1;
        private TextView txt2;

    }



}

And also there is a Example on Twoline listitem

Solution 3:

If you can take all titles or line 1 in one array and all details or line 2 in another array, You can do like this is in simple way using SimpleAdapter

String[] titleArray = {"title 1", "title 2", "title 3", "title 4"};
String[] detailArray = {"detail 1", "detail 2", "detail 3", "detail 4"};

List<Map<String, String>> listArray = newArrayList<>();

for(int i=0; i< titleArray.length; i++)
{
    Map<String, String> listItem = newHashMap<>();
    listItem.put("titleKey", titleArray[i]);
    listItem.put("detailKey", detailArray[i]);
    listArray.add(listItem);
}

SimpleAdapter simpleAdapter = newSimpleAdapter(this, listArray,
        android.R.layout.simple_list_item_2,
        newString[] {"titleKey", "detailKey" },
        new int[] {android.R.id.text1, android.R.id.text2 });

ListView listView = findViewById(R.id.listId);
listView.setAdapter(simpleAdapter);

Solution 4:

The lines:

TextViewtt= (TextView) findViewById(R.id.TextView01);
TextViewbt= (TextView) findViewById(R.id.TextView02);

are meaningless because there isn't a list item view for msg at that point, and you are not finding the text views by ID within a list item view, but rather the main view.

When your app first starts, you have an empty ListView. You then parse messages as part of onCreate (which is not a good idea, by the way, because time-consuming processes should not be performed in the UI thread) and then proceed to attempt to find non-existent text views by the IDs R.id.TextView01 and R.id.TextView02 in the main view.

Keep in mind that you will need two layout XML files: one for the layout of the list activity and another for the layout of each list item. Also keep in mind that findViewById queries for a view within another view. In this case, it's trying to query the main view, but that does not have the text views TextView01 and TextView02; text views TextView01 and TextView02 are elements of the list item views that are obtained by inflating the list item layout.

I highly suggest that you start with one the API demos first, so that you can see a complete, working example of something similar to what you are trying to accomplish. For example, http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

Post a Comment for "2 Lines In A Listview Item"