如何获取所选列表视图项中的id以打开上下文菜单并通过下一个活动来实现目标。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何获取所选列表视图项中的id以打开上下文菜单并通过下一个活动来实现目标。相关的知识,希望对你有一定的参考价值。

列表视图项长按打开上下文菜单并执行3选项视图,编辑,删除。查看是单击而不是获取listview中的选定项目的ID和通过下一个活动的意图...... ????

我展示你的代码......

Broker_Management.java遵循代码意图通过下一个活动,而不是传递putExtra值(选择的longpress项目ID),下一个活动是打开(Broker_Details_View.java)而不是getExtra获取id值以将数据提取到sqlite中。

public class Broker_Management extends ListActivity implements OnClickListener {

    ListAdapter adapter;
    DBUtil db;
    Button btn_add_broker, btn_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.broker_management);

        //CREATE A DATABASE
        db = new DBUtil(Broker_Management.this);

        broker_list_display();

        btn_add_broker = (Button) findViewById(R.id.btn_add_broker);
        btn_back = (Button) findViewById(R.id.btn_back);
        btn_add_broker.setOnClickListener(this);
        btn_back.setOnClickListener(this);
    }

    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.btn_add_broker:
                startActivity(new Intent(Broker_Management.this, Add_New_Broker.class));
                break;
            case R.id.btn_back:
                Intent goback = new Intent(Broker_Management.this, Admin_Home.class);
                startActivity(goback);
                break;
        }
    }

    public void broker_list_display() {
        Cursor c;
        db.open();
        String[] from = new String[]{"broker_name", "city"};
        int[] to = new int[]{R.id.broker_name, R.id.broker_city};
        c = db.get_broker_list();
        //String[] columns = new String[]{c.getColumnName(1),c.getColumnName(2)};
        adapter = new SimpleCursorAdapter(Broker_Management.this, R.layout.itemlist, c, from, to);
        setListAdapter(adapter);
        db.close();
        registerForContextMenu(getListView());
    }

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Context Menu");
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.broker_context_menu, menu);
    }

    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        Cursor cursor;
        switch (item.getItemId()) {
            case R.id.view:
                Toast.makeText(this, "View is Peform", Toast.LENGTH_SHORT).show();
                Intent intentview = new Intent(Broker_Management.this, Broker_Details_View.class);
                cursor = (Cursor) adapter.getItem(info.position);
                intentview.putExtra("KEY_1", cursor.getInt(cursor.getColumnIndex("_id")));
                startActivity(intentview);
                return true;
            case R.id.update:
                Toast.makeText(this, "Update is Perform", Toast.LENGTH_SHORT).show();
                Intent intentupdate = new Intent(Broker_Management.this, Broker_Details_Update.class);
                cursor = (Cursor) adapter.getItem(info.position);
                intentupdate.putExtra("KEY_1", cursor.getInt(cursor.getColumnIndex("_id")));
                startActivity(intentupdate);
                return true;
            case R.id.delete:
                Toast.makeText(this, "Delete Activity Perform", Toast.LENGTH_SHORT).show();
                Intent intentdelete = new Intent(Broker_Management.this, Broker_Details_Update.class);
                cursor = (Cursor) adapter.getItem(info.position);
                intentdelete.putExtra("KEY_1", cursor.getInt(cursor.getColumnIndex("_id")));
                startActivity(intentdelete);
                return true;
            default:
                return super.onContextItemSelected(item);
        }
    }
}

broker_details_view.Java

public class Broker_Details_View extends Activity {

    int id;
    DBUtil db;
    TextView tv_broker_name, tv_city, tv_contact_no, tv_email_id, tv_broker_login_id, tv_joining_date;
    Button btn_update, btn_delete, btn_close, btn_menu, btn_back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.broker_details_view);
        /*  
            id=getIntent().getIntExtra("KEY_1", 0);
            db=new DBUtil(Broker_Details_View.this);

            tv_broker_name = (TextView) findViewById(R.id.tv_broker_name);
            tv_city = (TextView) findViewById(R.id.tv_city);
            tv_contact_no = (TextView) findViewById(R.id.tv_contact_no);
            tv_email_id = (TextView) findViewById(R.id.tv_email_id);
            tv_joining_date = (TextView) findViewById(R.id.tv_joining_id);

            db.open();
            Cursor c = db.get_broker_details(id);
            if(c.getCount() == 1){
                c.moveToFirst();
                tv_broker_name.setText(c.getString(c.getColumnIndex("broker_name")));
                tv_city.setText(c.getString(c.getColumnIndex("city")));
                tv_contact_no.setText(c.getString(c.getColumnIndex("contact_no")));
                tv_email_id.setText(c.getString(c.getColumnIndex("email_id")));
                tv_broker_login_id.setText(c.getString(c.getColumnIndex("join_date")));     
            }
            db.close();      */
    }
}

D but IL.Java

package com.me.DBUtil;

import android.app.Activity;
import android.database.Cursor;

public class DBUtil extends Activity {

    private static final String DATABASE_NAME = "EMS";
    private static final int DATABASE_VERSION = 1;

    //BROKER table content
    private static final String DBTABLE_BROKER = "Broker";
    private static final String B_BROKER_ID = "_id";
    private static final String B_LOGIN_ID = "login_id";
    private static final String B_BROKER_NAME = "broker_name";
    private static final String B_ADDRESS = "address";
    private static final String B_CITY = "city";
    private static final String B_CONTACT_NO = "contact_no";
    private static final String B_EMAIL_ID = "email_id";
    private static final String B_DOB = "dob";
    private static final String B_AVTAR = "avtar";
    private static final String B_JOIN_DATE = "join_date";

    public Cursor get_broker_list() {
        String[] columns = new String[]{B_BROKER_ID, B_BROKER_NAME, B_CITY};
        Cursor c = ourDatabase.query(DBTABLE_BROKER, columns, null, null, null, null, null);
        return c;
    }

    public Cursor get_broker_details(int id) {
        String[] columns = new String[]{B_BROKER_ID, B_BROKER_NAME, B_CITY, B_CONTACT_NO, B_EMAIL_ID, B_JOIN_DATE};
        String con = "" + B_BROKER_ID + "=" + id;
        Cursor c = ourDatabase.query(DBTABLE_BROKER, columns, con, null, null, null, null);
        return c;
    }
}

请解决这个问题如何获取所选listView项目中的id以打开ContextMenu并通过下一个活动进行意图?

答案

为列表设置onItemClickListener。

 getListView().setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long arg3) {
            // TODO Auto-generated method stub

                            //open the context menu for the position
                            //to pass to the intent, retrive the value for the position from the cursor and pass it to the intent.

        }


    });

以上是关于如何获取所选列表视图项中的id以打开上下文菜单并通过下一个活动来实现目标。的主要内容,如果未能解决你的问题,请参考以下文章

从选定的列表视图项中获取 id

在 LongPress 上显示 ContextMenu 以获取 xamarin 表单中的视图

如何在jquery移动自动完成中获取所选列表数据的ID

如何以编程方式打开 Bootstrap 下拉菜单

带有列表子项可聚焦android的列表视图上下文菜单

根据所选列表视图项的位置设置铃声