开发者

Setting Onclick Listener for Adapter View

开发者 https://www.devze.com 2023-04-02 11:36 出处:网络
I have the following layout file and adapter code : <?xml version=\"1.0\" encoding=\"utf-8\"?> <RelativeLayout

I have the following layout file and adapter code :

<?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">
<Ima开发者_Go百科geView 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); 

}

Can anyone tell me how I set an onclick listener to detect when someone clicks on an item in my list ?

Thank you !

EDIT Whole main :

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); 

}

And here is 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);

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消