使我的`CustomDialogFragment`成为通用的

时间:2015-09-01 14:45:02

标签: java android generics android-fragments android-arrayadapter

所以我想让我的CustomArrayAdapter类通用。

CustomDialogFragmentNotGeneric:

public class CustomDialogFragment extends DialogFragment
{
    TextView listViewItemTextView;
    ArrayAdapter<String> arrayAdapter;
    ListView dialogListView;
    String[] items = {"Hello","Hello there","Hi","Hi there"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
        View rootView = inflater.inflate(R.layout.dialog, container,false);

        getDialog().setTitle("Choose an option"); // Set dialog title

        listViewItemTextView = (TextView) rootView.findViewById(R.id.list_view_item_text_view_id);
        dialogListView = (ListView) rootView.findViewById(R.id.dialog_list_view_id);

        getDialog().setTitle("Opening Words"); // Setting dialog title


        CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(getActivity(), items);
        dialogListView.setAdapter(customArrayAdapter);

        dialogListView.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,int position, long id)
            {
                Toast.makeText(getActivity(), items[position], Toast.LENGTH_SHORT).show();
            }
        });

        return rootView;
    }
}

由于String[] itemm =行,它不是通用的。 因此,不是构建很多的类,而是在每个类中都有不同的String[] item,我该如何制作那个通用的?

dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView 
    android:id="@+id/dialog_list_view_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

MyCustomArrayAdapter类:

package com.example.predesignedmails;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class CustomArrayAdapter extends ArrayAdapter<String>
{
    Context context;
    String[] items;
    LayoutInflater layoutInflater;

    public CustomArrayAdapter(Context context, String[] items) 
    {
        super(context, R.layout.list_view_row,items);

        this.context = context;
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        if (convertView == null)
        {
            this.layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = this.layoutInflater.inflate(R.layout.list_view_row,parent,false);
        }

        TextView rowTextView = (TextView) convertView.findViewById(R.id.row_text_view_id);

        rowTextView.setText(this.items[position]);

        return convertView;
    }
}

list_view_row.xml:

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

<TextView
    android:id="@+id/row_text_view_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />

</LinearLayout>

我在这篇文章中付出了很多努力。请帮帮我们。感谢。

修改

我的活动布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.predesignedmails.LoveMailsActivity" >

<TextView
    android:id="@+id/love_email_emai_to_send_to_text_view_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/emai_to_send_to_text_view_text"
    android:textSize="16sp"
    android:textStyle="bold"
    android:textColor="@color/opening_words_list_view_header_color"
    />

<EditText
    android:id="@+id/love_email_email_to_send_to_edit_text_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/emai_to_send_to_edit_text_hint"
    android:inputType="textEmailAddress"
    android:textSize="18sp"
    android:textColor="@color/selection_text_color"
    android:layout_below="@id/love_email_emai_to_send_to_text_view_id"
    />

<TextView
    android:id="@+id/love_email_opening_words_header_text_view_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/love_email_opening_words_text_view_text"
    android:textSize="16sp"
    android:textStyle="bold"
    android:textColor="@color/opening_words_list_view_header_color"
    android:layout_below="@id/love_email_email_to_send_to_edit_text_id"
    />

<TextView
    android:id="@+id/love_email_opening_words_text_view_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/initial_text"
    android:textSize="18sp"
    android:textColor="@color/selection_text_color"
    android:layout_below="@id/love_email_opening_words_header_text_view_id"
    />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

ArrayAdapter已经是通用的。根据您当前的代码,您可以删除整个CustomArrayAdapter类,只需使用此构造函数(将String替换为您的项目类型):

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        R.layout.list_view_row, R.id.row_text_view_id, items);

如果您打算保留CustomArrayAdapter,可以将此声明为保留泛型:

public class CustomArrayAdapter<T> extends ArrayAdapter<T> {
    ...
}

当然,您可以在必要时使用T代替String