这是我的代码,我在这行收到错误:
String[] opt = getResources().getStringArray(R.array.MainActivity);
所以,请检查我的代码并告诉我如何解决该行中的错误
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
String[] opt = getResources().getStringArray(R.array.MainActivity);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView lv = getListView();
ListAdapter la = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, opt);
lv.setAdapter(la);
lv.setTextFilterEnabled(true);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
Intent firstIntent = new Intent(MainActivity.this,
After1.class);
startActivity(firstIntent);
break;
case 1:
Intent secondIntent = new Intent(MainActivity.this,
After2.class);
startActivity(secondIntent);
break;
default:
break;
}
}
答案 0 :(得分:1)
你需要一个例子,你可以遵循这个:
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ListFruitActivity extends ListActivity {
static final String[] FRUITS = new String[] { "Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
"Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// no more this
// setContentView(R.layout.list_fruit);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_fruit,FRUITS));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show(); //here instead of showing toast message you can start new activity using intent
}
});
}
}
开始新活动
Intent intent = new Intent(this,NewActivity.class);
startActivity(intent);
这里有一些可以帮助你的教程
http://www.mkyong.com/android/android-listview-example/
http://www.androidhive.info/2011/10/android-listview-tutorial/
答案 1 :(得分:0)
只有在string.xml中添加字符串数组时才会定义R.array。所以首先他们使用R.array.array_name
来引用它<强> string.xml 强>
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="string_array_name"> <item >text_string</item> </string-array></resources>
活动中的
getStringArray(R.array.string_array_name);