Android 使用SimpleCursorAdapter的OnClickListener的getView()
希望你很好。 我搜索了更多关于如何在getView()方法中实现OnclickListener的信息,但这仍然没有实现,我想通过单击ListView中的按钮来执行一个操作。 这是我的密码Android 使用SimpleCursorAdapter的OnClickListener的getView(),android,Android,希望你很好。 我搜索了更多关于如何在getView()方法中实现OnclickListener的信息,但这仍然没有实现,我想通过单击ListView中的按钮来执行一个操作。 这是我的密码 public class Restaurant extends Activity { private DatabaseHandler myDatabaseHandler; ListView listContent; Context context; TextView rest_name; TextView re
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文件:listview的行
<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>
它是B'Z,因为您没有使用CustomCursorAdapter
您正在使用
final ListAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.lists, cursor, from, to);
您已经在CustomCursorAdapter
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();
}
});
所以你应该用like
final CustomCursorAdapter cursorAdapter = new CustomCursorAdapter(this, R.layout.lists, cursor, from, to);
尝试删除getView方法中的else语句我认为代码对齐错误会给您带来麻烦,请尝试这种方式…希望它能起作用
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 OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"ImageClickClick "+position, Toast.LENGTH_LONG).show();
}
});
试试这个
holder.b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"ImageClickClick "+position, Toast.LENGTH_LONG).show();
}
}
});
或实现CustomCursorAdapter
private class CustomCursorAdapter extends SimpleCursorAdapter implements OnClickListener
和使用
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),“ImageClickClicked”,Toast.LENGTH\u LONG.show()
您有什么错误/结果?单击时ImageButton不起作用,无法执行我需要的操作。@Anasherif为什么要使用SimpleCorsOrAdapter?尝试使用ArrayAdapter或BaseAdapter。您的CustomCursorAdapter实现不完整。好的,您能给我一个使用ArrayAdapter或BaseAdapter的示例吗。@Anasherif如果对您有效,请不要忘记向上投票并接受答案。@Anasherif请浏览此链接。我不能用SimpleCrsorAdapter吗?!?我知道这在处理数据库时更快。
}