我创建了一个单独的ListView类,它将显示该公司的名称。现在我想将此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();
}
}
答案 0 :(得分:0)
只需创建一个自定义对话框,如下所示:
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();
}
答案 1 :(得分:0)
call this on Button Click
private void Alert(){
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);
android.support.v7.app.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();
}
});
detailsbtn.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(final AdapterView<?> adapterView, View view, final int i, long l) {
alertdialog.dismiss();
}
});
alertdialog = builder.create();
alertdialog.show();
}
use Bean Class
public class Bean {
public Bean(String _id, String description) {
this._id = _id;
this.description = description;
}
String _id;
String description;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
use Adapter
public class Adapter extends BaseAdapter {
Context context;
List<Bean> list=new ArrayList<Bean>();
public Adapter(ArrayList<Bean> list, Context context) {
this.context = context;
this.list = list;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=LayoutInflater.from(context);
View v=inflater.inflate(R.layout.rating_layout_row,null);
TextView idTv= (TextView) v.findViewById(R.id.id);
TextView nameTv= (TextView) v.findViewById(R.id.name);
idTv.setText(String.valueOf(list.get(position)._id()));
nameTv.setText(list.get(position).getDescription());
return v;
}
}
创建rating_layout_row
<?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"
android:padding="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/id"
android:textColor="#000000"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/name"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
创建自定义布局rating_dialog_layout
<?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
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ratingListViewId"
android:layout_gravity="center_horizontal" />
</LinearLayout>
答案 2 :(得分:0)
您可以使用以下代码创建一个简单的列表提醒对话框...
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext);
alertDialogBuilder.setTitle("Awesome alert")
.setCancelable(false)
.setAdapter(new NotificationListAdapter(mContext, listItems), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// on item click...
dialog.cancel();
}
})
.setPositiveButton(getString(R.string.close), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
尝试像这样的BaseAdapter。
public class NotificationListAdapter extends BaseAdapter {
private Context mContext;
private List<NotificationModel> notificationModels;
public NotificationListAdapter(Context mContext, List<NotificationModel> notificationModels) {
this.mContext = mContext;
this.notificationModels = notificationModels;
}
@Override
public int getCount() {
return notificationModels.size();
}
@Override
public NotificationModel getItem(int i) {
return notificationModels.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
NotificationViewHolder notificationViewHolder;
if (view == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
view = mInflater.inflate(R.layout.item_notification, null);
notificationViewHolder = new NotificationViewHolder();
notificationViewHolder.item_view = (RelativeLayout) view.findViewById(R.id.notification_item);
notificationViewHolder.textView_notification = (TextView) view.findViewById(R.id.tv_notification);
notificationViewHolder.textView_time = (TextView) view.findViewById(R.id.tv_notification_time);
view.setTag(notificationViewHolder);
} else {
notificationViewHolder = (NotificationViewHolder) view.getTag();
}
NotificationModel notificationModel = getItem(position);
// UI Update...(Data binding)
return view;
}
private class NotificationViewHolder {
RelativeLayout item_view;
TextView textView_notification, textView_time;
}
}
如果您需要更多有关此信息。请在下面评论......快乐的编码...
答案 3 :(得分:0)
如果您的目的是在对话框中显示列表,请阅读警告对话框文档:Alert dialog
Alert类的Alert对话框有很多实现列表的方法:
builder.setItems for list string
builder.setMultiChoiceItems
builder.setSingleChoiceItems
答案 4 :(得分:0)
在您的主要活动中写:
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) {
openCompaniesActivity()
}
});
}
public void openCompaniesActivity()(){
startActivity(new Intent(this, ListViewActivity.class));
}
}
您的ListViewActivity:
public class ListViewActivity extends Activity{
String[] company;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
populateListView();
}
private void populateListView() {
//Create list of items
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);
companyList.setAdapter(adapter);
companyList.setOnItemsClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedCompany = company[position];
//Create you dialog, depending from selected company.
AlertDialog.Builder builder = new AlertDialog.Builder(ListViewActivity.this);
LayoutInflater inflater = ListViewActivity.this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.diallog_layout, null);
builder.setView(dialogView);
builder.setCancelable(true);
builder.setTitle("Contact");
builde.show();
}
})
}
}
不要忘记将所有需要的活动添加到清单。