2011-01-29 39 views
5

我想在每一個列表視圖兩行item.this是我的代碼,但它不工作...2號線在ListView項目

RE-編輯的代碼

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

第二WAY- - 不是工作壓力太大

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); 
     } 
+1

你需要發佈的所有代碼本,其並不清楚自己在做什麼在那裏。什麼是信息?你如何設置數據源,適配器和列表視圖? – Jems 2011-01-29 16:17:29

+0

好吧,我發佈了更多代碼sir – 2011-01-29 16:21:09

回答

0

的線條:

TextView tt = (TextView) findViewById(R.id.TextView01); 
TextView bt = (TextView) findViewById(R.id.TextView02); 

是沒有意義的,因爲在那個時候沒有msg的列表項視圖,並且您沒有在列表項視圖中通過ID查找文本視圖,而是在主視圖中查找。

當你的應用第一次啓動時,你有一個空的ListView。然後,您將消息解析爲onCreate的一部分(順便提一下,這不是一個好主意,因爲耗時的過程不應在UI線程中執行),然後繼續嘗試通過ID找到不存在的文本視圖R.id.TextView01和主視圖中的R.id.TextView02

請記住,您將需要兩個佈局XML文件:一個用於列表活動的佈局,另一個用於每個列表項的佈局。另外請記住findViewById查看以內另一種觀點。在這種情況下,它試圖查詢主視圖,但它沒有文本視圖TextView01TextView02;文本視圖TextView01TextView02是通過擴展列表項目佈局獲得的列表項目視圖的元素。

我強烈建議您先從一個API演示開始,以便您可以看到一個類似於您要完成的事情的完整工作示例。例如,http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html

+0

我真的非常感謝您的回答,但我一直有點困惑... – 2011-01-29 17:22:06

2

我只是使用充氣Twoline listitem xml佈局。在以下示例中,text1是標題,text2是子標題。

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; 

    } 



} 

而且也沒有對Twoline的ListItem一個Example

18

爲了在每個項目上創建一個ListView兩行文字,我以下列方式使用SimpleAdapter

創建List將保存您的數據。每個Map條目將具有ListView項目中的每一個的鍵(第一行)和值(第二行)。

List<Map<String, String>> data = new ArrayList<Map<String, String>>(); 

因爲我們希望把在ListView,創建一個HashMap並將其添加到之前聲明列表中的每個對。

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

然後申報SimpleAdapter。請注意,在構造函數中使用「First Line」和「Second Line」字符串。這將告訴適配器如何使用之前聲明的Map的字符串。還要注意使用佈局android.R.layout.simple_list_item_2和文本ID android.R.id.text1android.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 }); 
0

如果你不關心佈局和東西太多,只是想在列表視圖上多行。要做到這一點最簡單的方法就是這樣用回車「\ 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");