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

时间:2012-02-28 04:45:29

标签: java android listview

listview项目longpress打开contextmenu并执行3选项视图,编辑,删除。查看是单击而不是获取列表视图中所选项目的ID,并通过下一个活动...

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

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

getExtra

Broker_Details_View.java

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

DBUtil.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();      */
    }
}

请解决此问题如何获取所选listView项目中的ID以打开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; } } 并通过下一个活动进行意图?

1 个答案:

答案 0 :(得分:0)

为列表设置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.

        }


    });