带有自定义适配器的警报对话框列表视图不显示任何内容
alertdialog listview with custom adapter shows nothing
我在警报对话框中使用列表视图,但是当对话框显示时,它没有显示任何内容。该对象不为空,因为我正在填充它并将其添加到警报对话框之前的数组列表中。这是我的 xml 和 类 的代码:
我在哪里设置适配器:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select a match");
//insert array to constructor
LayoutInflater inflater = getLayoutInflater(savedInstanceState);
View dialogLayout = inflater.inflate(R.layout.dialoglist, null);
matchAdapter testAdapter = new matchAdapter(getContext(), userInfos);
ListView listView = (ListView) dialogLayout.findViewById(R.id.dialogListView);
listView.setAdapter(testAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do something
}
});
builder.setView(dialogLayout);
AlertDialog alert = builder.create();
alert.show();
自定义适配器:
class matchAdapter extends ArrayAdapter<userInfo> {
public matchAdapter(Context context, ArrayList<userInfo> test) {
super(context, R.layout.match_result, test);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customRow = inflater.inflate(R.layout.match_result, parent, false);
userInfo singleItemTest = getItem(position);
/*
TODO: get references to layout elements
*/
TextView username = (TextView) customRow.findViewById(R.id.matchUsername);
TextView sex = (TextView) customRow.findViewById(R.id.matchSex);
TextView age = (TextView) customRow.findViewById(R.id.matchAge);
Button sendRequest = (Button) customRow.findViewById(R.id.matchSendRequest);
username.setText(singleItemTest.getUserName());
return customRow;
}}
row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/match_result"
android:orientation="horizontal"
android:padding="16dp"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="left">
<TextView
android:id="@+id/matchUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/matchSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sex"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/matchAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/matchSendRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Request"
android:minHeight="5dp"
android:minWidth="0dp"
android:layout_marginLeft="80dp"/>
dialoglist.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialogListView"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</ListView>
在您的 Adapter 中添加一个对象 ArrayList<userInfo> infos
现在在构造函数中使用这个
public matchAdapter(Context context, ArrayList<userInfo> test) {
super(context, R.layout.match_result, test);
infos=test;
}
现在 getView
回调将像这样:
userInfo singleItemTest = infos.get(position);
我想这会解决你的问题:)
迈克。 M 在他的评论中是正确的。 arraylist 确实是空的。但是我现在已经修好了。对我的 for 循环的愚蠢监督。
我在警报对话框中使用列表视图,但是当对话框显示时,它没有显示任何内容。该对象不为空,因为我正在填充它并将其添加到警报对话框之前的数组列表中。这是我的 xml 和 类 的代码:
我在哪里设置适配器:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Select a match");
//insert array to constructor
LayoutInflater inflater = getLayoutInflater(savedInstanceState);
View dialogLayout = inflater.inflate(R.layout.dialoglist, null);
matchAdapter testAdapter = new matchAdapter(getContext(), userInfos);
ListView listView = (ListView) dialogLayout.findViewById(R.id.dialogListView);
listView.setAdapter(testAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//do something
}
});
builder.setView(dialogLayout);
AlertDialog alert = builder.create();
alert.show();
自定义适配器:
class matchAdapter extends ArrayAdapter<userInfo> {
public matchAdapter(Context context, ArrayList<userInfo> test) {
super(context, R.layout.match_result, test);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
View customRow = inflater.inflate(R.layout.match_result, parent, false);
userInfo singleItemTest = getItem(position);
/*
TODO: get references to layout elements
*/
TextView username = (TextView) customRow.findViewById(R.id.matchUsername);
TextView sex = (TextView) customRow.findViewById(R.id.matchSex);
TextView age = (TextView) customRow.findViewById(R.id.matchAge);
Button sendRequest = (Button) customRow.findViewById(R.id.matchSendRequest);
username.setText(singleItemTest.getUserName());
return customRow;
}}
row.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/match_result"
android:orientation="horizontal"
android:padding="16dp"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="left">
<TextView
android:id="@+id/matchUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/matchSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sex"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/matchAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:layout_marginRight="10dp"/>
<Button
android:id="@+id/matchSendRequest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Request"
android:minHeight="5dp"
android:minWidth="0dp"
android:layout_marginLeft="80dp"/>
dialoglist.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialogListView"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</ListView>
在您的 Adapter 中添加一个对象 ArrayList<userInfo> infos
现在在构造函数中使用这个
public matchAdapter(Context context, ArrayList<userInfo> test) {
super(context, R.layout.match_result, test);
infos=test;
}
现在 getView
回调将像这样:
userInfo singleItemTest = infos.get(position);
我想这会解决你的问题:)
迈克。 M 在他的评论中是正确的。 arraylist 确实是空的。但是我现在已经修好了。对我的 for 循环的愚蠢监督。