gpt4 book ai didi

java - 使我的 `CustomDialogFragment` 通用

转载 作者:行者123 更新时间:2023-12-01 11:11:18 26 4
gpt4 key购买 nike

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

自定义对话框 fragment 非通用:

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[]项,我如何使其通用?

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>

我为这篇文章付出了很多努力。请帮助我。谢谢。

编辑

我的 Activity 布局:

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

最佳答案

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

关于java - 使我的 `CustomDialogFragment` 通用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32334591/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com