android - 关于可以在 SQLite 数据库中使用项目 ID 的多按钮 ListView 项目的物流问题

标签 android sqlite listview

所以这是一个有点复杂的情况,所以我会尽力解释发生了什么。我有一个填充 ListView 的 sqlite 数据库。 ListView 中的每个项目都包含项目的名称以及两个按钮。我希望这两个按钮能够使用它们所包含的项目的 sqlite 数据库行 ID 来执行它们的目的。所以我需要获取项目的 ID,但我不认为使用 onListItemClick(ListView l, View v, int position, long id) 对我有用,因为我没有点击整个项目,而不是其中的按钮。如果我误解了这一点,请告诉我。为了单击按钮,我创建了一个扩展 SimpleCursorAdapter 的类,它可以正确处理单击。这是有问题的两个类。首先是列表 Activity ,其次是自定义列表适配器。

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。感谢您阅读并感谢您的回答。如果我可以做出任何澄清,请告诉我。

最佳答案


通过 implements OnClickListener

创建您的类
class myCustomClass implements OnClickListener {
        int rowid;
        public myCustomClass(int rowid){
            this.rowid=rowid;
        }

        @Override
        public void onClick(View v) {
            //do you code in 'rowid' will have the current row position 

        }

    }

所以在你的

getView

函数你可以像这样设置监听器

  //your other codes 
 confirm.setOnClickListener(new myCustomClass(position));
  //your other codes 
 deny.setOnClickListener(new myCustomClass(position));
  //your other codes

希望这能给你一个想法。

关于android - 关于可以在 SQLite 数据库中使用项目 ID 的多按钮 ListView 项目的物流问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4981540/

相关文章:

python - django sqlite 为别名 'default' 创建测试数据库

php - MySQL从另一个表插入随机值

java - ListView在android中打开超链接

listview - 钛JS : Is it possible to group a collection alphabetically in a ListView using data binding?

android - 使用地址打开默认导航应用程序

android - dumpsys 输出的 SQLite 缓存状态的实际含义是什么

android - Retrofit 2 在拦截中将帖子附加到请求体

android - 滚动 ListView 时出现内存不足异常?

android - 使用jsoup仅从图像src路径获取一小部分

android - 钛: Intent 后的新观点