具有自定义适配器的alertdialog listview不显示任何内容

时间:2016-05-15 09:26:59

标签: android listview alertdialog

我在alertdialog中使用listview,但是当对话框显示时,它不会显示任何内容。该对象不是空的,因为我填充它并将其添加到alertdialog之前的数组列表中。下面是我的xml和类的代码:

我在设置适配器的地方:

function delayAjax(ajaxCall){
    clearTimeout(myFn);

    myFn = setTimeout(ajaxCall(), 300)

}

自定义适配器:

<button id="btnNext" onclick="delayAjax(ajaxCall);"><button>

row.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();

dialoglist.xml:

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

3 个答案:

答案 0 :(得分:0)

在适配器中添加对象ArrayList<userInfo> infos

现在在Constractor中使用此

public matchAdapter(Context context, ArrayList<userInfo> test) {
    super(context, R.layout.match_result, test);
    infos=test;

}

现在getView回调会是这样的:

userInfo singleItemTest = infos.get(position);

我认为这将解决您的问题:)

答案 1 :(得分:0)

您在适配器中对 match_result.xml 进行了充气,​​但是您正试图访问 row.xml中的ID

答案 2 :(得分:0)

麦克。 M的评论是正确的。 arraylist确实是空的。不过我现在已经修好了。对我的for循环进行愚蠢的监督。