2013-03-07 78 views
1

我想要当我检查复选框代码返回仅在同一行中的文本视图的字符串。Toast当在自定义适配器列表视图中检查

我正在使用LayoutInflater和另一个具有用于创建包含字符串的列表的代码。

我的代码:

public class InteractiveArrayAdapter extends ArrayAdapter<model> { 


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); 
      viewHolder.checkbox 
       .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, 
        boolean isChecked) { 
        model element = (model) viewHolder.checkbox 
         .getTag(); 
        element.setSelected(buttonView.isChecked()); 

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


     } 
} 

我的xml:

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

<TableLayout 
android:id="@+id/h103" 
android:layout_width="match_parent" 
android:layout_height="27dp" 
android:layout_x="0dp" 
android:layout_y="0dp" 
android:background="@drawable/back1" > 

<TableRow 
    android:id="@+id/TableRow05" 
    android:layout_width="match_parent" 
    android:layout_height="27dp" 
    android:layout_marginTop="4dp" > 

    <CheckBox 
     android:id="@+id/CheckBox05" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="-4dp" 
     android:button="@drawable/test" /> 

    <TextView 
     android:id="@+id/TextView07" 
     android:layout_width="220dp" 
     android:layout_height="22dp" 
     android:layout_marginLeft="23dp" 
     android:layout_marginTop="-6dp" 
     android:text="" /> 

    <TextView 
     android:id="@+id/TextView08" 
     android:layout_width="110dp" 
     android:layout_height="22dp" 
     android:layout_marginLeft="0dp" 
     android:layout_marginTop="-4dp" 
     android:text="095110263" /> 

</TableRow> 

</TableLayout> 

</AbsoluteLayout> 
+0

你有什么问题? – PCoder 2013-03-07 03:20:22

+0

阅读我的帖子的第一行:)。 – hesham 2013-03-07 10:53:46

回答

0

您必须对checkbox.onclicklistener工作,而不是checkbox.onCheckedChanged因为如果你使用它在列表视图,然后它会自动调用那么您选中的复选框将会隐藏起来。



    new OnClickListener() 
    { 
     @Override 
     public void onClick(View view) 
     {} 
    } 


在onclicklistener

,你视图参数是一个复选框。这里视图有一个getparent()方法,它返回一个视图,其中设置了复选框。您可以通过打印日志来检查它。如果你有复选框的父母,那么你也可以得到其中有一个子视图是复选框的父母的所有子视图。从所有的孩子的意见,你可以得到值,以显示他们在吐司。如果复选框的父项在列表项的视图中有另一个父项,则还可以通过调用getparent()方法获取父视图的父项。

请记住,视图有父子关系,所以如果你想要所有的孩子,那么你必须得到父母。

+0

我觉得有更容易的形式, – hesham 2013-03-07 11:21:50

+0

请分享您的解决方案。它可以帮助我编写好的和分布式的代码。 – 2013-03-07 16:03:52

0

先加1个按钮乌尔列表视图的底部btnOk

btnOk.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) 
      { 
       // TODO Auto-generated method stub 
       StringBuffer responseText = new StringBuffer(); 


       ArrayList<model> almodel = adapter.almodel; 
       responseText.append("The following were selected...\n"); 

       for(int i=0;i<almodel.size();i++) 
       { 
        model m = almodel.get(i); 
        if(m.isSelected()) 
        { 

         responseText.append("\n" + m.getName()); 

        } 

        } 


     Toast.makeText(getApplicationContext(),responseText,Toast.LENGTH_LONG).show(); 
相关问题