code_finder
code_finder

Reputation: 1370

How to get data from listview to context menu?

actually i got a problem while creating my project. I am having one list view in one screen where data comes from database & binds to it. I created one context menu here with two menus (view & delete). The problem i am facing is when i make a long click context menu occur & when i click on anyone of the menu it navigates to another screen. here i want the listview(which was clicked) item data to pass to next screen. By i am not getting it. This is the following code...

Main.java

     /*******some code****/

    DbHandler dbh=new DbHandler(GroupName.this);
    ast=dbh.selectgroupnam(s); 
       //here "ast" is of ArrayList defined globally        
    ArrayAdapter<String> adp=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ast);
    lv.setAdapter(adp);
    registerForContextMenu(lv); 

    lv.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> arg0, View v, int p, long arg3) {
    // TODO Auto-generated method stub  
     TextView tv=(TextView)v;
     String gnam=tv.getText().toString();

}});

  }//on create

   //context menu code

   @Override
   public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    // TODO Auto-generated method stub
    super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, v.getId(), 0, "View");  
        menu.add(0, v.getId(), 0, "Delete");    
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    Toast.makeText(getApplicationContext(), "group name" + gnam,30).show();
    if (item.getTitle() == "Delete") {
        Toast.makeText(getApplicationContext(), "selected group" + gnam, 30).show();
    startActivity(new Intent(GroupName.this,GroupEdit.class));
    }
    else 
    {
    startActivity(new Intent(GroupName.this,GroupEdit.class));
    }
    return super.onContextItemSelected(item);
}

As per the following code how to get the list view data (which was long clicked for context menu) & pass the data to GroupEdit.class.

Waiting for reply......

Upvotes: 1

Views: 1393

Answers (1)

Simon Dorociak
Simon Dorociak

Reputation: 33505

So you start activity startActivity(new Intent(GroupName.this,GroupEdit.class)) but any data you are adding to intent. So try it with putExtra(<key>,<data>) or if you want to use Bundle so putExtras(<bundle>)

You should to it like this:

Intent i = new Intent(GroupName.this,GroupEdit.class);
i.putExtra("key", <data>);
startActivity(i);

Then in new Activity GroupEdit you get this data with getIntent() method that return the intent that started this activity and getExtras() with it you retrieve a map of extended data from the intent.

So in GroupEdit String text = getIntent().getExtras().getString("keyOfField")

Of you will use Bundle so

Intent i = new Intent(GroupName.this,GroupEdit.class);
    i.putExtras(bundle);
    startActivity(i);

in GroupEdit you retrieve data with Bundle data = getIntent().getExtras()

Regards

Upvotes: 2

Related Questions