tags:

views:

45

answers:

1

Dear friends, I have created one music app. It contains some collection of albums, when we click on a particular album the songs in that album will be loaded in to anther activity. These songs are displayed as rows of table layout and these rows are dynamically changed based on the songs.I have registered context menu for tablerow and this context menu is also displayed. But my problem is to find a particular row on which the context menu is displayed not possible in onContextItemSelected(). I tried ContextMenuInfo info = item.getMenuInfo(); but info gets null. My questions are, 1)Is context menu works only for ListView by using Adapters? 2)Tablelayout is also works like ListView, then what is problem with my code?

Code snap

 public class AlbumActivity extends Activity{
   private int DYNAMIC_ROW_ID = 1;

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.album);
    LoadingAlbumThread t = new LoadingAlbumThread(dataHandler);
    t.start();
}
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo menuInfo){
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.album_contextmenu, menu);  
 }

public boolean onContextItemSelected(MenuItem item){
    ContextMenuInfo info = item.getMenuInfo();    // here 'info' gets null value,that is the main problem in my application
    switch(item.getItemId()){
    case R.id.playsong_contextmenuitem:
        playSong(info.id); //This 'info.id' gives the id of selected table row
        return true;
    case R.id.add_to_playlist_contextmenuitem:
        return true;
    case R.id.emial_contextmenuitem:
        return true;
    case R.id.twitter_contextmenuitem:
        return true;
    case R.id.facebook_contextmenuitem:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }

}
final Handler dataHandler = new Handler(){
    public void handleMessage(Message msg){
        TableRow tr = new TableRow(getBaseContext());
        registerForContextMenu(tr);
            tr.setId(DYNAMIC_ROW_ID++);
            .......
            .......
    }
};
private class LoadingAlbumThread extends Thread{
Handler handler = null;
public LoadingDataThread(Handler handler) {
   this.handler = handler;
}
public void run(){
      albumJSONObject = getJSONObject(intent.getStringExtra("JSONAlbumData"));
      if(albumJSONObject!=null)
             albumJSONArray = albumJSONObject.getJSONArray("album");
      if(albumJSONArray!=null){                 
      noOfSongs = albumJSONArray.length();
      for(int i=0;i<noOfSongs;i++){
    songTitleStr = songsJSONArray.getJSONObject(i).getString("song_title");
    handler.sendMessage(handler.obtainMessage());
      }
      }
   }private JSONObject getJSONObject(String data){
        JSONObject jsonObj = null;
            if(data!=null){
                jsonObj = new JSONObject(data)
            }
        return jsonObj;
}

I welcome your valuable suggestions.

Thanks venu

A: 

Is context menu works only for ListView by using Adapters?

No, a context menu can be set up by any widget. In your case, you will need to call registerForContextMenu() for each TableRow.

That being said, I recommend you convert this to a ListView if you expect to have more than a few dozen rows.

CommonsWare
thanks for your good suggestion..In my application all songs are displayed as table rows dynamically..so when i am going to create a table row for a song i registered the context menu for that row. i repeat this process for all table rows(songs). My problem is when i click a song(table row) the context menu will be displayed and then if we select any menu item the onContextitemSelected() will be called but i am unable to find the selected row in this method..can you give me the suggestions in this case?? What will happen if more mo.of table rows will be created dynamically?? Thanks in advance.
Venu Gopal
@Venu Gopal: "the onContextitemSelected() will be called but i am unable to find the selected row in this method" -- good point. "can you give me the suggestions in this case?" -- if your context menu choices happen to all call `startActivity()`, you could attach `Intents` to each menu item and then you would not need `onContextItemSelected()`. Or, you could call `setOnMenuItemClickListener()` on each `MenuItem`, and then you would not need `onContextItemSelected()`.
CommonsWare
Again thanks for your reply..in my app, i am calling a method after clicking on menuitem..could you please tell me how can i use setOnMenuItemClickListener()..??
Venu Gopal
@Venu Gopal: Call `setOnMenuItemClickListener()` from `onCreateContextMenu()`, as part of defining your menu. You supply a listener object. You can teach that object which song that menu item pertains to (e.g., `final` local variable, constructor parameter on your custom listener subclass). However, I would like to take this opportunity to re-emphasize my recommendation that you convert this to a `ListView` if you expect to have more than a few dozen rows.
CommonsWare
@CommonsWare..i definitely follow your recommendation. Coming to my app..i have used MenuInflater object to load the menu from xml resource.Now tell me how can i use setOnMenuItemClickListener() or will i create the menu items programatically and then assign this listener??
Venu Gopal
@Venu Gopal: Call `getItem()` on your `Menu` after inflation to get the items for attaching listeners to them, I guess.
CommonsWare
@CommonsWare..i registered the setOnMenuItemClickListener() for each menu item.But its callback method onMenuItemClick(MenuItem item) will give MenuItem object as onContextmenuitemselected().How can i get the selected row?? for ex if we take ListView, in onContextmenuitemselected() we use AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); to find the selected row.. like this is there any easy method?
Venu Gopal
@Venu Gopal: "How can i get the selected row?" -- you provide it to the listener when you create it (e.g., `final` local variable, constructor parameter on your custom listener subclass).
CommonsWare
Sorry CommonsWare..i am not getting your suggestion. I created table rows(songs) dynamically, registered each table row for context menu. I assigned setOnMenuItemClickListener() for each menu item in onCreateContextmenu()..Now tell me what can i do to get the selected table row in onMenuItemClick()
Venu Gopal
@Venu Gopal: AAAAAAAAAAAAAUUUUUUGGGGHHHH! Your listener object is an instance of what in Java we refer to as a "class". A "class" can have "data members" populated by a "constructor". You are creating your listener object in `onCreateContextMenu()`, **where you know what row you are on**, because it is passed in as the 2nd parameter. So, when you call the "constructor" for your listener "class", you can pass a parameter to your listener that provides whatever details you need about your row. Your listener "class" would store that in a "data member" and could use it in `onMenuItemClick()`.
CommonsWare