-4

这是我的代码,我在这一行遇到错误:

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;
                }

            }
4

2 回答 2

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/

于 2013-03-28T07:09:40.707 回答
0

只有当您在 string.xml 中添加字符串数组时,才会定义 R.array。所以首先他们使用 R.array.array_name 引用它

字符串.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);

于 2013-03-28T07:10:31.507 回答