0

已解决,请参阅我的答案以获取更多信息。Android - 将自定义DialogFragment添加到ListView中


我一直想在我的应用程序中添加功能,其中,如果用户点击一个ListView的项目,它会显示自定义DialogFragment。

问题是我找不到有关如何实际显示此对话框的文档或答案。我试过fragment.show(getFragmentManager, "dialog")甚至fragment.show(Activity.getFragmentManager, "dialog")和变化像AppCompatActivity.getSupportedFragmentManager(我的父母活动从AppCompatActivity延伸)。

如果有解决方案只使用视图/上下文这将是完美的!

我会粘贴我的代码波纹管,我希望它可以有一定的用处,谢谢!

PS:对不起,糟糕的代码

public class OSArrayAdapter extends ArrayAdapter<ServiceOrder> 
{ 
    private Context     context; 
    private TextView    statusTextView; 
    private ImageView    directionsButton; 
    private ArrayList<String>  statuses; 
    private ArrayList<String>  addresses; 
    private ArrayList<ServiceOrder> infos; 

    public OSArrayAdapter(Context context, int resource, ArrayList<ServiceOrder> infos) 
    { 
     // Constructor 
     super(context, resource, infos); 
     this.context = context; 
     this.addresses = new ArrayList<>(); 
     this.statuses = new ArrayList<>(); 
     this.infos  = infos; 
     // set values for the objects 
     for(ServiceOrder info : this.infos) { 
      this.addresses.add(info.getAddress()); 
      this.statuses.add(info.getStatus()); 
     } 
    } 

    public View getView(final int position, View currentView, ViewGroup parent) 
    { 
     // Called when rendering the list 
     // Get property we're displaying 
     String address = addresses.get(position); 
     String status = statuses.get(position); 

     // Get the inflater and inflate the XML for it 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(
       Activity.LAYOUT_INFLATER_SERVICE); 
     View view = inflater.inflate(R.layout.os_list_view, null); 

     TextView addressTextView = (TextView) view.findViewById(R.id.address_text_view); 
     statusTextView   = (TextView) view.findViewById(R.id.status_text_view); 
     directionsButton   = (ImageView) view.findViewById(R.id.image_button_goto_map); 

     // Setting address in the text view 
     // Display "..." trimming the address if it's too long 
     if(address.length() > 34) { 
      address = address.substring(0, 30) + "..."; 
     } 
     addressTextView.setText(address); 

     // Setting the status in the text view 
     try { 
      setStatus(status); 
     } catch(InvalidOptionException e) { 
      e.printStackTrace(); 
     } 

     // Create and set the listener for the layout itself 
     view = createLayoutAndSetListener(view, address, "Severino de Maria", 
              "Problema no controle", "1234"); 

     directionsButton.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       Intent intent = new Intent(context, MapsActivity.class); 
       intent.putExtra("position", position); 
       context.startActivity(intent); 
      } 
     }); 

     // Finally, we return it! 
     return view; 
    } 

    private View createLayoutAndSetListener(View view, final String address, final String clientName, 
              final String serviceType, final String serviceCode) 
    { 
     view.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       // Define the dialog's properties 
       ServiceOrderInformationDialog dialog = ServiceOrderInformationDialog 
         .newInstance(address, clientName, serviceType, serviceCode); 
       dialog.show(getSupportFragmentManager(), "Informações"); 
      } 
     }); 
     return view; 
    } 

    @Override 
    public ServiceOrder getItem(int position) 
    { 
     return infos.get(position); 
    } 

    @TargetApi(Build.VERSION_CODES.KITKAT) 
    public void setStatus(String status) throws InvalidOptionException 
    { 
     Resources cResources = context.getResources(); 
     if(Objects.equals(status, cResources.getString(R.string.terminado))) { 
      statusTextView.setText(status); 
      statusTextView.setTextColor(cResources.getColor(R.color.doneGreen)); 
     } else if(Objects.equals(status, cResources.getString(R.string.pendente))) { 
      statusTextView.setText(status); 
      statusTextView.setTextColor(cResources.getColor(R.color.colorVivid)); 
     } else { 
      throw new InvalidOptionException(); 
     } 
    } 
} 

而定制DialogFragment

public class ServiceOrderInformationDialog extends BaseDialogFragment<ServiceOrderInformationDialog> 
{ 

    public static ServiceOrderInformationDialog newInstance(String address, String clientName, 
                  String serviceType, String serviceCode) 
    { 
     // This is what we should use to create new dialogs, it'll let us set the values for 
     // the text fields (TextView) in our dialog 
     ServiceOrderInformationDialog frag = new ServiceOrderInformationDialog(); 
     Bundle args = new Bundle(); 
     args.putString("address", address); 
     args.putString("clientName", clientName); 
     args.putString("serviceType", serviceType); 
     args.putString("serviceCode", serviceCode); 
     frag.setArguments(args); 
     return frag; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

     // Getting the Layout Inflater 
     LayoutInflater inflater = getActivity().getLayoutInflater(); 

     // Get our view 
     View view = inflater.inflate(R.layout.dialog_service_order_information, null); 

     // Inflate the layout and set its design to the one we made 
     // Pass null as the parent view because it's going in the dialog layout 
     builder.setView(view); 

     // Set text values 
     String address = getArguments().getString("address", null); 
     String clientName = getArguments().getString("clientName", null); 
     String serviceType = getArguments().getString("serviceType", null); 
     String serviceCode = getArguments().getString("serviceCode", null); 

     // Defining the Text View fields 
     TextView addressTextView = (TextView) 
       view.findViewById(R.id.advanced_address_dialog); 
     TextView clientNameTextView = (TextView) 
       view.findViewById(R.id.advanced_client_name_dialog); 
     TextView serviceTypeTextView = (TextView) 
       view.findViewById(R.id.advanced_service_type_dialog); 
     TextView serviceCodeTextView = (TextView) 
       view.findViewById(R.id.advanced_service_code_dialog); 

     // Set text view values 
     addressTextView.setText(address); 
     clientNameTextView.setText(clientName); 
     serviceTypeTextView.setText(serviceType); 
     serviceCodeTextView.setText(serviceCode); 

     // return the created dialog 
     return builder.create(); 
    } 
} 
+0

怎么样使用https://developer.android.com/reference/android/app/DialogFragment.html。在文档中,他们还展示了一个例子如何去做 –

回答

0

我知道了!

我所需要做的就是将context作为AppCompatActivity,并从中获得支持片段。

洙是显示对话框时,我们现在有:

dialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "Informações); 
相关问题