不知道我是否在正確的部分,但我需要幫助爲我的學校項目。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();
}
}
即使這是一個學校項目,閱讀如何正確解析XML - 'substring' /'indexOf'不正確:)它會爲您節省很多麻煩,可能已經在這個項目中。這裏有一個很好的介紹,即使涉及RSS,所以它應該很容易適應:http://www.ibm.com/developerworks/opensource/library/x-android/ –
嗨,感謝您的回覆,我之前讀過它,我試着跟着它,但失敗了,現在沒有太多時間來改變,因爲我今天的截止日期是,謝謝。 – user967424
使用你的例子我得到了這個工作:String title =((Header)l.getItemAtPosition(position))。title.toString(); – Logic1