空吐司消息 [英] Empty Toast message

查看:109
本文介绍了空吐司消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个行看,它有两个文本视图和一个复选框。我想实现它,这样被点击复选框时,它显示的两个文本视图的字符串在同一行

I have a 'row' view, which has two text views and one check-box. I am trying to implement it so that when the checkbox is clicked on, it displays the strings of the two text views in the same row.

我实现了一个for循环来获取复选框视图的父级,然后遍历所有的父视图儿童和它们内部得到的字符串。但是相反的文字我得到一个空吐司消息。

I have implemented a for loop to get the parent of the checkbox view, then iterate all the parent views children and get the strings from inside them. But instead of the text I am getting an empty Toast message.

我的第一类code:

public class InteractiveArrayAdapter extends ArrayAdapter<model> implements
        OnClickListener {

    private final List<model> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<model> list) {
        super(context, R.layout.rep, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView text;
        protected CheckBox checkbox;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.rep, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.text = (TextView) view.findViewById(R.id.TextView07);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.CheckBox05);

            view.getParent();

            viewHolder.checkbox
                    .setOnClickListener(new CompoundButton.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            boolean h = ((CompoundButton) v).isChecked();
                            model element = (model) viewHolder.checkbox
                                    .getTag();
                            element.setSelected(h);

                            ViewGroup row = (ViewGroup) viewHolder.checkbox
                                    .getParent();

                            for (int itemPos = 0; itemPos < ((ViewGroup) row)
                                    .getChildCount(); itemPos++) {
                                View view = ((ViewGroup) row)
                                        .getChildAt(itemPos);
                                if (view instanceof TextView) {
                                    viewHolder.text = (TextView) view; //Found it!
                                    Toast.makeText(context,
                                            viewHolder.text.getText(), 10000)
                                            .show();

                                    break;
                                }
                            }
                        }
                    });

            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.text.setText(list.get(position).getName());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
}

我的第二类模型:

My second class model :

public class ListActivity1 extends ListActivity {

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_list);
        // Create an array of Strings, that will be put to our ListActivity
        InteractiveArrayAdapter adapter = new InteractiveArrayAdapter(this,
                getModel());
        setListAdapter(adapter);
    }

    private List<model> getModel() {
        List<model> list = new ArrayList<model>();
        list.add(get("Linux"));
        list.add(get("Windows7"));
        list.add(get("Suse"));
        list.add(get("Eclipse"));
        list.add(get("Ubuntu"));
        list.add(get("Solaris"));
        list.add(get("Android"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));
        list.add(get("iPhone"));

        // Initially select one of the items
        list.get(1).setSelected(true);
        return list;
    }

    private model get(String s) {
        return new model(s);
    }
}

任何一个可以帮助?

Can any one help?

推荐答案

请尝试,而不是你的自定义适配器该适配器。

pls try this adapter instead of your custom adapter.

public class ListAdapter<T extends BaseEntity> extends ArrayAdapter {
    @SuppressWarnings("unused")
    private final List<T> objects;
    private Activity activity;

    private final List<T> checkboxStatusListOfObject;

    private OnClickListener listener;

    @SuppressWarnings("unchecked")
    public ListAdapter(Activity activity, List<T> objects,
        OnClickListener listener, List<T> checkboxStatusListOfObject) {
        super( R.layout.simple_row_checkbox_1_item_listview , objects);
        this.objects = objects;
        this.activity = activity;
        this.listener = listener;
        this.checkboxStatusListOfObject = checkboxStatusListOfObject;

        getFilter();
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View rowView = convertView;
        ObjectViews sqView = null;

        if(rowView == null)
        {
            // Get a new instance of the row layout view
            LayoutInflater inflater = this.activity.getLayoutInflater();
            rowView = inflater.inflate(R.layout.YourCustomLAyout, null);

            // Hold the view objects in an object,
            // so they don't need to be re-fetched
            sqView = new ObjectViews();
            sqView.checbox = (CheckBox) rowView.findViewById(R.id.checkbox);
            if(this.listener !=null){
            sqView.checbox.setOnClickListener( this.listener);
            }
            sqView.text1 = (TextView) rowView.findViewById(R.id.text);
            sqView.dataObject = new Object();

            // Cache the view objects in the tag,
            // so they can be re-accessed later
            rowView.setTag(sqView);
        } else {
            sqView = (ObjectViews) rowView.getTag();
        }

        // Transfer the stock data from the data object
        // to the view objects
        T object = (T) objects.get(position);
        sqView.text1.setText(object.toString());



        sqView.checbox.setText("");
        sqView.checbox.setTag(sqView);
        sqView.dataObject = object;

        if(sqView.getChecbox().isChecked()){
            if(this.checkboxStatusListOfObject.indexOf( sqView.dataObject) != -1 ){
            sqView.getChecbox().setChecked(true);
            }
            else{
            sqView.getChecbox().setChecked(false);
            }
        }
        return rowView;
    }

    public class ObjectViews {
        Object dataObject;
    CheckBox checbox;
        TextView text1;
    public Object getDataObject() {
        return dataObject;
    }
    public void setDataObject(Object dataObject) {
        this.dataObject = dataObject;
    }
    public CheckBox getChecbox() {
        return checbox;
    }
    public void setChecbox(CheckBox checbox) {
        this.checbox = checbox;
    }
    public TextView getText1() {
        return text1;
    }
    public void setText1(TextView text1) {
        this.text1 = text1;
    }

    }

定义你的听众在活动,并发送至适配器。

Define your listener in activity and send to adapter.

OnClickListener listener = new OnClickListener() {

        @Override
        public void onClick(View v) {
             CheckBox cb = (CheckBox) v ;  
             // here your code
             if(cb.isChecked()){
                  ObjectViews object = (ObjectViews) cb.getTag();
                  String text = object.getText1().getText();
                  Toast.makeText(context,text, Toast.LENGTH_LONG).show();
             }
        }
    };

//您可以配置该适配器构造函数。这是唯一的例子。

//you can configure this adapter constructor. this is only example

yourListView.setAdapter(
        new ListAdapter(getApplicationContext(), yourObjectList
            listener, YourSelectedObjectList));

和布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingBottom="6dip"
    android:paddingTop="4dip" >

    <CheckBox
        android:id="@+id/checkbox"
        android:layout_width="0dp"
        android:layout_height="wrap_content" 
        android:layout_weight="1"
        android:textSize="20dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="0dp"
        android:layout_marginLeft="10dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:textSize="20dp" />

</LinearLayout>

这篇关于空吐司消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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