0

我有一个“行”视图,它有两个文本视图和一个复选框。我正在尝试实现它,以便在单击复选框时,它会在同一行中显示两个文本视图的字符串。

我已经实现了一个 for 循环来获取复选框视图的父级,然后迭代所有父级视图子级并从它们内部获取字符串。但是,我收到的不是文本,而是一条空的 Toast 消息。

我的第一堂课代码:

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

我的二等车型:

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

任何人都可以帮忙吗?

4

1 回答 1

1

请尝试使用此适配器而不是您的自定义适配器。

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

    }

在活动中定义您的侦听器并发送到适配器。

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

//你可以配置这个适配器构造函数。这只是一个例子

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>
于 2013-03-07T13:09:42.907 回答