onListItemClick 在单击标题时显示描述 toast
不确定我是否在正确的部分,但我的学校项目需要帮助。
目前我正在做一个列表视图,显示最新学校新闻的标题,每当我单击任何标题时,我希望它相应地显示所选标题的描述。
任何人都可以帮助我吗?谢谢
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();
}
}
not sure if I am on the correct section, but I needed help for my school project.
Currently I am doing a listview that display the titles of the latest school news, whenever I click any of the title, I want it to toast the description of the selected title correspondingly.
Anyone can help me on it? Thank you
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过将以下代码放入
onCreate
中来替换方法protected void onListItemClick
。如果它不起作用。我也很惊讶无限 while 循环正在工作。您应该使用 线程 来完成该部分,否则后面的部分将无法达到该方法的目的。
You can replace the method
protected void onListItemClick
with by putting intoonCreate
the code below. If it isn't working.Also I'm surprised that infinite while loop is working. You should do that part using threading otherwise the later parts of the method won't be reached.
字符串标题=(字符串)parent.getItemAtPosition(位置);
Toast.makeText(this, "您选择了:" + 标题, Toast.LENGTH_LONG)
。展示();
String title = (String) parent.getItemAtPosition(position);
Toast.makeText(this, "You selected: " + title, Toast.LENGTH_LONG)
.show();