Ajay Bhat
Ajay Bhat

Reputation: 11

Populate a listView from string resources in Android

I am trying to populate a listview from the string resources defined in the string XML file in Android. I am not getting any errors but when I run the application it asks me to force close. I am getting the following error in log cat manager 03-26 01:04:42.903: E/dalvikvm(205): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

Here is the code for my program

public class MainMenu extends Activity {
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

    /*ListView menu = (ListView) findViewById(R.id.ListView_Menu);

    String[] items = { getResources().getString(R.string.today_bd),getResources().getString(R.string.Add_bd),
            getResources().getString(R.string.msg_tmplate),getResources().getString(R.string.credits),
            getResources().getString(R.string.help)
    };

    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.menu,items);

    menu.setAdapter(adapt);*/
    ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
    String[] items = { getResources().getString(R.string.today_bd),
            getResources().getString(R.string.add_bd),
            getResources().getString(R.string.msg_tmplate),
            getResources().getString(R.string.credits),
            getResources().getString(R.string.help)
            };

    ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu, items);
    menuList.setAdapter(adapt);
    menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {

            TextView textView = (TextView) itemClicked;
            String strText = textView.getText().toString();
            if (strText.equalsIgnoreCase(getResources().getString(R.string.today_bd))) {

                //startActivity(new Intent(QuizMenuActivity.this, QuizGameActivity.class));
            } else if (strText.equalsIgnoreCase(getResources().getString(R.string.add_bd))) {
                // Launch the Help Activity
                //startActivity(new Intent(QuizMenuActivity.this, QuizHelpActivity.class));
            } else if (strText.equalsIgnoreCase(getResources().getString(R.string.msg_tmplate))) {
                // Launch the Settings Activity
                //startActivity(new Intent(QuizMenuActivity.this, QuizSettingsActivity.class));
            } else if (strText.equalsIgnoreCase(getResources().getString(R.string.credits))) {
                // Launch the Scores Activity
               // startActivity(new Intent(QuizMenuActivity.this, QuizScoresActivity.class));
            }
            else if (strText.equalsIgnoreCase(getResources().getString(R.string.help))) {
                // Launch the Scores Activity
               // startActivity(new Intent(QuizMenuActivity.this, QuizScoresActivity.class));
            }
        }


        //public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                //long arg3) {
            // TODO Auto-generated method stub

        //}
    });




}

}

Please help!!!

Upvotes: 1

Views: 2349

Answers (2)

Ajay Bhat
Ajay Bhat

Reputation: 11

I tried with the method given above and it didn't seem to work. Here is my menu.xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:layout_weight="0.00" >

     <TextView
        android:id="@+id/TextView01"
        android:layout_height="wrap_content"
        android:text="@string/main_menu"
        android:textSize="@dimen/screen_title_size"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="10"
        android:layout_width="wrap_content"
        android:layout_gravity="fill_horizontal|center"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:shadowColor="@android:color/white"
        android:textColor="@color/title_color"></TextView>
</RelativeLayout>

<ListView
    android:id="@+id/listView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.00" >
</ListView>

Upvotes: 0

dldnh
dldnh

Reputation: 8951

the ArrayAdapter constructor you're calling takes three arguments -- you've got the first and third right, but the second one wants to be the item layout.

textViewResourceId The resource ID for a layout file containing a TextView to use when instantiating views.

try using android.R.layout.simple_list_item_1 and that should help...

ArrayAdapter<String> adapt = 
  new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

ref: javadoc

Upvotes: 2

Related Questions