0

Gelöst, finden Sie in meiner Antwort für weitere Informationen.Android - Hinzufügen benutzerdefinierter DialogFragment zu ListView


Ich habe versucht, eine Funktionalität auf meiner Anwendung hinzuzufügen, wo, wenn der Benutzer auf ein Element eines Listview klickt wird es eine benutzerdefinierte DialogFragment zeigen.

Das Problem ist, dass ich nicht finden kann, Dokumentation oder Antworten darüber, wie dieser Dialog angezeigt wird. Ich habe versucht fragment.show(getFragmentManager, "dialog") und sogar fragment.show(Activity.getFragmentManager, "dialog") und Variationen wie 10 (meine übergeordnete Aktivität erstreckt sich von AppCompatActivity).

Wenn es eine Lösung dafür gibt, nur Ansichten/Kontext zu verwenden, wäre es perfekt!

Ich werde meinen Code einfügen, ich hoffe, es kann von etwas Nutzen sein, danke!

PS: Sorry für die schlechten Code

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

und die benutzerdefinierte 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

was über die Verwendung https://developer.android.com/reference/android/app/DialogFragment.html. In der Dokumentation zeigen sie auch ein Beispiel, wie es geht –

Antwort

0

ich es!

Alles, was ich tun musste, war die context als AppCompatActivity zu werfen und das Support-Fragment von diesem zu bekommen.

Soo, dass, wenn der Dialog zeigen wir jetzt haben:

dialog.show(((AppCompatActivity) context).getSupportFragmentManager(), "Informações);