希望你好好的。我搜索了有关如何在 getView() 方法中实现 OnclickListener 的更多信息,但这仍然没有实现,我想通过单击位于 ListView 内的按钮来执行操作。这是我的代码。
public class Restaurant extends Activity {
private DatabaseHandler myDatabaseHandler;
ListView listContent;
Context context;
TextView rest_name;
TextView rest_hotline;
ImageButton call;
ImageView rest_image;
String image_string;
String r_n = "";
String r_e = "";
String r_h = "";
private CursorAdapter dataSource;
RelativeLayout resta;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_restaurant);
listContent = (ListView)findViewById(R.id.restaurants_list);
call = (ImageButton) findViewById(R.id.call);
/*
* Create/Open a SQLite database
* and fill with dummy content
* and close it
*/
myDatabaseHandler = new DatabaseHandler(this);
myDatabaseHandler.openToWrite();
myDatabaseHandler.deleteAll();
myDatabaseHandler.insert("First","1111",R.drawable.ic_launcher);
myDatabaseHandler.insert("Second","0000",R.drawable.ic_launcher);
myDatabaseHandler.close();
/*
* Open the same SQLite database
* and read all it's content.
*/
myDatabaseHandler = new DatabaseHandler(this);
myDatabaseHandler.openToRead();
Cursor cursor = myDatabaseHandler.queueAll();
startManagingCursor(cursor);
String[] from = new String[]{DatabaseHandler.KEY_REST_NAME,DatabaseHandler.KEY_REST_HOTLINE, DatabaseHandler.KEY_REST_IMAGE};
int[] to = new int[]{R.id.rest_name,R.id.rest_hotline, R.id.rest_image};
final ListAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.lists, cursor, from, to);
listContent.setAdapter(cursorAdapter);
myDatabaseHandler.close();
}
private class CustomCursorAdapter extends SimpleCursorAdapter {
public CustomCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
}
class ViewHolder {
ImageButton b;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
View view = convertView;
ViewHolder holder;
if(view == null) {
LayoutInflater vi = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.lists, null);
holder = new ViewHolder();
holder.b = (ImageButton) view.findViewById(R.id.call);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
holder.b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"ImageClickClick "+position, Toast.LENGTH_LONG).show();
}
});
}
return view;
}
XML 文件:列表视图的行
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/restau"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/resta"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#e74c3c">
<ImageView
android:id="@+id/rest_image"
android:layout_width="70dip"
android:layout_height="70dip"
android:layout_marginLeft="10dip"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/rest_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dip"
android:layout_toRightOf="@+id/rest_image"
android:text="ALL"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="25dip"
android:typeface="sans"/>
<TextView
android:id="@+id/rest_hotline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="15dip"
android:layout_toRightOf="@+id/rest_image"
android:text="Hotline"
android:layout_below="@+id/rest_name"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="15dip"
android:typeface="sans"/>
<ImageButton
android:id="@+id/call"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="15dip"
android:focusable = "false"
android:clickable="true"
android:background="@drawable/ic_launcher"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/stroke"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_below="@+id/cat"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:background="#d93b2b">
</RelativeLayout>
</RelativeLayout>