关于多按钮列表视图项目的物流问题,该项目可以使用 SQLite 数据库中项目的 id

发布于 2024-10-16 22:09:28 字数 4275 浏览 1 评论 0原文

所以这是一个有点复杂的情况,所以我会尽力解释发生了什么。我有一个填充列表视图的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

油饼 2024-10-23 22:09:28

通过 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 

        }

    }

因此在您的

获取视图

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

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

希望这能给你一个想法。

Create your a class by 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 

        }

    }

So in your

getView

function you can set listener like this

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

Hope this give you an idea.

皓月长歌 2024-10-23 22:09:28

你也许可以这样做,在你的自定义适配器中使用 getView 方法

public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        //you can set view's tag here
        //and you should do the reflect on your position to your rowid
        view.setTag(customMethodForGetId(position));
        //end
        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;
    }

,在你的点击事件中你可以通过 view.getTag() 获取 id;

you maybe can do like this,in your custom adapter the getView method

public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        //you can set view's tag here
        //and you should do the reflect on your position to your rowid
        view.setTag(customMethodForGetId(position));
        //end
        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;
    }

and in your click event you can get the id by view.getTag();

风尘浪孓 2024-10-23 22:09:28
 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);
            view.setTag(position);
            Button deny = (Button)view.findViewById(R.id.deny_request);
            deny.setOnClickListener(this);
            view.setTag(position);
            return view;
        }

@Override
public void onClick(View v) {

      // get the position here
      Integer position = (Integer)v.getTag();

      switch(v.getId()) {
        case R.id.confirm_request :
            break;
        case R.id.deny_request :
            String denyState = "3";
            break;
        }
    }
 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);
            view.setTag(position);
            Button deny = (Button)view.findViewById(R.id.deny_request);
            deny.setOnClickListener(this);
            view.setTag(position);
            return view;
        }

@Override
public void onClick(View v) {

      // get the position here
      Integer position = (Integer)v.getTag();

      switch(v.getId()) {
        case R.id.confirm_request :
            break;
        case R.id.deny_request :
            String denyState = "3";
            break;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文