【发布时间】:2017-07-12 11:24:01
【问题描述】:
我创建了一个单独的 ListView 类,它将显示公司的名称。现在我想将此列表视图传递给 Main Activity 上的 AlertDialog。我创建了一个列表布局和一个文本布局来显示列表的项目。现在我为 Alertdialog 创建了另一个布局。我想在第一页上按下按钮时显示此对话框。如何在此 Alerdialog 中传递 listView。这是我的主要活动代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.option_menu_laout);
Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog();
}
});
}
public void showDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.diallog_layout, null);
builder.setView(dialogView);
builder.setCancelable(true);
builder.setTitle("Contact");
ListViewActivity listViewActivity=new ListViewActivity();
//how can I show li list item here?
}
}
我的 ListViewActivityClass 是
public class ListViewActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
populateListView();
}
private void populateListView() {
//Create list of items
String[] company= getResources().getStringArray(R.array.company_name);
//Build Adapter
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this, //Context for the Activity
R.layout.first_alertlist_textstyle,
android.R.id.text1,//Layout to use
company); //Items to be displayed
//Configure the list view
ListView companyList =(ListView) findViewById(R.id.list_view);
TextView alertTitle = (TextView) findViewById(R.id.title);
companyList.setAdapter(adapter);
}
}
我的列表布局是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
/>
</LinearLayout>
显示列表项的文本布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center"
/>
</LinearLayout>
对话框布局是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Contact"
android:gravity="center_horizontal"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:layout_gravity="center"
/>
</LinearLayout>
编辑代码
public class MainActivity extends AppCompatActivity {
ArrayList<Bean> bean=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.option_menu_laout);
Button button=(Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Alert();
Toast.makeText(MainActivity.this, "Dialog", Toast.LENGTH_SHORT).show();
}
});
}
private void Alert(){
ArrayList<Bean> bean=new ArrayList<>();
View view = getLayoutInflater().inflate(R.layout.rating_dialog_layout, null);
ListView details = (ListView) view.findViewById(R.id.ratingListViewId);
Adapter adapter = new Adapter(bean,getApplicationContext());
details.setAdapter(adapter);
final AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select");
builder.setView(view);
builder.setCancelable(false);
builder.setNegativeButton("close", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
details.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> adapterView, View view, final int i, long l) {
;
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
【问题讨论】:
标签: android listview dialog android-alertdialog