我是安卓系统的新手,但我正在尽最大努力掌握它。我已经研究了大部分时间,我得出结论,我的实际适配器一定有问题。
不幸的是,当我加载对话框时,我没有填充列表视图。
我的目标
我想要一个对话框,它是一个包含列表视图的自定义布局。这已经创建,并已被证明可以与其他适配器一起使用。当列表视图出现时,我希望使用单选按钮自动选择一个选项。
自定义对话框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="wrap_content"
android:orientation="vertical"
android:background="@drawable/dialog_main_style">
<TextView
android:id="@+id/main_title_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:text="Format"
android:textColor="#020202"
android:textSize="20sp" />
<View
android:id="@+id/dialog_top_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/main_title_dialog"
android:layout_marginBottom="12dp"
android:background="#e4e4e4" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/format_choices_list_view"/>
<TextView
android:id="@+id/ok_change_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_gradient_button"
android:gravity="center"
android:layout_below="@id/dialog_top_divider"
android:padding="16dp"
android:text="OK"
android:textColor="#009261"
android:textStyle="bold" />
</LinearLayout>
这里没有什么特别的,只是引用了我的自定义布局和ListView。
单选按钮的自定义列表视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/format_radio_button"
android:text="mp3"
android:button="@null"
android:drawableRight="@android:drawable/btn_radio"
android:layout_marginRight="16dp"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" />
</LinearLayout>
通常单选按钮需要一个单选组,但由于这是一个自定义布局,我想使用它,似乎我只需要1个单选按钮?
创建对话框和适配器
private void showFormatChangeDialog() {
//TODO get the list view working in the dialog
Dialog changeFormatDialog = new Dialog(SettingsActivity.this);
changeFormatDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
changeFormatDialog.setContentView(R.layout.change_audio_format_dialog);
changeFormatDialog.getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
ListView formatListView = (ListView) changeFormatDialog.findViewById(R.id.format_choices_list_view);
AudioFormatAdapter adapter = new AudioFormatAdapter(SettingsActivity.this);
formatListView.setAdapter(adapter);
changeFormatDialog.show();
}
我创建了一个对话框,并将其设置为我的自定义对话框布局。然后我得到了一个列表视图的参考,并装箱一个适配器并设置它
FormatAdapter
public class AudioFormatAdapter extends ArrayAdapter<String> {
static class ViewHolder {
RadioButton radioButton;
}
private String formatNames[] = {"mp3", "wav","amr"};
public AudioFormatAdapter(Context c) {
super(c, 0);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View formatView = convertView;
ViewHolder holder;
if(formatView == null) {
formatView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_formats_layout, parent, false);
holder = new ViewHolder();
holder.radioButton = (RadioButton) formatView.findViewById(R.id.format_radio_button);
holder.radioButton.setText(formatNames[position]);
formatView.setTag(holder);
} else {
holder = (ViewHolder)formatView.getTag();
}
return formatView;
}
}
我在这里的目标是为单选按钮获取对我的自定义列表视图的引用。我获取单选按钮的引用,并根据位置将文本设置为我的数组值。
我似乎没有从这个ArrayAdapter中得到任何数据,这让我大吃一惊。自从我开始使用以来,我已经使用了几个ArrayAdapter
,而且我从未遇到过这种问题。当我把这个适配器换成另一个适配器时,它就工作了。我不确定这里的问题是什么。
如有任何帮助,我们将不胜感激。
谢谢。
好吧,它修复了我想要的,我认为这归结为适配器故障。现在我知道我的适配器没有ViewHolder,但它有有限的选项,所以现在还可以。
首先,我需要一个允许CheckableLinearLayout的类。这是一种特殊类型的LinearLayout,允许检查和取消检查布局的部分。您只需将其创建一个新类,然后将其作为根视图添加到XML文件中。com.YOUR-IDENTIFIER-CLASS-NAME-OF-CHECKABLELINEARLAYOUT:(你可以在下面的链接中看到它!
可检查线性布局-在这里找到-如何使用单选按钮和单选项创建自定义列表视图项目
在使用了那个类之后,我制作了一个不同的适配器,它更简单了,而且似乎可以工作。
适配器
public class AudioFormatAdapter extends ArrayAdapter<String> {
private ArrayList<String> formats;
public AudioFormatAdapter(Context c){
super(c, 0);
}
public AudioFormatAdapter(Context c, ArrayList<String> choices){
super(c, 0, choices);
formats = choices;
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
View listItemView = convertView;
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.list_view_formats_layout, parent, false);
CheckedTextView _radioButton = (CheckedTextView) listItemView.findViewById(R.id.format_radio_button);
_radioButton.setText(formats.get(position));
return listItemView;
}
}
正如我们所看到的,这个适配器得到了一个"CheckedTextView"而不是单选按钮。原因是我只需要一个带有一些文本的单选按钮样式,这就给了我这一点。
我用arrayList设置文本,并以"formats"将其传递给适配器
最后,我创建了对话框,并获得了对列表视图的引用。
使用自定义布局创建对话框
private void showFormatChangeDialog(ArrayList<String> formatNames) {
LayoutInflater inflater = LayoutInflater.from(SettingsActivity.this);
View dialogLayout = inflater.inflate(R.layout.change_audio_format_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(SettingsActivity.this);
builder.setView(dialogLayout);
final ListView formatListView = (ListView) dialogLayout.findViewById(R.id.format_choices_list_view);
formatListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
final AudioFormatAdapter adapter = new AudioFormatAdapter(SettingsActivity.this, formatNames);
formatListView.setAdapter(adapter);
AlertDialog customDialog = builder.create();
customDialog.show();
}
如果只希望选择其中一个选项,则将模式设置为SINGLE_CHOICE。我在这里制作适配器,并传递一个字符串列表,这些字符串被接受为函数的参数。然后创建对话框
结论
这是一个相当令人恼火的过程,因为我认为我的适配器破坏了大多数东西,我希望我在互联网上搜索并找出一些零碎的东西可以帮助其他人。现在我只需要根据选择的选项来保存和存储数据!
我希望这能帮助到别人!