使用列表视图在自定义对话框(机器人) [英] Using Listview In a custom dialog (android)

查看:152
本文介绍了使用列表视图在自定义对话框(机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

香港专业教育学院创建了自定义adpater我的列表视图藏汉为已创建我的自定义对话框,在其中一个列表视图,但我不知道如何将数据链接到自定义对话框的列表视图(即时通讯做的一个非常糟糕的工作解释这个我知道)。我的适配器使用具有复选框的列表视图,我想知道如何能够即时存储,如果有检查或没有下一次申请是开放的。
我将它放在步骤,所以它不是这么混乱:
我要:
我现有的自定义对话框内创建我的适配器列表视图,
存储复选框的状态的应用程序打开的下一次。

(它没有显示,但我的列表视图称为listviewdialog)

我的主要活动(仅显示自定义对话框位)

 按钮=(按钮)findViewById(R.id.button01);    //添加按钮监听器
    button.setOnClickListener(新OnClickListener(){      公共无效的onClick(查看为arg0){        //定制对话框
        最后对话的对话=新的对话框(背景);
        dialog.setContentView(R.layout.list);
        dialog.setTitle(以下简称目录);
        //设置自定义对话框组件 - 文字,图片和按钮
        TextView的文本=(TextView的)dialog.findViewById(R.id.TextView01);
        text.setText(你看不懂的按钮:P我不会finshed对这个尚未XD?);
        按钮dialogBu​​tton =(按钮)dialog.findViewById(R.id.Button01);
        //如果点击按钮,关闭对话框定制
        dialogBu​​tton.setOnClickListener(新OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                dialog.dismiss();
            }
        });        dialog.show();
      }
    });

我的自定义适配器:

 包kevin.erica.box;进口kevin.erica.box.R;进口android.content.Context;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.ArrayAdapter;
进口android.widget.CheckBox;
进口android.widget.ImageView;
进口android.widget.TextView;公共类MobileArrayAdapter扩展ArrayAdapter<串GT; {
私人最终上下文的背景下;
私人最终的String []值;公共MobileArrayAdapter(上下文的背景下,的String []值){
    超(背景下,R.layout.list_adapter,价值观);
    this.context =背景;
    this.values​​ =值;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    LayoutInflater吹气=(LayoutInflater)上下文
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    查看rowView = inflater.inflate(R.layout.list_adapter,父母,假);
    复选框的TextView =(复选框)rowView.findViewById(R.id.checkBox1);
    textView.setText(值[位置]);    返回rowView;
}
 }


解决方案

在您设置你的对话部分:

 的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);

这篇关于使用列表视图在自定义对话框(机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆