ListItem单击不起作用

时间:2012-04-17 18:36:45

标签: java android android-listview

在我的代码中,我想在Rss feed的列表中显示webview和textview。这是代码:

 public class VediolistfetchActivity extends Activity {
        /** Called when the activity is first created. */

        ListView Feed;
        URL url;
        String[] title = { " " }, image={""};
        ArrayAdapter<String> adapter;
        ArrayList<Home> homes = new ArrayList<Home>();


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            Feed = (ListView) findViewById(R.id.list);


            try {

                SAXParserFactory saxPF = SAXParserFactory.newInstance();
                SAXParser saxP = saxPF.newSAXParser();
                XMLReader xmlR = saxP.getXMLReader();

                url = new URL("http://www.powergroupbd.com/sweethome/videolist.xml");

                RSSHandler myXMLHandler = new RSSHandler();
                xmlR.setContentHandler(myXMLHandler);
                xmlR.parse(new InputSource(url.openStream()));

                ArrayList<String> GOTTitle = myXMLHandler.gotTitle();
                ArrayList<String> GOTImage = myXMLHandler.gotImage();

                title = new String[GOTTitle.size()];
                image = new String[GOTImage.size()];

                for (int i = 0; i < GOTTitle.size(); i++) {
                    Home home = new Home();
                    title[i] = GOTTitle.get(i).toString();
                    image[i] = GOTImage.get(i).toString();
                    home.openclose=GOTTitle.get(i);
                    home.imagehome=GOTImage.get(i);
                    homes.add(home);
                }

                HomeAdapter homeAdapter = new HomeAdapter(
                        VediolistfetchActivity.this, R.layout.list_catagory,
                        homes);

                Feed.setAdapter(homeAdapter);

            }

            catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                        "Something Went Wrong, Please check your net connection",
                        Toast.LENGTH_LONG).show();
            }
        }
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub

            Log.i("List Click", "Yes");
            String dtlHomeTitle = Feed.getItemAtPosition(arg2).toString();
            Log.i("title", dtlHomeTitle);


        }


    }

这是我的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
<ListView
                    android:id="@id/list"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:divider="#000000"
                    android:scrollbars="none"
                    android:scrollingCache="true" />
</LinearLayout>

这是listcatagory.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
       <ListView
        android:id="@+id/list"
        android:layout_width="1px"
        android:layout_height="1px"
        android:layout_weight="1" >
    </ListView>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:weightSum="100" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="38"
            android:orientation="vertical" >

            <WebView
                android:id="@+id/img"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:paddingBottom="2dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingTop="2dp"
                android:scaleType="centerCrop"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="15"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:textSize="20dp"
                android:textStyle="bold"
                android:paddingLeft="5dp" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="47"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>



</LinearLayout>

但是当我点击个人列表项时,没有任何事情发生..plz有人告诉我为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

您实际上没有在ListView上设置onItemClick侦听器

在你的onCreate中尝试这个

Feed = (ListView) findViewById(R.id.list);
Feed.setOnItemClickListener(this);

@edit:注意到你实际上没有让你的类实现OnItemClickListener,尽管创建了一个与你实现的方法相同的方法。确保将implements OnItemClickListener添加到您的班级声明

public class VediolistfetchActivity extends Activity implements OnItemClickListener