2016-07-13 38 views
-3

我正在从事一个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(); 
    } 
} 
+0

您的适配器类中的代码如何?这不正常吗? – Jas

+0

当按下contactHolder.button时,应用程序崩溃,其余部分正在工作,所有其他数据都与按钮一起完美显示。 –

+0

发布您的崩溃日志 – Jas

回答

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> 

只是调用这个方法在onItemClick列表视图

的,但是你想自定义布局和访问元素,因为我在Java代码中

+0

在getSystemService中显示错误... –

+0

Thnx它的工作,:-)它将是getContext()。getSystemService(.... ...).. –

+0

这是因为我在Activity类 ,如果你在片段中,你可能不得不使用getActivity() – Jayanth

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

这可以工作,但我有一个自定义对话框,我该如何使用它?您的上述解决方案弹出一个窗口,我需要在alertbox中编辑文本和文本视图。 –

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

您可以使用列表视图项click事件。而表现出来弹出。

0
callDialog cd = new callDialog(); 

表明不要创建新的一个FragmentActivity。

使用意图。

相关问题