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