OnListItemClick未触发

时间:2011-12-09 06:30:32

标签: android onitemclicklistener

我正在尝试为在主活动(扩展Activity)内创建的ListView设置OnItemClickListener。以下代码未触发。为什么onItemClick不运行?

Main.java

import java.io.InputStream;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class Main extends Activity {
    /** Called when the activity is first created. */
    List<Title> titleList;
    ListView lv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // set list view
        setContentView(R.layout.listview);
        setTitle("TITLE");

        // Create Parser for raw/countries.xml
        TitleParser titleParser = new TitleParser();
        InputStream inputStream = getResources().openRawResource(R.raw.titles);

        // Parse the input stream
        titleParser.parse(inputStream);

        // Get Titles
        titleList = titleParser.getList();

        // Create a customized ArrayAdapter
        TitleArrayAdapter adapter = new TitleArrayAdapter(
                getApplicationContext(), R.layout.title_listitem, titleList);

        // Get reference to ListView holder
        lv = (ListView) this.findViewById(R.id.titleLV);

        // Set the ListView adapter
        lv.setAdapter(adapter);

        // on list item click
        AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                Title title = (Title) lv.getItemAtPosition(position);
                try{
                    Class<?> challengeClass = Class.forName("com.glvangorp.app.TITLECHALLENGE");
                    Intent challengeIntent = new Intent(Main.this, challengeClass);
                    challengeIntent.putExtra("challenge", title.challenge);
                    challengeIntent.putExtra("title", title.resourceId);
                    startActivity(challengeIntent);
                } catch(ClassNotFoundException e){
                    Log.d("TAG", e.getMessage());
                    e.printStackTrace();
                }
            }
        };

        lv.setOnItemClickListener(listener);        
    }
}

listview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/titleLV"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    </ListView>
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

一个没有调用的原因是列表项已被设置为可点击的false。或者在列表项上设置了一些单击或触摸侦听器并返回true(如果已写入则使其返回false)。

答案 1 :(得分:0)

也许它正在被调用,但你只是得到一个ClassNotFoundException因此,你不知道它是否在OnItemClick中做了什么...为什么你不把Log.d("TAG", e.getMessage);放在

  

catch子句。