gpt4 book ai didi

android - 如何将 Listview 链接到 AlertDialog

转载 作者:行者123 更新时间:2023-11-29 02:39:42 24 4
gpt4 key购买 nike

我已经创建了一个单独的 ListView 类,它将显示公司的名称。现在我想将此 ListView 传递给 Main Activity 上的 AlertDialog。我创建了一个列表布局和一个文本布局来显示列表项。现在我已经为 Alertdialog 创建了另一个布局。我想在第一页上按下按钮时显示此对话框。我怎样才能在这个 Alerdialog 中传递 listView。这是我的主要 Activity 代码

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

最佳答案

只需创建一个自定义对话框,如下所示:

http://www.edumobile.org/android/custom-listview-in-a-dialog-in-android/

或者看下面的例子:

private void showDialog(){

final Dialog dialog = new Dialog(this);

View view = getLayoutInflater().inflate(R.layout.dialog_main, null);

ListView lv = (ListView) view.findViewById(R.id.custom_list);

// Change MyActivity.this and myListOfItems to your own values
CustomListAdapterDialog clad = new CustomListAdapterDialog(MyActivity.this, myListOfItems);

lv.setAdapter(clad);

lv.setOnItemClickListener(........);

dialog.setContentView(view);

dialog.show();

}

关于android - 如何将 Listview 链接到 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45056335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com