我正在尝试使用上下文菜单来处理项目列表,如下所示,但我不确定如何将项目单击到列表名称时进行链接。包含>>>>>的行是我认为我错了。有人可以看看我做错了什么吗? 这是我的Main_Acitivty:
import java.util.ArrayList;
import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerForContextMenu(getListView());
this.getListView().setSelector(R.drawable.selector);
//create a simple list
mSounds = new ArrayList<Sound>();
Sound s = new Sound();
s.setDescription("Anjels");
s.setSoundResourceId(R.raw.anjels);
mSounds.add(s);
s = new Sound();
s.setDescription("Aggro");
s.setSoundResourceId(R.raw.aggro);
mSounds.add(s);
s = new Sound();
s.setDescription("Axo");
s.setSoundResourceId(R.raw.axo);
mSounds.add(s);
s = new Sound();
s.setDescription("Basix");
s.setSoundResourceId(R.raw.basix);
mSounds.add(s);
s = new Sound();
s.setDescription("Bender");
s.setSoundResourceId(R.raw.bender);
mSounds.add(s);
s = new Sound();
s.setDescription("Blimp");
s.setSoundResourceId(R.raw.blimp);
mSounds.add(s);
mAdapter = new SoundAdapter(this, R.layout.list_row, mSounds);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id){
Sound s = (Sound) mSounds.get(position);
MediaPlayer mp = MediaPlayer.create(this, s.getSoundResourceId());
mp.start();
}@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
>>>>>>String[] names = getResources().getStringArray(mSounds);
switch(item.getItemId()) {
case R.id.setasnotification:
Toast.makeText(this, "Applying " + getResources().getString(R.string.setas) +
" context menu option for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}}
答案 0 :(得分:1)
CHange String[] names = getResources().getString(R.array.?????);
到String[] names = getResources().getStringArray(R.array.?????);
您可以参考android docs on String Resources。
字符串数组资源示例:
XML file saved at res/values/strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
要访问行星数组:
String[] names = getResources().getStringArray(R.array.planets_array);