![](/img/trans.png)
[英]Reload ListView by an OnClickListener in a SimpleCursorAdapter
[英]getView() with OnClickListener using SimpleCursorAdapter
希望你好好的。 我搜索了更多有关如何在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文件: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>
您正在使用CustomCursorAdapter的是B'coz
您正在使用
final ListAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.lists, cursor, from, to);
并且您在CustomCursorAdapter内给出了onClick操作
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();
}
});
所以你应该使用像
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_LONG).show();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.