我想在对话框中使用listview,我已经在xml文件中设置了listview,但是我不熟悉怎么使用它(我想用string array填充listview中的值)。
main java:
package custom.dialouge.list;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class CustomDialougeListTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context mContext = this;
final Context context = this;
Button button;
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("List");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Test");
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
custom dialouge .xml
<?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">
<Button
android:id="@+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Back" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/Button01"
android:layout_alignParentLeft="true"
android:layout_below="@+id/TextView01" >
</ListView>
</RelativeLayout>
在一个自定义的对话框中如何使用一个list?
不是很明白你的需求,你是不懂listview怎么不用呢,还是不懂怎么自定义listview?
怎么自定义listview的话就需要有一个listview的布局文件,这个布局文件控制你这个自定义listview的每一个item的样式。至于你要怎么设计就是你自己的问题了
如果是怎么用自定义的listview的话,那看你对这个listview的功能需求,如果要求复杂度高点的话就要自己写个适配器,然后给这个控件绑定那就行了,否则的话可以直接调用listview的一些自带适配器
是想放什么样的列表呢?只是一个菜单?如果是菜单,它已提供一个方法,只要设置一个字符串数组的资源ID就可以了。但如果是一个自定义的listview可以试试使用setcontentview这个方法把自定义的内容放上去。
请使用:
ArrayList<String> myStringArray = ... // insert your code to get/create the array here
ListView view = dialog.findViewById(R.id.listview1);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, myStringArray);
view.setAdapter(listAdapter);
比如:
public class CustomDialougeListTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Context mContext = this;
final Context context = this;
Button button;
ArrayList<String> myStringArray = ... // insert your code to get/create the array here
ArrayAdapter<String> listAdapter = new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, myStringArray);
button = (Button) findViewById(R.id.button01);
// add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.list);
dialog.setTitle("List");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.TextView01);
text.setText("Test");
ListView view = dialog.findViewById(R.id.listview1);
view.setAdapter(listAdapter);
Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}