且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用列表视图在自定义对话框(机器人)

更新时间:2022-12-10 22:13:29

在您设置你的对话部分:

 的String [] MDATA;
//获取数据;我不知道它来自哪里
MobileArrayAdapter mAdapter =新MobileArrayAdapter(的getContext(),MDATA);
ListView控件mListView =(ListView控件)dialog.findViewById(R.id.listviewdialog);
mListView.setAdapter(mAdapter);

Ive created a custom adpater for my list view aswell as having created my custom dialog with a list view in it but i have no idea how to link the data into the listview in the custom dialog (im doing a really bad job of explaining this i know). My adapter uses a listview that has checkbox's and i would like to know how im able to store if there checked or not for the next time the application is open. I'll put it into steps so its not so confusing: I want to: Create a list view with my adapter inside my existing custom dialog, Store the state of the checkbox's for the next time the application is open.

(its not shown but my listview is called listviewdialog)

My main activity (just the custom dialog bit)

button = (Button) findViewById(R.id.button01);

    // add button listener
    button.setOnClickListener(new OnClickListener() {

      public void onClick(View arg0) {

        // custom dialog
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.list);
        dialog.setTitle("The List");


        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.TextView01);
        text.setText("Did you not read the button? :P i'm not finshed on this yet XD");


        Button dialogButton = (Button) dialog.findViewById(R.id.Button01);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
      }
    });

My Custom Adapter:

package kevin.erica.box;

import kevin.erica.box.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

public class MobileArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;

public MobileArrayAdapter(Context context, String[] values) {
    super(context, R.layout.list_adapter, values);
    this.context = context;
    this.values = values;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.list_adapter, parent, false);
    CheckBox textView = (CheckBox) rowView.findViewById(R.id.checkBox1);
    textView.setText(values[position]);



    return rowView;
}
 }

In the part where you set up your dialog:

String[] mData;
// get your data; I don't know where its coming from
MobileArrayAdapter mAdapter = new MobileArrayAdapter(getContext(), mData);
ListView mListView = (ListView) dialog.findViewById(R.id.listviewdialog);
mListView.setAdapter(mAdapter);