Reputation: 87
I have list view with check box and text view. When i checked check box, change the text in same row. This way i will check all the check boxes in list view and change the text in every row. Can any one explain with sample code?
public class DialogAdapter extends BaseAdapter{
//private LayoutInflater inflater = null;
ArrayList<String> timeList = new ArrayList<String>();
Context context;
public DialogAdapter(Context context, ArrayList<String> timeList) {
// TODO Auto-generated constructor stub
this.context = context;
this.timeList = timeList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
//return timings.size();
return timeList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return timeList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@SuppressLint("ViewHolder")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//String timing = "10-30,14-08,16-30,19-00";
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.dialog_row_list,null);
//timeListView.setVisibility(convertView.VISIBLE);
TextView txt_time=(TextView) convertView.findViewById(R.id.text_times);
txt_time.setText(timeList.get(position).toString());
EditText comments=(EditText)convertView.findViewById(R.id.editText_comment);
final TextView txt_Action=(TextView) convertView.findViewById(R.id.textView_act);
CheckBox cBox= (CheckBox) convertView.findViewById(R.id.checkBox_action);
cBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@SuppressLint("ResourceAsColor")
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
int getPosition = (Integer) buttonView.getTag();
timeList.get(getPosition).setSelected(buttonView.isChecked());
Toast.makeText(context, "position"+getPosition, 0).show();
if(isChecked){
txt_Action.setText("finished");
txt_Action.setTextColor(R.color.Green);
}
else{
txt_Action.setText("UnChecked");
txt_Action.setTextColor(R.color.Red);
}
}
});
return convertView;
}
}
this is my code please check n give solution how to change listview text when check checkbox in listview???
Upvotes: 0
Views: 645
Reputation: 64
First I will suggest you to use ViewHolder pattern for listview adapter so that you don't need to do findViewById()
everytime you scroll your list this will prevent your listview from performance slow down and also I would prefer to use setOnClickListener()
over CheckedChangeListener()
.
Below is the sample code you can try
public class CustomAdapter extends BaseAdapter {
private Context context;
public static ArrayList<Item> modelArrayList;
public CustomAdapter(Context context, ArrayList<Item> modelArrayList){
this.context = context;
this.modelArrayList = modelArrayList;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getCount() {
return modelArrayList.size();
}
@Override
public Object getItem(int position) {
return modelArrayList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.layout_list_view_row_items, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Item currentItem = (Item) getItem(position);
if(currentItem.isChecked())
viewHolder.tvName.setText("Checked);
else
viewHolder.tvName.setText("Unchecked);
viewHolder.checkBox.setChecked(currentItem.isChecked());
viewHolder.checkBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final boolean checked = currentItem.isChecked();
if(checked){
viewHolder.checkBox.setChecked(!checked);
viewHolder.tvName.setText("Unchecked");
currentItem.setChecked(!checked);
}else{
viewHolder.checkBox.setChecked(checked);
viewHolder.tvName.setText("Checked");
currentItem.setChecked(checked);
}
}
});
return convertView;
}
private class ViewHolder {
protected CheckBox checkBox;
private TextView tvName;
public ViewHolder(View view) {
checkBox = (CheckBox)view.findViewById(R.id.checkbox);
tvName = (TextView) view.findViewById(R.id.textView);
}
}
}
Upvotes: 0
Reputation: 375
There is view before checkboxes
and texviews
that is listview
,if u want to access textviews
from listview
please make sure you are accessing from listview
one item layout
Like
TextView txtvw=findViewByID(ur_layout_of_list_item).findViewByID(ur_textview);
after that changes will be done.
Upvotes: 0
Reputation: 637
in the getView
method of your ListView
adapter:
if(yourCheckBox.isChecked)
yourTextView.setText("text if checkBox is checked.");
else
yourTextView.setText("text if checkBox is not checked.");
Upvotes: 1