开发者

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<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.s开发者_如何学PythonetListAdapter(adapter);
            //  textView.setText(Html.fromHtml(titles.get(position)));

            } catch (Throwable t){


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


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


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


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 = new ArrayList<>();

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

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

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


The lines:

TextView tt = (TextView) findViewById(R.id.TextView01);
TextView bt = (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


If you don't care too much about layout and stuff and just want multiple lines on a listview. The easiest way to do that is like this with the carriage return '\n'

xList = FindViewById<ListView>(Resource.Id.xlist);
mItems = new List<string>();
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mItems);

xList.Adapter = adapter;
adapter.Add("FirstLine\nSecondLine\nThirdLine\nEtc");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜