Reputation: 3
I'd search days ago about this but unfortunately I couldn't find answer to my problem. I am currently working on an android project that when the you click a button, it will show you a dialog box with a list view of items from string array resources. Thanks you.
here is my string array:
<string-array name="heart_attacks_and_shock">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
here is my code:
import java.util.ArrayList;
import com.sap.BSMA.R.string;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
@SuppressLint("ParserError")
public class FirstaidActivity extends Activity {
Resources res = getResources();
String[] has = res.getStringArray(R.array.heart_attacks_and_shock);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstaid_layout);
ImageButton has_button = (ImageButton) findViewById(R.id.HAS);
has_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//set up dialog
Dialog dialog = new Dialog(FirstaidActivity.this);
dialog.setContentView(R.layout.firstaidcategoryoutput_layout);
dialog.setTitle("Heart attack and shock");
dialog.setCancelable(true);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,has);
setListAdapter(adapter);
dialog.show();
}
});
}
}
my xml for popup window:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:background="@drawable/mainbackground">
<ListView
android:id="@+id/listExample"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
android:choiceMode="singleChoice"
/>
</RelativeLayout>
Upvotes: 0
Views: 9009
Reputation: 2311
add this
Dialog dialog = new Dialog(FirstaidActivity.this);
dialog.setContentView(R.layout.firstaidcategoryoutput_layout);
ListView list = (ListView)dialog.findViewById(R.id.listExample);
dialog.setTitle("Heart attack and shock");
dialog.setCancelable(true);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (FirstaidActivity.this,android.R.layout.simple_list_item_1,has);
list.setAdapter(adapter);
dialog.show();
Upvotes: 2
Reputation: 2762
You can try changing:
android:id="@+id/listExample"
to
android:id="@android:id/list"
Upvotes: 0
Reputation: 3505
One of your problems is that the "this" object you are using is referring to the wrong thing inside your onclick method. Instead of this try Firstaidactivity.this to get the context for your activity
Second problem is that you cannot use setListAdapter inside a method unless the class extends a ListActivity. You need to get a reference to your listExample and then do list.setAdapter(adapter)
Upvotes: 0