关于多按钮列表视图项目的物流问题,该项目可以使用 SQLite 数据库中项目的 id
所以这是一个有点复杂的情况,所以我会尽力解释发生了什么。我有一个填充列表视图的 sqlite 数据库。列表视图中的每个项目都包含项目名称以及两个按钮。我希望这两个按钮能够使用它们所包含的项目的 sqlite 数据库行 ID 来执行其目的。所以我需要获取项目的 id,但我不认为使用 onListItemClick(ListView l, View v, int position, long id)
对我有用,因为我没有点击整个项目,而不是其中的按钮。如果我误解了这一点,请告诉我。为了单击按钮,我创建了一个扩展 SimpleCursorAdapter 的类,它可以正确处理单击。这是有问题的两个类。首先是列表活动,其次是自定义列表适配器。
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
public class FriendRequestList extends ListActivity implements OnClickListener {
private Button m_closeButton;
private TagDBAdapter mDbAdapter;
private Cursor mCursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbAdapter = new TagDBAdapter(this);
mDbAdapter.open();
this.initLayout();
fillData();
}
private void initLayout() {
setContentView(R.layout.request_list);
m_closeButton = (Button)findViewById(R.id.request_window_close_button);
m_closeButton.setOnClickListener(this);
}
private void fillData() {
SharedPreferences prefs = this.getSharedPreferences("Prefs", MODE_WORLD_READABLE);
String username = prefs.getString("keyUsername", "");
RestClient.getRequests(username, this);
mCursor = mDbAdapter.fetchAllRequests();
startManagingCursor(mCursor);
String[] from = new String[] {TagDBAdapter.KEY_NAME};
int[] to = new int[] {R.id.requester};
SimpleCursorAdapter requests = new FriendRequestListAdapter(this, R.layout.request_view, mCursor, from, to);
this.setListAdapter(requests);
}
public String getRequester() {
return null;
}
@Override
public void onClick(View v) {
SharedPreferences prefs = this.getSharedPreferences("Prefs", MODE_WORLD_READABLE);
String username = prefs.getString("keyUsername", "");
RestClient.getUpdates(username, this);
Intent intent = new Intent(this, MainMenu.class);
this.startActivity(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
mDbAdapter.close();
}
}
和适配器类:
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
public class FriendRequestListAdapter extends SimpleCursorAdapter implements OnClickListener {
private Context mContext;
public FriendRequestListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
mContext = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
Button confirm = (Button)view.findViewById(R.id.confirm_request);
confirm.setOnClickListener(this);
Button deny = (Button)view.findViewById(R.id.deny_request);
deny.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
SharedPreferences prefs = mContext.getSharedPreferences("Prefs", Activity.MODE_WORLD_READABLE);
String username = prefs.getString("keyUsername", "");
//want to get the id here so i can do stuff with the buttons
switch(v.getId()) {
case R.id.confirm_request :
String confirmState = "2";
//these buttons both work
break;
case R.id.deny_request :
String denyState = "3";
//these buttons both work
Log.e("TESTING", "deny");
break;
}
}
}
我希望我的问题已经足够清楚了。总而言之,我不知道如何获取嵌套了一对按钮的项目的 rowId,因为我从未使用过 onListItemClick 方法,据我所知,这是一种方法通常会获取项目的 id。感谢您的阅读并感谢您的回答。如果我可以做任何澄清,请告诉我。
So this is a bit complicated situation, so I will try to explain what is going on as best I can. I have a sqlite db which populates a listview. Each item in the listview contains the name of the item, along with two buttons. I want these two buttons to be able to use the sqlite db row id of the item they're contained in order to execute their purpose. So I need to get the id of the item, but I don't think using onListItemClick(ListView l, View v, int position, long id)
will work for me, becuase I'm not clicking the entire item, rather, buttons within it. If I'm misunderstanding this point, please let me know. In order for the buttons to be clicked, I've created a class which extends SimpleCursorAdapter, which correctly handles clicks. Here are the two classes in question. First, the listactivity, and second, the custom listadapter.
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
public class FriendRequestList extends ListActivity implements OnClickListener {
private Button m_closeButton;
private TagDBAdapter mDbAdapter;
private Cursor mCursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbAdapter = new TagDBAdapter(this);
mDbAdapter.open();
this.initLayout();
fillData();
}
private void initLayout() {
setContentView(R.layout.request_list);
m_closeButton = (Button)findViewById(R.id.request_window_close_button);
m_closeButton.setOnClickListener(this);
}
private void fillData() {
SharedPreferences prefs = this.getSharedPreferences("Prefs", MODE_WORLD_READABLE);
String username = prefs.getString("keyUsername", "");
RestClient.getRequests(username, this);
mCursor = mDbAdapter.fetchAllRequests();
startManagingCursor(mCursor);
String[] from = new String[] {TagDBAdapter.KEY_NAME};
int[] to = new int[] {R.id.requester};
SimpleCursorAdapter requests = new FriendRequestListAdapter(this, R.layout.request_view, mCursor, from, to);
this.setListAdapter(requests);
}
public String getRequester() {
return null;
}
@Override
public void onClick(View v) {
SharedPreferences prefs = this.getSharedPreferences("Prefs", MODE_WORLD_READABLE);
String username = prefs.getString("keyUsername", "");
RestClient.getUpdates(username, this);
Intent intent = new Intent(this, MainMenu.class);
this.startActivity(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
mDbAdapter.close();
}
}
And the adapter class:
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
public class FriendRequestListAdapter extends SimpleCursorAdapter implements OnClickListener {
private Context mContext;
public FriendRequestListAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
mContext = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
Button confirm = (Button)view.findViewById(R.id.confirm_request);
confirm.setOnClickListener(this);
Button deny = (Button)view.findViewById(R.id.deny_request);
deny.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v) {
SharedPreferences prefs = mContext.getSharedPreferences("Prefs", Activity.MODE_WORLD_READABLE);
String username = prefs.getString("keyUsername", "");
//want to get the id here so i can do stuff with the buttons
switch(v.getId()) {
case R.id.confirm_request :
String confirmState = "2";
//these buttons both work
break;
case R.id.deny_request :
String denyState = "3";
//these buttons both work
Log.e("TESTING", "deny");
break;
}
}
}
I hope I've made my question clear enough. To summarize, I don't know how I would get the rowId of the item a pair of buttons is nested in, because I'm not ever using the onListItemClick method, which is, as far as I'm aware, the way one would normally get the id of the item. Thank you for reading and thank you for your answers. Please let me know if there is any clarifications I can make.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过
implements OnClickListener
创建一个类,因此在您的
函数你可以像这样设置监听器
希望这能给你一个想法。
Create your a class by
implements OnClickListener
So in your
function you can set listener like this
Hope this give you an idea.
你也许可以这样做,在你的自定义适配器中使用 getView 方法
,在你的点击事件中你可以通过 view.getTag() 获取 id;
you maybe can do like this,in your custom adapter the getView method
and in your click event you can get the id by view.getTag();