正如你所看到的,我一直在砍掉和改变我在整个地方找到的代码片段,这需要很长时间!
我被困在上下文菜单上,当选择一个项目时没有捕获触摸选择,并且eclipse显示错误,我不知道如何纠正。
我不确定如何引用主线程中的数组列表,我知道如果它是一个来自字符串的数组,但不是主线程上的数组。
* 的行是提供错误的行。
只是想知道是否有人可以看一眼?
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;
/** 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);
}
}}
Sound.java:
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; }
}
答案 0 :(得分:1)
您必须使用
String[] names = getResources().getStringArray(R.array.names);
而不是
********String[] names = getResources().getString(R.array.names);
答案 1 :(得分:0)
更改
********String[] names = getResources().getString(R.array.names);
到
String[] names = getResources().getString(R.array.names);
您还在ArrayList mSound
中添加对象而不实例化mSound
。你应该这样做:
@Override
public void onCreate(Bundle savedInstanceState) {
.....
mSound = new ArrayList<Sound>();
然后做
mSound.add(s);
}
答案 2 :(得分:0)
您的目标参数可能不正确。
********String[] names = getResources().getString(R.array.mSounds);
我觉得你的目标是ArrayList<Sound>
类型的mSound。这种方法不起作用。如果要使用上述方法,则必须创建XML文件。以下是一个例子,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="myArray">
<item>first song</item>
<item>second song</item>
<item>third song</item>
<item>fourth song</item>
<item>fifth song</item>
</array>
</resources>
将其保存到 soundArray.xml 下的XML文件中。然后你可以使用上面的方法
String[] names = getResources().getStringArray(R.array.soundArray);
这应该有用。
(它不是一个Android类吗?) 你必须遍历列表并返回所述声音对象的字符串。
int length = mSounds.size(); // get the length of mSounds object
String[] names = new String[length]; // creates a fixed array with strings
for(int i = 0; i < length; i++) {
// add sound name to string array
names[i] = mSounds.get(i).getDescription(); // returns the string name
}
但是如果Sound
类是您的自定义类,则必须查看它是否包含返回所述声音项的名称的方法。如果没有(或者如果你没有覆盖toString() - 方法,它将返回参考地址,这是你不想看到的......