2011-09-27 158 views
2

不知道我是否在正確的部分,但我需要幫助爲我的學校項目。onListItemClick當標題被點擊時顯示說明烤麪包

目前我正在做一個listview,顯示最新的學校新聞的標題,每當我點擊任何一個標題,我希望它相應地爲選定的標題描述。

任何人都可以幫助我嗎?謝謝

package sp.buzz.rss; 

    import java.util.ArrayList; 

    import android.app.ListActivity; 
    import android.os.Bundle; 
    import android.util.EventLogTags.Description; 
    import android.view.View; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.Toast; 
    import sp.buzz.rss.tools.*; 

public class StringRss extends ListActivity { 
    HttpFetch a = new HttpFetch(); 

    /** Called when the activity is first created. */ 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     String strOrg = a 
       .DownloadText("http://www.sp.edu.sg/wps/wcm/connect/lib-spws/Site-SPWebsite/?srv=cmpnt&source=library&cmpntname=MNU-MobileRSSFeed-SPBuzz-Shine"); 
     int start = strOrg.indexOf("<title>"); 
     int end = strOrg.indexOf("</title>"); 

     int startdesc = strOrg.indexOf("<![CDATA["); 
     int enddesc = strOrg.indexOf("]]>"); 

     int count = 0; 

     ArrayList<String> value = new ArrayList(); 
     ArrayList<String> cData = new ArrayList(); 

     String title = strOrg.substring(start + 7, end); 
     String description = strOrg.substring(startdesc + 9, enddesc); 
     // Toast.makeText(this, title, Toast.LENGTH_LONG).show();// first title 
     Toast.makeText(this, description, Toast.LENGTH_LONG).show();// first 
                    // desc 
     // value.add(title); 
     // count++; 
     cData.add(description); 

     String newContent = strOrg.substring(end + 5); 

     String newDesc = strOrg.substring(enddesc + 3); 

     start = newContent.indexOf("<title>"); 
     end = newContent.indexOf("</title>"); 

     startdesc = newDesc.indexOf("<![CDATA["); 
     enddesc = newDesc.indexOf("]]>"); 

     title = newContent.substring(start + 7, end); 
     description = newDesc.substring(startdesc + 9, enddesc); 

     // Toast.makeText(this, title, Toast.LENGTH_LONG).show();// second title 
     Toast.makeText(this, description, Toast.LENGTH_LONG).show();// second 
                    // desc 
     value.add(title); 
     cData.add(description); 
     count++; 

     while (true) { 

      newContent = newContent.substring(end + 5); 
      newDesc = newDesc.substring(enddesc + 3); 

      start = newContent.indexOf("<title>"); 
      end = newContent.indexOf("</title>"); 

      startdesc = newDesc.indexOf("<![CDATA["); 
      enddesc = newDesc.indexOf("]]>"); 

      if (start == -1 || end == -1) { 
       break; 
      } else if (startdesc == -1 || enddesc == -1) { 
       break; 
      } 

      title = newContent.substring(start + 7, end); 
      description = newDesc.substring(startdesc + 9, enddesc); 
      // Toast.makeText(this, description, Toast.LENGTH_LONG).show();// 
      // for 
      count++; 
      value.add(title); 
      cData.add(description); 
      /* 
      * Toast.makeText(this, "Value array: " + title, Toast.LENGTH_LONG) 
      * .show();// for debugging 
      */ 
      // Toast.makeText(this, description, Toast.LENGTH_LONG).show();// 
      // for 
      // description 

     } 

     // Create an array of Strings, that will be put to our ListActivity 
     String[] names = new String[count]; 
     String[] desc = new String[count]; 
     // Create an ArrayAdapter, that will actually make the Strings above 
     // appear in the ListView 
     for (int i = 0; i < names.length; i++) { 

      names[i] = value.get(i); 

     } 

     for (int i = 0; i < desc.length; i++) { 

      desc[i] = cData.get(i).replaceAll("</P>", "\n") 
        .replaceAll("<P>", ""); 
     } 

     this.setListAdapter(new ArrayAdapter<String>(this, 
       android.R.layout.simple_list_item_1, names)); 

    } 

    static String title = ""; 
    static String desc = ""; 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 
     // Get the item that was clicked 

     Object o = this.getListAdapter().getItem(position); 
     title = o.toString(); 
     Toast.makeText(this, "You selected: " + title, Toast.LENGTH_LONG) 
       .show(); 

    } 
} 
+0

即使這是一個學校項目,閱讀如何正確解析XML - 'substring' /'indexOf'不正確:)它會爲您節省很多麻煩,可能已經在這個項目中。這裏有一個很好的介紹,即使涉及RSS,所以它應該很容易適應:http://www.ibm.com/developerworks/opensource/library/x-android/ –

+0

嗨,感謝您的回覆,我之前讀過它,我試着跟着它,但失敗了,現在沒有太多時間來改變,因爲我今天的截止日期是,謝謝。 – user967424

+0

使用你的例子我得到了這個工作:String title =((Header)l.getItemAtPosition(position))。title.toString(); – Logic1

回答

0

您可以通過投入onCreate下面的代碼替換方法protected void onListItemClick。如果它不工作。

ListView lv = getListView(); 

lv.setOnItemClickListener(new OnItemClickListener() { 
public void onItemClick(AdapterView<?> parent, View view, 
    int position, long id) { 
    String title = ((TextView) view).getText(); 
    Toast.makeText(this, "You selected: " + title, Toast.LENGTH_LONG) 
      .show(); 
}}); 

此外,我很驚訝,無限while循環工作。您應該使用threading來完成該部分,否則將無法達到該方法的後續部分。

+0

嗨,感謝您的回覆,我是新來的機器人,所以請原諒我,現在整個代碼工作,只是當我點擊標題在列表視圖中,它會顯示您選擇:(標題),但是,我想敬酒是與該標題相對應的描述,是否可以這樣做?謝謝 – user967424

+0

我已經在代碼中改變了從listitem獲得標題的方式。你在做的方式是使用一般對象來字符串方法,而這種方法是專門從Android textviews獲取文本。 – silleknarf

+0

啊,我還是不太明白,你能從我的代碼中加入嗎?謝謝 – user967424

0

String title =(String)parent.getItemAtPosition(position);

Toast.makeText(this,「You selected:」+ title,Toast.LENGTH_LONG) .show();

相關問題