studio personalizado full custom android listview dialog android-alertdialog

android - personalizado - ¿Cómo puedo vincular una Listview a un AlertDialog?



dialog full screen android (5)

Creé una clase ListView separada que mostrará el nombre de la empresa. Ahora quiero pasar esta lista a AlertDialog en la actividad principal. He creado un diseño de lista y un diseño de texto para mostrar el elemento de la lista. Ahora he creado otro diseño para Alertdialog. Me gustaría mostrar este cuadro de diálogo al presionar un botón en la primera página. ¿Cómo puedo pasar el listView dentro de este Alerdialog? Aquí está mi código para la actividad principal

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? } }

Mi ListViewActivityClass es

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

El diseño de mi lista es

<?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>

Diseño de texto para mostrar el elemento de la lista

<?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>

Dialog Layout is

<?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>

Editar código

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

}


En tu actividad principal escribe:

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

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

No olvides agregar todas las actividades necesarias para manifestar.


Puede crear un diálogo de alerta de lista simple con el siguiente código ...

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

Pruebe BaseAdapter de esta manera.

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

Si necesitas más información sobre esto Por favor comente abajo ... Feliz codificación ...


Si su propósito es mostrar una lista dentro del cuadro de diálogo, lea el documento de diálogo de alerta: Diálogo de alerta

La clase de generador de diálogo de Alerta tiene muchos métodos para implementar una lista:

builder.setItems for list string builder.setMultiChoiceItems builder.setSingleChoiceItems


Simplemente cree un cuadro de diálogo personalizado de la siguiente manera:

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

o vea el siguiente ejemplo:

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


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

crear 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>

crear diseño personalizado 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>