Android的 - 正确执行自定义列表视图中的自定义对话框自定义、视图、对话框、正确

2023-09-06 00:04:08 作者:过来我抱抱

我有一个应用程序,我想显示自定义列表视图,包括内部的自定义对话框2 textViews的机器人。到目前为止,我有困难的时候搞清楚如何连接两个并生成对话框的内容。

I have an application where I would want to display a custom list view consisting of two textViews inside a custom dialog box in android. So far, I'm having a difficult time figuring out how to connect the two and generate the contents of the dialog box.

我试图按照自定义的ListView教程,只需设置对话框适配器,但无济于事的希望。

I tried to follow custom listView tutorials with the hopes of just setting the dialog adapter to it but to no avail.

到目前为止,这是我到目前为止有:

So far, this is what I have so far:

这是list_row_dialog.xml,对于含有两个textviews该行的布局:

This is list_row_dialog.xml, the layout for the row containing two textviews:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="50dp"
    android:orientation="horizontal"
    android:padding="5dip" >

<TextView
    android:id="@+id/unit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textStyle="bold"
    android:typeface="sans" />

<TextView
    android:id="@+id/quantity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:textColor="#343434"
    android:textSize="12sp" />

</RelativeLayout>

这里是dialog_main.xml文件,我想用对话框布局文件,它基本上是一个ListView控件。

And here is the dialog_main.xml file, the layout file that I want the dialog box to use, it's basically a listView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<ListView
    android:id="@+id/custom_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:dividerHeight="1dp" />

</LinearLayout>

而这里的CustomListAdapterDialog.java,我不知道如果我甚至需要用它显示在对话框中的数据:

And here's CustomListAdapterDialog.java, I'm not sure if I even need to use this to display data on the dialog box:

public class CustomListAdapterDialog extends BaseAdapter {

private ArrayList<ItemClass> listData;

private LayoutInflater layoutInflater;

public CustomListAdapterDialog(Context context, ArrayList<ItemClass> listData) {
    this.listData = listData;
    layoutInflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
    return listData.size();
}

@Override
public Object getItem(int position) {
    return listData.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = layoutInflater.inflate(R.layout.list_row_dialog, null);
        holder = new ViewHolder();
        holder.unitView = (TextView) convertView.findViewById(R.id.unit);
        holder.quantityView = (TextView) convertView.findViewById(R.id.quantity);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.unitView.setText(listData.get(position).getVariant().toString());
    holder.quantityView.setText(listData.get(position).getUnit().toString());

    return convertView;
}

static class ViewHolder {
    TextView unitView;
    TextView quantityView;
}

}

起初,我想这code,以显示该对话框:

Initially, I tried this code to display the dialog box:

private void showDialog(){
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dialog_main);


    dialog.show();
}

但它只能说明一空行,这是很清楚的,我不填充它正确。

But it only shows, one blank row, it is very clear that I am not populating it properly.

任何想法吗?另外,我可以填充对话框中的的ShowDialog()函数,因为我也有我想填充在同一个Java类中的数据。

Any ideas anyone? Also, I can populate the dialog boxes in the showDialog() function since I also have the data I want to populate with in the same java class.

推荐答案

您做以下?您将需要膨胀布局文件 dialog_main ,找到的ListView ,设置适配器和OnItemClickListener它。在此之后,您可以使用此对话框的的setContentView(查看)方法来获取列表中显示。

Are you doing the following? You will need to inflate the layout file dialog_main, find the ListView, set an adapter and an OnItemClickListener for it. After this, you can use the dialog's setContentView(View) method to get the list to display.

private void showDialog(){

    final Dialog dialog = new Dialog(this);

    View view = getLayoutInflater().inflate(R.layout.dialog_main, null);

    ListView lv = (ListView) view.findViewById(R.id.custom_list);

    // Change MyActivity.this and myListOfItems to your own values
    CustomListAdapterDialog clad = new CustomListAdapterDialog(MyActivity.this, myListOfItems);

    lv.setAdapter(clad);

    lv.setOnItemClickListener(........);

    dialog.setContentView(view);

    dialog.show();

}

顺便说一句,您的适配器看起来正常的。这是行不通的,因为你不给它任何工作。

By the way, your adapter looks alright. It isn't working because you're not giving it anything to work on.