关于多按钮列表视图项的后勤问题,可以使用SQLite DB中项的id

时间:2011-02-13 00:10:32

标签: android sqlite listview

所以这是一个有点复杂的情况,所以我会尽力解释发生了什么。我有一个sqlite数据库填充listview。列表视图中的每个项目都包含项目的名称以及两个按钮。我希望这两个按钮能够使用它们包含的项的sqlite db行id来执行它们的目的。所以我需要得到该项目的ID,但我不认为使用onListItemClick(ListView l, View v, int position, long id)对我有效,因为我没有点击整个项目,而是点击其中的按钮。如果我误解了这一点,请告诉我。为了点击按钮,我创建了一个扩展SimpleCursorAdapter的类,它正确处理点击。这是有问题的两个类。首先是listactivity,其次是自定义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();
    }

}

适配器类:

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

3 个答案:

答案 0 :(得分:1)


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

希望这会给你一个想法。

答案 1 :(得分:0)

你可以这样做,在自定义适配器中使用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

答案 2 :(得分:0)

 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;
        }
    }