gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 15:44:39 24 4
gpt4 key购买 nike

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

<?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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com