Muhitun Azad Sohan Muhitun Azad Sohan - 4 months ago 159
Android Question

Using custom dialog box in custom listview button click

I am working on an android project where I have to fetch data from server and display it in a custom listview along with a button, I have fetched and showed the listview but I want to start a dialog box whenever the button in the each row of the listview is clicked. I am stucked on how to initiate the call from the button press, I have tried in some ways but doesn't work.. My codes are pasted below, any help would be greatly appreciated..

The contactAdapter class of my code is given below:

package com.example.sohan.doctor;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.FragmentManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.v4.app.DialogFragment;
import java.util.ArrayList;
import java.util.List;


/**
* Created by Sohan on 6/9/2016.
*/
public class ContactAdapter extends ArrayAdapter<Contacts> {
// notification nt = new notification();
List<Contacts> list = new ArrayList<Contacts>();
View row;
ContactHolder contactHolder;
LayoutInflater inflater;
View v;
private Activity activity;

public ContactAdapter(Context context, int resource) {
super(context, resource);
}



public void add(List<Contacts> updatedList) {
list.clear();
list.addAll(updatedList);
notifyDataSetChanged();
}


@Override
public Contacts getItem(int position) {
return list.get(position);
}

@Override
public int getCount() {
return list.size();
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
row = convertView;

if(row==null){
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.view_symptom_layout,parent,false);
contactHolder = new ContactHolder();
contactHolder.Name =(TextView) row.findViewById(R.id.textView2);
contactHolder.Age =(TextView) row.findViewById(R.id.textView3);
contactHolder.Height =(TextView) row.findViewById(R.id.textView4);
contactHolder.Weight =(TextView) row.findViewById(R.id.textView5);
contactHolder.Symptom =(TextView) row.findViewById(R.id.textView6);
contactHolder.button = (Button) row.findViewById(R.id.button3);
contactHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { // this is the onClick method for the custom button
String name;
// name = list.get(position).getName();
// Toast.makeText(getContext().getApplicationContext(), name+" is served ", Toast.LENGTH_LONG).show();
// Toast.makeText(getContext().getApplicationContext(), "Position of "+name+" is "+position, Toast.LENGTH_LONG).show();
callDialog cd = new callDialog();
cd.showBox();
//android.support.v4.app.FragmentManager fm = cd.getSupportFragmentManager();
//md.show(, "my_dialog");

//--------- another solution
// inflater = getActivity().getLayoutInflater();
// v = inflater.inflate(R.layout.custom_dialogbox, null);
// AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// builder.setView(v).setPositiveButton("Send Treatment", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialog, int which) {
//
// }
// });
// builder.show();
//---------- another solution
}
});

row.setTag(contactHolder);
}
else{

contactHolder = (ContactHolder)row.getTag();
}



Contacts contacts = (Contacts)this.getItem(position);
contactHolder.Name.setText("Name: "+contacts.getName());
contactHolder.Age.setText("Age: "+contacts.getAge());
contactHolder.Height.setText("Height: "+contacts.getHeight());
contactHolder.Weight.setText("Weight: "+contacts.getWeight());
contactHolder.Symptom.setText("Symptoms: " + contacts.getSymptom());
return row;

}

public Activity getActivity() {
return activity;
}

static class ContactHolder{
TextView Name;
TextView Age;
TextView Height;
TextView Weight;
TextView Symptom;
Button button;
}


class callDialog extends FragmentActivity {

public void showBox(){
MyDialog md = new MyDialog();
md.show(getSupportFragmentManager(), "my_dialog_box");
}
}



}


The dialog box code are given below:

package com.example.sohan.doctor;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;

/**
* Created by Sohan on 7/13/2016.
*/
public class MyDialog extends DialogFragment {

LayoutInflater inflater;
View v;
public Dialog onCreateDialog(Bundle savedInstanceState) {
inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.custom_dialogbox, null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v).setPositiveButton("Send Treatment", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
return builder.create();
}
}

Answer

Here is an Example of how to create Custom Dialog

 public void showCustomDialog() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inf = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inf.inflate(R.layout.email_dialog, null);
    builder.setView(view);
    final EditText et = (EditText) view.findViewById(R.id.id_student_email_edittext);
    final EditText nameEditText = (EditText) view.findViewById(R.id.id_student_name_edittext);
    final RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.student_gender_group);
    final RadioButton radiomale = (RadioButton) view.findViewById(R.id.gender_male);
    final RadioButton radioFemale = (RadioButton) view.findViewById(R.id.gender_female);

    builder.setPositiveButton("Ok", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            //Click listner
        }
    });
    builder.setNegativeButton("cancel", new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            //click listner.
        }
    });
    builder.show();
}

and this is the layout file for custom dialog.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dip">

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/id_student_name_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Name"
        android:inputType="textEmailAddress"
        android:padding="10dip"
        android:selectAllOnFocus="true" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/id_student_email_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Email"
        android:inputType="textEmailAddress"
        android:padding="10dip"
        android:selectAllOnFocus="true" />

</android.support.design.widget.TextInputLayout>

<RadioGroup
    android:layout_marginTop="10dip"
    android:id="@+id/student_gender_group"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:id="@+id/gender_male"
        android:text="Male"/>

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/gender_female"
        android:text="Female"
        android:layout_weight="1" />
</RadioGroup>
</LinearLayout>

Just call that method in onItemClick of listview

Customize the layout however you want and access elements as i showed in java code