God
God

Reputation: 1248

Making my `CustomDialogFragment` generic

So i want to make my CustomArrayAdapter class generic.

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

It's not generic because of the String[] itemm = line. So instead of building a lot of classes that in each there will be a different String[] item, how do i make that generic?

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 class:

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>

I have put a lot of effort into this post. Please help me guys. Thanks.

EDIT

my activity layout:

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

Upvotes: 0

Views: 98

Answers (1)

Karakuri
Karakuri

Reputation: 38605

ArrayAdapter is already generic. Based on your current code, you can get rid of your entire CustomArrayAdapter class and simply use this constructor (replacing String with whatever type your items are):

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

If you plan to keep CustomArrayAdapter, you can declare it like this to keep the generics:

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

Naturally you would use T instead of String wherever necessary.

Upvotes: 1

Related Questions