Skip to content Skip to sidebar Skip to footer

How Can I Link A Listview To An AlertDialog

I have created a seperate ListView Class which will show the name of the company.Now I want to pass this listview to the AlertDialog on Main Activity. I have created a list layout

Solution 1:

Just create a custom dialog as below :

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

or see example below :

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

}

Solution 2:

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

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

create custom layout 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>

Solution 3:

If your purpose is show a list inside dialog, please read alert dialog document: Alert dialog

Builder class of Alert dialog have many method to implement a list:

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

Solution 4:

You can create a simple list alert dialog with following code...

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

Try BaseAdapter like this.

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

If you need more info about this. Please comment below...Happy coding...


Solution 5:

In your main activity write:

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

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

  }
}

Do not forget to add all needed activities to manifest.


Post a Comment for "How Can I Link A Listview To An AlertDialog"