Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2 lines in a listView item

Tags:

android

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<Message> messages; 

    @Override 
    public void onCreate(Bundle icicle) { 
        super.onCreate(icicle);
        setContentView(R.layout.main2);
        loadFeed(ParserType.DOM);
    }


    private void loadFeed(ParserType type){
        try{
            Log.i("News", "ParserType="+type.name());
            FeedParser parser = FeedParserFactory.getParser(type);
            long start = System.currentTimeMillis();
            messages = parser.parse();
            long duration = System.currentTimeMillis() - start;
            Log.i("News", "Parser duration=" + duration);
            String xml = writeXml();
            Log.i("News", xml);
            List<String> titles = new ArrayList<String>(messages.size());
            for (Message msg : messages){

                String o = titles.get(messages.size());
                if (o != null) {
                    TextView tt = (TextView) findViewById(R.id.TextView01);
                    TextView bt = (TextView) findViewById(R.id.TextView02);
                    if (tt != null) {
                         titles.add(""+msg.getDate());                            }
                    if(bt != null){
                        titles.add("date: "+ msg.getTitle());
                    }return;
            //titles.add(msg.getDate()+"\n"+msg.getTitle());
                }
            //  titles.add(msg.getTitle()+"  "+msg.getDate());

            }
            ArrayAdapter<String> adapter = 
                new ArrayAdapter<String>(this, R.layout.row,titles);
            this.setListAdapter(adapter);
        //  textView.setText(Html.fromHtml(titles.get(position)));

        } catch (Throwable t){
            Log.e("News",t.getMessage(),t);
        }
    }

SECOND WAY--not working too

    List<String> titles = new ArrayList<String>(messages.size());
                for (Message msg : messages){       

                        String o = titles.get(messages.size());
                    if (o != null) {
                        TextView tt = (TextView) findViewById(R.id.TextView01);
                        TextView bt = (TextView) findViewById(R.id.TextView02);
                        if (tt != null) {
                             titles.add(""+msg.getDate());                            }
                        if(bt != null){
                            titles.add("date: "+ msg.getTitle());
                        }return;
                //titles.add(msg.getDate()+"\n"+msg.getTitle());
                    }
                //  titles.add(msg.getTitle()+"  "+msg.getDate());

                }
                ArrayAdapter<String> adapter = 
                    new ArrayAdapter<String>(this, R.layout.row,titles);
                this.setListAdapter(adapter);
            //  textView.setText(Html.fromHtml(titles.get(position)));

            } catch (Throwable t){


Log.e("News",t.getMessage(),t);
        }
like image 772
menu_on_top Avatar asked Jan 29 '11 16:01

menu_on_top


2 Answers

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 = new ArrayList<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 = new HashMap<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 = new SimpleAdapter(this, data,
                    android.R.layout.simple_list_item_2, 
                    new String[] {"First Line", "Second Line" }, 
                    new int[] {android.R.id.text1, android.R.id.text2 });

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

ListView listView = (ListView) findViewById(R.id.theIdOfYourListView);
listView.setAdapter(adapter);
like image 194
MigDus Avatar answered Sep 27 '22 23:09

MigDus


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

public class CustomListAdapter extends BaseAdapter {

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

    }

    @Override
    public int getCount()
    {
        return stringArray.length;
    }

    @Override
    public Object getItem(int position)
    {
        return position;
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

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

        final MainListHolder mHolder;
        View v = convertView;
        if (convertView == null)
        {
            mHolder = new MainListHolder();
            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;
    }
    class MainListHolder 
    {
        private TextView txt1;
        private TextView txt2;

    }



}

And also there is a Example on Twoline listitem

like image 41
Ramesh Akula Avatar answered Sep 27 '22 21:09

Ramesh Akula