2016-06-13 61 views
1

如果复选框被单击,我已经在arraylist中存储了一些值,但我很难从listView类中获取它。我怎么才能得到它。此外,如果我保持 clickListener中包含复选框和textView,复选框单击不起作用,但如果我在TextView中单击它的作品。如何解决它。以下是我的代码。 在此先感谢从listView类中的适配器获取arrayList值

singlerow_compare.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:id="@+id/singleRow" android:gravity="center"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/textViewCompare" /> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageViewCompare"/> 

</RelativeLayout> 

的ListView XML:

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/listViewCompare" /> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Compare" 
    android:id="@+id/compare" 
    android:layout_alignParentBottom="true"/> 

ListView.java

listView = (HorizontalListView) findViewById(R.id.listViewCompare); 
compareBtn = (Button) findViewById(R.id.compare); 
listView.setAdapter(new CustomAdapter(this, nameList, imageList)); 
compareBtn.setOnClickListener(new View.OnClickListener() 
    { @Override 
      public void onClick(View view) { 
      //I need to have addCheckBoxValue arraylist from adapter here 
      } 
    } 

CustomAdapter.java

public class CustomAdapter extends BaseAdapter { 
ArrayList result; 
Context context; 
Drawable [] imageId; 
protected ArrayList<String> addCheckBoxValue; 

private static LayoutInflater inflater=null; 
public CustomAdapter(CompareListView mainActivity, ArrayList nameList, Drawable[] imageList) { 
    result=nameList; 
    context=mainActivity; 
    imageId=imageList; 
    inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 
public class Holder 
{ 
    TextView tv; 
    ImageView img; 
    CheckBox checkBox; 
} 

@Override 
public View getView(final int i, View view, ViewGroup viewGroup) { 
    addCheckBoxValue = new ArrayList(); 
    final Holder holder=new Holder(); 
    View rowView; 
    rowView = inflater.inflate(R.layout.singlerow_compare, null); 
    holder.tv=(TextView) rowView.findViewById(R.id.textViewCompare); 
    holder.checkBox = (CheckBox) rowView.findViewById(R.id.imageViewCompare); 
    holder.tv.setText(result.get(i).toString()); 
    holder.checkBox.setButtonDrawable(imageId[i]); 
    final String selectedCbValue = holder.tv.getText().toString(); 
    holder.checkBox.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      boolean checked = ((CheckBox) view).isChecked(); 
      if(checked){ 
       if (!addCheckBoxValue.contains(selectedCbValue)) 
        addCheckBoxValue.add(selectedCbValue); 
      }else{ 
       if (addCheckBoxValue.contains(selectedCbValue)) 
        addCheckBoxValue.remove(selectedCbValue); 
      } 
     } 
    }); 
    rowView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
    //if i click on textView, it works but when i click on checkbox here, it doesnt work why?? So i have clicklistener in checkbox abov 
     } 
    }); 
    return rowView; 
} 
+0

使用一个接口来获取值,在适配器中定义的接口,并在您的活动 –

+0

实现它你需要维护单独的数组复选框来存储值 –

回答

1

CustomAdapter类创建下面的方法:

public ArrayList<String> getCheckBoxValue(){ 
    return addCheckBoxValue; 
} 

而在你的ListActivity。改变你的活动这样的代码:

private CustomAdapter listAdapter; // declare this before on create 

现在,你在你的设置适配器创建写这样的代码:

listAdapter = new CustomAdapter(this, nameList, imageList) 
// set Adapter like this: 
listView.setAdapter(listAdapter); 

您的点击代码按钮应该是这样的:

compareBtn.setOnClickListener(new View.OnClickListener() 
    { @Override 
      public void onClick(View view) { 
      //I need to have addCheckBoxValue arraylist from adapter here 
      listAdapter.getCheckBoxValue(); // do whatever you want to do here 

      } 
    } 

快乐编码!

+0

tysm。我尝试初始化适配器类,但在listView.setAdapter()和那个错误之后做了。我没有看到,并失去了几个小时。谢谢..... – beck

1

我建议你做布尔数组复选框,并保持它像这样,这个工作对我来说

public class CustomAdapter extends BaseAdapter { 
    private final LayoutInflater inflater; 
    private final Context context; 
    private List<ModelPooja> listData; 

    public CustomAdapter(Context mainActivity, List<ModelPooja> listData) { 
     context = mainActivity; 
     this.listData = listData; 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     return listData.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return listData.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 

     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(R.layout.list_item_poojaselection, null); 
      holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname); 
      holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck); 
      convertView.setTag(holder); 
     }else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 
     holder.checks.setOnCheckedChangeListener(null); 
     holder.checks.setFocusable(false); 

     if (listData.get(position).isselected) { 
      holder.checks.setChecked(true); 
     } else { 
      holder.checks.setChecked(false); 
     } 

     holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton cb, boolean b) { 

       if (checkMaxLimit()) { 

        if (listData.get(position).isselected && b) { 
         holder.checks.setChecked(false); 
         listData.get(position).isselected = false; 

        } else { 
         holder.checks.setChecked(false); 
         listData.get(position).isselected = false; 
         Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show(); 
        } 
       } else { 
        if (b) { 
         listData.get(position).isselected = true; 
        } else { 
         listData.get(position).isselected = false; 
        } 
       } 
      } 
     }); 

     holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME()); 
     return convertView; 
    } 

    public boolean checkMaxLimit() { 
     int countermax = 0; 
     for(ModelPooja item : listData){ 
      if(item.isselected){ 
       countermax++; 
      } 
     } 
     return countermax >= 5; 
    } 

    public class ViewHolder { 
     TextView tv; 
     public CheckBox checks; 
    } 
}