【问题标题】:Making my `CustomDialogFragment` generic使我的“自定义 DialogFragment”通用
【发布时间】:2015-09-01 14:45:02
【问题描述】:

所以我想让我的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>

【问题讨论】:

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


    【解决方案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

    【讨论】:

    • 使用数组适配器的方法在listView.setAdapter() 行上给了我一个 NullPointerException。可能是因为我的活动中没有 ListView 视图?
    • 根据我看到的代码,R.layout.dialog 需要有一个 ListView 和android:id="@+id/dialog_list_view_id"
    • 但它是另一种布局。我的setContentView() 方法不包含ListView
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多