java不确定如何在主线程中引用stringArray
正如你所看到的,我一直在切割和修改我在各地找到的代码片段,而且花了很长时间
我被上下文菜单卡住了,当选择一个项目时,它没有捕获触摸选择,eclipse显示了一个错误,我不知道如何更正
我不确定如何在主线程中引用数组列表,我知道如果它是字符串中的数组,如何引用,但不在主线程中
带有*的行是给出错误的行
只是想知道有没有人可以看一眼
import java.util.ArrayList;
import 安卓.app.ListActivity;
import 安卓.media.MediaPlayer;
import 安卓.os.Bundle;
import 安卓.view.ContextMenu;
import 安卓.view.ContextMenu.ContextMenuInfo;
import 安卓.view.MenuInflater;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.AdapterView.AdapterContextMenuInfo;
import 安卓.widget.ListView;
import 安卓.widget.Toast;
public class MainActivity extends ListActivity {
private ArrayList<Sound> mSounds = null;
private SoundAdapter mAdapter = null;
static MediaPlayer mMediaPlayer = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerForContextMenu(getListView());
this.getListView().setSelector(R.drawable.selector);
mSounds = new ArrayList<Sound>();
mSounds.add(s);
s = new Sound();
s.setDescription("Echoex");
s.setSoundResourceId(R.raw.echoex);
mSounds.add(s);
s = new Sound();
s.setDescription("Edge");
s.setSoundResourceId(R.raw.edge);
mSounds.add(s);
s = new Sound();
s.setDescription("Enterprise");
s.setSoundResourceId(R.raw.enterprise);
mSounds.add(s);
s = new Sound();
s.setDescription("Envy");
s.setSoundResourceId(R.raw.envy);
mSounds.add(s);
s = new Sound();
s.setDescription("Etcher");
s.setSoundResourceId(R.raw.etcher);
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);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
********String[] names = getResources().getString(R.array.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);
}
}}
声音。爪哇:
public class Sound {
private String mDescription = "";
private int mSoundResourceId = -1;
private int mIconResourceId = -1;
public void setDescription(String description) { mDescription = description; }
public String getDescription() { return mDescription; }
public void setSoundResourceId(int id) { mSoundResourceId = id; }
public int getSoundResourceId() { return mSoundResourceId; }
public void setIconResourceId(int id) { mIconResourceId = id; }
public int getIconResourceId() { return mIconResourceId; }
}
# 1 楼答案
您的目标参数可能不正确
我觉得你的目标是
ArrayList<Sound>
类型的mSound。这种方法不起作用。如果要使用上述方法,必须创建一个XML文件。下面是一个例子这将保存到一个XML文件中,位于soundArray下面。xml。然后你可以使用上面的方法
这应该行得通
或者如果你想使用声音对象
(这不是安卓类?) 你必须在列表中循环并返回所述声音对象的字符串
但是,如果
Sound
类是来自您的自定义类,则必须查看它是否包含返回所述声音项名称的方法。如果它没有(或者如果您没有覆盖toString()-方法,它将返回引用地址,这是您不想看到的# 2 楼答案
你必须使用
而不是
# 3 楼答案
改变
到
您还在
ArrayList mSound
中添加对象,而不实例化mSound
。你应该这样做:那就去吧