Android - 将自定义DialogFragment添加到ListView

时间:2016-07-28 17:13:27

标签: android listview android-fragments android-dialogfragment

已解决,请参阅我的回答以获取更多信息。

我一直在尝试在我的应用程序中添加一项功能,如果用户点击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();
    }
}

1 个答案:

答案 0 :(得分:0)

我明白了!

我需要做的就是将context转换为AppCompatActivity并从中获取支持片段。

Soo,当我们显示对话框时,我们现在:

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