onListItemClick 在单击标题时显示描述 toast

发布于 2024-12-06 12:24:22 字数 4692 浏览 0 评论 0原文

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

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

任何人都可以帮助我吗?谢谢

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左秋 2024-12-13 12:24:22

您可以通过将以下代码放入 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 循环正在工作。您应该使用 线程 来完成该部分,否则后面的部分将无法达到该方法的目的。

You can replace the method protected void onListItemClick with by putting into onCreate the code below. If it isn't working.

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

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.

琴流音 2024-12-13 12:24:22

字符串标题=(字符串)parent.getItemAtPosition(位置);

Toast.makeText(this, "您选择了:" + 标题, Toast.LENGTH_LONG)
。展示();

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文