使用自定义列表视图按钮中的自定义对话框单击

时间:2016-07-13 12:03:55

标签: android

我正在开发一个Android项目,我必须从服务器获取数据并将其显示在自定义列表视图中以及一个按钮,我已经获取并显示了列表视图但我想在每次按钮时启动一个对话框单击列表视图的每一行。我一直坚持如何通过按钮按下来启动呼叫,我已尝试过某些方式但不起作用..我的代码粘贴在下面,任何帮助将不胜感激..

我的代码的contactAdapter类如下:

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



}

对话框代码如下:

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

4 个答案:

答案 0 :(得分:0)

试试这个:

根据需要自定义警报。

contactHolder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { 
AlertDialog.Builder alertbox = new AlertDialog.Builder(v.getRootView().getContext());
    alertbox.setMessage("Message");
    alertbox.setTitle("Title");
    alertbox.setIcon(R.drawable.img);

    alertbox.setPositiveButton("Send Treatment",
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface arg0,
                        int arg1) {

                }
            });
  alertbox.show();
  }
            });

答案 1 :(得分:0)

   mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showDialog();
            }
        });


private void showDialog()
    {
        AlertDialog.Builder builder =
                new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
        builder.setTitle("No Connection");
        builder.setMessage("check your internet connection");
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
               //do it here
            }
        });
        builder.show();
    }

您可以使用listview项目点击事件。并将其显示为弹出。

答案 2 :(得分:0)

以下是如何创建自定义对话框

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

这是自定义对话框的布局文件。

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

只需在listview的onItemClick中调用该方法

根据需要自定义布局并访问我在java代码中显示的元素

答案 3 :(得分:0)

callDialog cd = new callDialog();

不要使用new创建FragmentActivity。

使用意图。

相关问题