java - 为适配器 View 设置 Onclick 监听器

标签 java android onclick listener adapter

我有以下布局文件和适配器代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:background="@drawable/white"
android:orientation="horizontal"
android:padding="10sp">
<ImageView android:id="@+id/Logo"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="6dip" />    
<TextView 
android:id="@+id/name"
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:textColor="#FF000000"
android:textStyle="bold"
android:textSize="12sp"
android:typeface="sans"  
android:layout_toRightOf="@id/Logo" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:layout_below="@id/name"
android:textColor="#0000CC"
android:layout_toRightOf="@id/Logo" />
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="12sp"
android:layout_below="@id/description"
android:textColor="#990000"
android:layout_toRightOf="@id/Logo" 
/>
</RelativeLayout>    

private void showPlaces(Cursor cursor) {

    MyCustomAdapterClass adapter = new MyCustomAdapterClass(this,cursor, true);  
setListAdapter(adapter); 

}

谁能告诉我如何设置 onclick 监听器来检测某人何时单击我的列表中的项目?

谢谢!

编辑整个主要内容:

package org.example.DatabaseImport;

import java.io.IOException;

import android.app.Activity;
import android.database.SQLException;
import android.os.Bundle;
import static android.provider.BaseColumns._ID;
import static org.example.DatabaseImport.Constants.TABLE_NAME;
import static org.example.DatabaseImport.Constants.AREA;
import static org.example.DatabaseImport.Constants.ADDRESS;
import static org.example.DatabaseImport.Constants.UNIQUEID;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.app.ListActivity;
import android.widget.SimpleCursorAdapter;

public class Main extends ListActivity {

private DataBaseHelper myDbHelper;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

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

    yourList.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                        Log.e("onClick",""+arg1);
                        }   
    });



    myDbHelper = new DataBaseHelper(this);

    try {

        myDbHelper.createDataBase();

} catch (IOException ioe) {

    throw new Error("Unable to create database");

}

try {

    myDbHelper.openDataBase();

}catch(SQLException sqle){

    throw sqle;

}

try {

    Cursor cursor = getPlaces();
    showPlaces(cursor);

}

finally {

myDbHelper.close();

}
}

private static String[] FROM = { _ID, AREA, ADDRESS, UNIQUEID};
private static String ORDER_BY = _ID + " ASC";

private Cursor getPlaces() {

    SQLiteDatabase db = myDbHelper.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, ORDER_BY);
    startManagingCursor(cursor);
    return cursor;

}

private static int[] TO = {R.id.name, R.id.description, R.id.address, };
private void showPlaces(Cursor cursor) {

    MyCustomAdapterClass adapter = new MyCustomAdapterClass(this,cursor, true); setListAdapter(adapter); 

}

这是 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"
>
<ListView 
android:id="@android:id/list"
android:layout_width="wrap_content" 
android:layout_height="wrap_content" />
</LinearLayout>

最佳答案

public class Main extends ListActivity implements OnItemClickListener {

private DataBaseHelper myDbHelper;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    myDbHelper = new DataBaseHelper(this);

    try {
      myDbHelper.createDataBase();
    } catch (IOException ioe) {
    throw new Error("Unable to create database");
    }
    try {
    myDbHelper.openDataBase();
    }catch(SQLException sqle){

    throw sqle;

}

try {

    Cursor cursor = getPlaces();
    showPlaces(cursor);

}

finally {

myDbHelper.close();

}
}

private static String[] FROM = { _ID, AREA, ADDRESS, UNIQUEID};
private static String ORDER_BY = _ID + " ASC";

private Cursor getPlaces() {

    SQLiteDatabase db = myDbHelper.getReadableDatabase();
    Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, ORDER_BY);
    startManagingCursor(cursor);
    return cursor;

}

private static int[] TO = {R.id.name, R.id.description, R.id.address, };
private void showPlaces(Cursor cursor) {

    MyCustomAdapterClass adapter = new MyCustomAdapterClass(this,cursor, true); setListAdapter(adapter); 

}

@Override
    public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
        Log.e("Position",""+position);

    }

关于java - 为适配器 View 设置 Onclick 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7316955/

相关文章:

java - Hibernate @Inheritance 以表为父级, View 为子级

Java EE 自动完成

IF 语句完成后 Java 重新触发循环

java - 从自定义 View 调用 findViewById 返回 null

javascript - 如何通过自定义属性获取另一个元素内元素内的文本

java - 从字符串中获取与正则表达式匹配的未知值的字符串方法

android - 在 Android 上相当于键值观察

android - SwipeRefreshLayout fragment 创建时没有动画

android - onTouch 和 onClick 没有正确触发

javascript - onclick 事件作用于特定按钮