我试图从我创建的自定义适配器中获取复选框的范围。我在适配器中有三个文本字段以及一个复选框,其可见性已设置为不可见..我已实现了一个要删除的上下文菜单转发和回复..所以......每当我点击删除上下文菜单项...复选框将显示,我将能够通过选择它删除多个项目..
这是我用于创建自定义适配器的listview行xml ...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_normal_selector" >
<TextView
android:id="@+id/Sug_Sender_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sender Name"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/Sug_Sent_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Sug_Sender_Name"
android:layout_alignBottom="@+id/Sug_Sender_Name"
android:layout_alignParentRight="true"
android:layout_marginRight="17dp"
android:text="Time" />
<TextView
android:id="@+id/Sug_Sent_Topic_Display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_alignLeft="@+id/Sug_Sender_Name"
android:layout_below="@+id/Sug_Sender_Name"
android:text="Subject :" />
<TextView
android:id="@+id/Sug_Sent_Topic"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/Sug_Sent_Topic_Display"
android:layout_alignBottom="@+id/Sug_Sent_Topic_Display"
android:layout_marginLeft="8dp"
android:singleLine="true"
android:layout_toRightOf="@+id/Sug_Sent_Topic_Display"
android:text="Topic" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/Sug_Sent_Topic_Display"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:text="Details :" />
<TextView
android:id="@+id/Sug_Sent_Details"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignLeft="@+id/Sug_Sent_Topic"
android:text="Details" />
<CheckBox
android:id="@+id/suggestion_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/Sug_Sent_Details"
android:layout_alignLeft="@+id/Sug_Sent_time"
android:visibility="invisible"/>
</RelativeLayout>
Plz帮助家伙...我想在按下“删除”项目时显示复选框。提前谢谢
以下是自定义适配器的代码..
public class SuggestionsListAdapter extends BaseAdapter
{
Context ctx;
ArrayList<SuggestionMessage> suggestion_list;
LayoutInflater inflater=null;
SuggestionHolder sug_holder=null;
Calendar cal = Calendar.getInstance();
DateFormat date_formatter = java.text.DateFormat.getDateInstance(DateFormat.DEFAULT);
public SuggestionsListAdapter(Context ctx,ArrayList<SuggestionMessage> suggestion_list)
{
super();
this.ctx = ctx;
this.suggestion_list = suggestion_list;
inflater=(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class SuggestionHolder
{
TextView sug_name,sug_time,sug_topic,sug_details;
CheckBox suggestion_select_box;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return suggestion_list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return suggestion_list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// TODO Auto-generated method stub
if(convertView==null)
{
convertView=inflater.inflate(R.layout.suggestion_row_layout, parent, false);
sug_holder=new SuggestionHolder();
sug_holder.sug_name=(TextView) convertView.findViewById(R.id.Sug_Sender_Name);
sug_holder.sug_time=(TextView) convertView.findViewById(R.id.Sug_Sent_time);
sug_holder.sug_topic=(TextView) convertView.findViewById(R.id.Sug_Sent_Topic);
sug_holder.sug_details=(TextView) convertView.findViewById(R.id.Sug_Sent_Details);
sug_holder.suggestion_select_box=(CheckBox) convertView.findViewById(R.id.suggestion_checkbox);
sug_holder.suggestion_select_box.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean isChecked)
{
// TODO Auto-generated method stub
int pos=(Integer)view.getTag();
suggestion_list.get(pos).setSelected(view.isChecked());
}
});
convertView.setTag(sug_holder);
}
else
{
sug_holder=(SuggestionHolder) convertView.getTag();
}
SuggestionMessage suggestion=(SuggestionMessage) getItem(position);
sug_holder.sug_name.setText("Sender"+suggestion.getFirst_name()+" "+suggestion.getLast_name());
sug_holder.sug_topic.setText(suggestion.getSuggestion_topic());
sug_holder.sug_details.setText(suggestion.getSuggestion_details());
try {
Date temp_date=date_formatter.parse(suggestion.getSuggestion_date());
if(cal.getTime()==temp_date)
{
sug_holder.sug_time.setText(suggestion.getSuggestion_time());
}
else
{
sug_holder.sug_time.setText(suggestion.getSuggestion_date());
}
} catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
sug_holder.suggestion_select_box.setTag(position);
sug_holder.suggestion_select_box.setChecked(suggestion.isSelected());
return convertView;
}