onListItemClick,单击标题时显示描述吐司

时间:2011-09-27 15:42:37

标签: java android 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();

    }
}

2 个答案:

答案 0 :(得分:0)

您可以将protected void onListItemClick方法替换为下面的代码onCreate。如果它不起作用。

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执行该部分,否则将无法访问该方法的后续部分。

答案 1 :(得分:0)

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

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