以下是main.xml中的文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ListView
android:id="@+id/lv_items"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>  
</ListView>
</LinearLayout>
下面是studetail.xml中的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:id="@+id/id"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:numeric="integer"
   ></TextView>
  <TextView  
    android:id="@+id/name"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"     
   ></TextView>
  <TextView  
    android:id="@+id/score"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"
    android:numeric="integer"
   ></TextView>
</LinearLayout>下面是MainActivity.java 中的部分内容:
        ListView lv_items = (ListView) findViewById(R.id.lv_items);
        
        ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
        HashMap<String, Object> map;        map = new HashMap<String, Object>();
        map.put("id", 1111);
        map.put("name", "james0");
        map.put("score", 156);
        data.add(map);        
        map = new HashMap<String, Object>();
        map.put("id", 2222);
        map.put("name", "james1");
        map.put("score", 156);
        data.add(map);
        SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.studetail, new String[] 
                                       {"id","name","score"}, new int[]{R.id.id,R.id.name,R.id.score});
        lv_items.setAdapter(adapter);
        Toast.makeText(MainActivity.this, "Before Hello", Toast.LENGTH_SHORT).show();
        lv_items.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
}
});

    运行取来貌似不会执行setOnItemClickListener(){}里面的内容,这是为什么呢?
    我sdk是用的2.2,LongClick的listener是有用的。

解决方案 »

  1.   

    这看起没错!要不改成这样试试!Toast.makeText(arg1.this, "Hello", Toast.LENGTH_SHORT).show();
      

  2.   

    MainActivity.java这个类代码发全啊
      

  3.   

    完整的MainActivity.java代码package james.students.activity;import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;import james.students.db.StuDBService;
    import james.students.entity.Student;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
    import android.widget.AdapterView.OnItemClickListener;public class MainActivity extends Activity {
        /** Called when the activity is first created. */
    EditText et_id;
    EditText et_name;
    EditText et_score;

    Button btn_save;
    Button btn_listall;

    ListView lv_detail;



        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            initialWidget();
            
            btn_save.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

    StuDBService service = new StuDBService(MainActivity.this);
    Student student = new Student();

    String id = et_id.getText().toString();
    String name = et_name.getText().toString();
    String score = et_score.getText().toString();

    student.setStu_id(new Integer(id));
    student.setStu_name(name);
    student.setStu_score(new Integer(score));

    service.insert(student);

    et_id.setText("");
    et_name.setText("");
    et_score.setText("");

    Toast.makeText(MainActivity.this, "保存记录成功!", Toast.LENGTH_LONG).show();
    }
    });
            
            btn_listall.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {

    StuDBService service = new StuDBService(MainActivity.this);

    List<Student> students = service.getAll();
    ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();

    for(Student stu:students){
    HashMap<String,Object> map = new HashMap<String,Object>();
    map.put("id", stu.getStu_id());
    map.put("name", stu.getStu_name());
    map.put("score", stu.getStu_score());

    data.add(map);
    }

    SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, data, R.layout.studetail, new String[]{"id","name","score"}, new int[]{R.id.id,R.id.name,R.id.score});
    lv_detail.setAdapter(adapter);
    Toast.makeText(MainActivity.this, "显示成功!", Toast.LENGTH_LONG).show();
    lv_detail.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1,
    int arg2, long arg3) {
    Log.i("TEST", "fdaga");
    Toast.makeText(MainActivity.this, "hehe", Toast.LENGTH_SHORT).show();
    }
    });

    }
    });
            
            
        }
        
        private void initialWidget()
        {
         et_id= (EditText) findViewById(R.id.et_id);
         et_name =(EditText) findViewById(R.id.et_name);
         et_score = (EditText) findViewById(R.id.et_score);
        
         btn_save = (Button) findViewById(R.id.btn_save);
         btn_listall = (Button) findViewById(R.id.btn_listall);
        
         lv_detail = (ListView) findViewById(R.id.lv_studetail);
        }
    }
      

  4.   

    lv_detail.setAdapter(adapter); 
    Toast.makeText(MainActivity.this, "显示成功!", Toast.LENGTH_LONG).show();
     lv_detail.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1,
     int arg2, long arg3) {
     Log.i("TEST", "fdaga");
     Toast.makeText(MainActivity.this, "hehe", Toast.LENGTH_SHORT).show();
     }
     });
    这个拿出来和其他2个监听器并列放置
      

  5.   

    在你的item layout根上设定如下
    android:descendantFocusability=”blocksDescendant”
    即可