【问题标题】:onListItemClick that shows description toast when title is clickedonListItemClick 在单击标题时显示描述 toast
【发布时间】:2011-09-27 15:42:37
【问题描述】:

不确定我是否在正确的部分,但我的学校项目需要帮助。

目前我正在做一个显示最新学校新闻标题的列表视图,每当我点击任何标题时,我都希望它相应地烤出所选标题的描述。

有人可以帮我吗?谢谢

    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,所以应该很容易适应:ibm.com/developerworks/opensource/library/x-android
  • 您好,感谢您的回复,我之前已经阅读过,我尝试关注它但失败了,现在没有太多时间更改,因为我的截止日期是今天,谢谢。
  • 使用你的例子我得到了这个工作: String title = ((Header) l.getItemAtPosition(position)).title.toString();

标签: java android listview


【解决方案1】:

您可以通过将下面的代码放入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();
}});

我也很惊讶无限循环正在工作。您应该使用threading 完成该部分,否则将无法到达该方法的后面部分。

【讨论】:

  • 嗨,感谢您的回复,我是 android 新手,所以请原谅我,现在整个代码都在工作,只是当我单击列表视图中的标题时,它会显示 You selected : (title ) 但是,我希望 toast 是与该标题相对应的描述,可以这样做吗?谢谢
  • 我在代码中更改了从列表项中获取标题的方式。您正在做的方式是使用通用对象到字符串方法,而此方法特定于从 Android 文本视图中获取文本。
  • 啊我还是不太明白,你能从我的代码中添加它吗?谢谢
  • 添加String title = ((TextView) view).getText(); 而不是Object o = this.getListAdapter().getItem(position); title = o.toString();
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多