Android set selected listview item highlighted with custom adapter - android

I need your help. My problem is that I need to highlight the selected listview item. Note that it only needs to set single item, not multiple.
Here's what I've tried so far:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_video_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.background = (LinearLayout) convertView.findViewById(R.id.background);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
result = new HashMap<>();
result = data.get(position);
if(firstTimeStartup && position == 0) { // default selected
result = data.get(0);
highlightCurrentRow(viewHolder.background);
} else {
unhighlightCurrentRow(viewHolder.background);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = data.get(position);
if (firstTimeStartup) { // first time highlight first row
firstTimeStartup = false;
result = data.get(0);
unhighlight(viewHolder.background, 0);
}
highlightCurrentRow(viewHolder.background);
});
return convertView;
}
private void unhighlightCurrentRow(View rowView) {
rowView.setBackgroundResource(android.R.color.transparent);
}
private void highlightCurrentRow(View rowView) {
rowView.setBackgroundResource(R.color.main_blue);
}
private Boolean firstTimeStartup = true;
But the problem is I couldn't unhighlight the item when I clicked the convertView. Also the default (first) item is not unhighlighting when I tried to click other items. Would appreciate any help. Thanks!

convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = data.get(position);
for(int i=0;i<data.size();i++){
if(i == position){
highlightCurrentRow(viewHolder.background);
}
else{
unhighlightCurrentRow(viewHolder.background);
}
}
});

create a method like this :
CustomAdapter extends BaseAdapter {
int selectedPosition=0;
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(position==selectedPosition){
highlightCurrentRow(viewHolder.background);
}else{
unhighlightCurrentRow(viewHolder.background);
}
}
void highlightUnhighlight(int position){
seslectedPostion= postion;
notifyDatasetChanged
}
}
Call highlightUnhighlight(postion) in onItemClickListener.

I edited your code ,please check this code may be your problem will solve. I used this code in my project.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if(convertView == null) {
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_video_item, parent, false);
viewHolder = new ViewHolder();
viewHolder.background = (LinearLayout) convertView.findViewById(R.id.background);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
result = new HashMap<>();
result = data.get(position);
if(firstTimeStartup[position]) { // default selected
result = data.get(0);
highlightCurrentRow(viewHolder.background);
} else {
unhighlightCurrentRow(viewHolder.background);
}
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result = data.get(position);
if (firstTimeStartup[position]) {
Arrays.fill(firstTimeStartup, Boolean.FALSE);
firstTimeStartup[position] = false;
result = data.get(0);
unhighlight(viewHolder.background, 0);
}else{
Arrays.fill(firstTimeStartup, Boolean.FALSE);
firstTimeStartup[position] = true;
highlightCurrentRow(viewHolder.background);
}
notifyDataSetChanged();
});
return convertView;
}
private void unhighlightCurrentRow(View rowView) {
rowView.setBackgroundResource(android.R.color.transparent);
}
private void highlightCurrentRow(View rowView) {
rowView.setBackgroundResource(R.color.main_blue);
}
private Boolean firstTimeStartup[] = new Boolean[data.size()];
firstTimeStartup[0]=true;

Related

CustomAdapter - CheckedTextView

I have a problem with "remembering" states of Checkboxes in CheckTextView.
Now when I click, it changes the state but on different CheckBox.
class CustomAdapterSmartwatch extends ArrayAdapter<String> {
static class ViewHolder {
protected CheckedTextView cTextView;
}
ViewHolder holder;
public CustomAdapterSmartwatch(Context context, ArrayList<String> variables) {
super(context,R.layout.row_smartwatch_resources,variables);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.cTextView.setText(getItem(position));
holder.cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (holder.cTextView.isChecked()) {
holder.cTextView.setChecked(false);
} else {
holder.cTextView.setChecked(true);
}
}
});
return convertView;
}
}
Now i'm trying to figure out with using Holder but i heard that it's possible to use SparseBooleanArray. Can someone explain how to make it works?
EDIT 1
Ok, I have ListView which is controlled by CustomAdapter. That adapter contains only 1 item (CheckedTextView).
The problem is that after scrolling these checkedboxes don't remember their state.
I saw your answer with explanation but now after clicking any "CheckedTextView", the state of their checkbox doesn't change.
EDIT 2
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("MainActivity","Position = " + position);
CheckedTextView checkedText = (CheckedTextView) v;
checkedState.put(position, !checkedText.isChecked());
Log.d("MainActivity","State = " + checkedState.get(position));
}
});
holder.cTextView.setText(getItem(position));
holder.cTextView.setChecked(checkedState.get(position));
return convertView;
}
EDIT 3
class CustomAdapterSmartwatch extends ArrayAdapter<String> {
public SparseBooleanArray checkedState = new SparseBooleanArray();
private CheckedTextView cTextView;
public CustomAdapterSmartwatch(Context context, ArrayList<String> variables) {
super(context,R.layout.row_smartwatch_resources,variables);
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
cTextView.setText(getItem(position));
cTextView.setChecked(checkedState.get(position));
cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("MainActivity","Position = " + position);
CheckedTextView checkedText = (CheckedTextView) v;
checkedText.toggle();
if (checkedText.isChecked())
checkedState.put(position,true);
else
checkedState.delete(position);
Log.d("MainActivity","State = " + checkedState.get(position));
}
});
return convertView;
}
}
You have to keep a boolean flag for each CheckTextView. This can be an array, List, or SparseBooleanArray. The data structure you choose should be a field of your adapter:
class CustomAdapterSmartwatch extends ArrayAdapter<String> {
private SparseBooleanArray checkedState = new SparseBooleanArray();
// ...
}
You do not want to put it in the holder because the holder is retired only fr visible rows. On the other hand, you want to maintain the checked state for every row, visible or not.
Now, you set the stored checked state when the user clicks on a CheckedTextView:
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// ...
holder.cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckedTextView checkedText = (CheckedTextView)v;
checkedState.put(position, checkedText.isChecked());
}
});
}
And you restore the checked state whenever you inflate a view or reuse one from a holder:
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// ...
holder.cTextView.setText(getItem(position));
holder.cTextView.setChecked(checkedState.get(position));
// ...
}
You can simply use the view in onClick() method itself.
holder.cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(v instanceOf CheckedTextView) {
CheckedTextView c = (CheckedTextView) v;
c.setChecked(!c.isChecked());
}
}
});
Replace your code with following update:
class CustomAdapterSmartwatch extends ArrayAdapter<CustomAdapterSmartwatch.MyPojo> {
class MyPojo {
boolean checked;
String data;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
static class ViewHolder {
protected CheckedTextView cTextView;
}
ViewHolder holder;
public CustomAdapterSmartwatch(Context context, ArrayList<MyPojo> variables) {
super(context, R.layout.row_smartwatch_resources, variables);
}
#Nullable
#Override
public MyPojo getItem(int position) {
return super.getItem(position);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater smartwatchinflater = LayoutInflater.from(getContext());
convertView = smartwatchinflater.inflate(R.layout.row_smartwatch_resources,parent,false);
holder.cTextView = (CheckedTextView) convertView.findViewById(R.id.row_smartwatch_checked);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final MyPojo item = getItem(position);
holder.cTextView.setText(item.getData());
holder.cTextView.setChecked(item.isChecked());
holder.cTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean nextStatus = !item.isChecked();
item.setChecked(nextStatus);
holder.cTextView.setChecked(nextStatus);
}
});
return convertView;
}
}

Switch button changing state when scrolling in listview

I'm a beginner in Android development. This problem happens when I am vertically scrolling in this listview. Switch button changes the state automatically. How do I solve this issue? The code is shown below:
public class TrainerSessionAdapterView extends BaseAdapter {
public TrainerSessionAdapterView(Context context) {
this.context = context;
inflater = LayoutInflater.from(this.context);
workoutList = new ArrayList<>();
newSessionList = new ArrayList<>();
type = context.getResources().getString(R.string.type);
other = context.getResources().getString(R.string.other);
objectID = context.getResources().getString(R.string.object_id);
}
#Override
public int getCount() {
if (workoutList.isEmpty()) {
return 0;
} else {
return workoutList.size();
}
}
#Override
public ParseObject getItem(int position) {
return workoutList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
removeSession = new ArrayList<String>();
trainerWorkoutList = (ArrayList<String>)user.get("trainer_workout_type");
if (convertView == null) {
convertView = inflater.inflate(R.layout.session_listview, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (!workoutList.isEmpty() && position < workoutList.size() && change) {
ParseObject workoutObject = workoutList.get(position);
if (!TextUtils.isEmpty(workoutObject.get(type).toString()));
holder.sessionTypeView.setText(workoutObject.get(type).toString());
if(trainerWorkoutList!=null && workoutObject.getObjectId()!=null){
if (trainerWorkoutList.contains(workoutObject.getObjectId())){
holder.sessionSwitch.setChecked(true);
}
}
else{
holder.sessionSwitch.setChecked(false);
}
}
return convertView;
}
}
add this line in your getView method.
holder.switch.setChecked(ArrayList.get(position).isStudentPresent());
You could try this,
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
removeSession = new ArrayList<String>();
trainerWorkoutList = (ArrayList<String>)user.get("trainer_workout_type");
if (convertView == null) {
convertView = inflater.inflate(R.layout.session_listview, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.sessionSwitch.setChecked(false);
if (!workoutList.isEmpty() && position < workoutList.size() && change) {
ParseObject workoutObject = workoutList.get(position);
if (!TextUtils.isEmpty(workoutObject.get(type).toString()));
holder.sessionTypeView.setText(workoutObject.get(type).toString());
if(trainerWorkoutList!=null && workoutObject.getObjectId()!=null){
if (trainerWorkoutList.contains(workoutObject.getObjectId())){
holder.sessionSwitch.setChecked(true);
}
}
}
return convertView;
}
}

Textview duplication in custome listview

I have a custom list view with base adapter.In the listview each item contain button and a textview,I want to visible the textview when clicking on the corresponding button.It is working bt my problem is that when i click on the button the textview is visible in more than one item.Please suggest any solution.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
}
holder.ans=(Button)convertView.findViewById(R.id.btn_ans);
holder.textview=(TextView)convertView.findViewById(R.id.txt_ans);
holder.ans.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
holder.textview.setText("Answer is correct");
}
});
}
public class Holder
{
TextView textview;
Button ans;
}
Try this code.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
holder.ans=(Button)convertView.findViewById(R.id.btn_ans);
holder.textview=(TextView)convertView.findViewById(R.id.txt_ans);
convertView.setTag(holder);
}else{
holder=(holder)convertView.getTag();
}
if(!isTextViewVisible[position]) { holder.textview.setVisibility(View.Invisible); }
else {
holder.textview.setVisibility(View.visible);
}
holder.ans.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
holder.textview.setText("Answer is correct"); isTextViewVisible[position] = true;
} });
}
public class Holder
{
TextView textview;
Button ans;
}
You are didn't follow ViewHolder Pattern.You have to use convertView.setTag(holder) and holder=(holder)convertView.getTag()
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
// convertView = new View(context);
convertView = inflater.inflate(R.layout.secondadapter, null);
holder.ans=(Button)convertView.findViewById(R.id.btn_ans);
holder.textview=(TextView)convertView.findViewById(R.id.txt_ans);
convertView.setTag(holder);
}else{
holder=(holder)convertView.getTag();
}
holder.ans.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
holder.textview.setText("Answer is correct");
}
});
}
public class Holder
{
TextView textview;
Button ans;
}
Please have a read to understand , how listview works.
public class SampleAdapter extends BaseAdapter {
public SampleAdapter (Activity callingActivity,
ArrayList datas, Resources imagesEtc) {
this.callingActivity = callingActivity;
this.datas = datas;
this.imagesEtc = imagesEtc;
}
#Override
public void notifyDataSetChanged() {
// Refresh List rows
super.notifyDataSetChanged();
}
#Override
public int getCount() {
if (datas.size() <= 0)
return 1;
return datas.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public static class ItemsInEachRow {
TextView sample1, sample2, sample3;
}
#Override
public View getView(final int position, View eachRowsView,
ViewGroup parentView) {
// To Inflate Single Item of ListView
View eachRow = eachRowsView;
ItemsInEachRow itemsInEachRow;
if (eachRowsView == null) {
itemsInEachRow = new ItemsInEachRow();
eachRow = callingActivity.getLayoutInflater().inflate(
R.layout.your_layout, null);
itemsInEachRow.sample1= (TextView) eachRow
.findViewById(R.id.sample1);
eachRow.setTag(itemsInEachRow);
} else {
itemsInEachRow = (ItemsInEachRow) eachRow.getTag();
}
itemsInEachRow.sample1
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
return eachRow;
}
}
Please see below code hope it will work for you
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
convertView = inflater.inflate(R.layout.secondadapter, null);
holder.ans=(Button)convertView.findViewById(R.id.btn_ans);
holder.textview=(TextView)convertView.findViewById(R.id.txt_ans);
convertView.setTag(holder);
}
else
{
m_viewHolder = (ViewHolder) convertView.getTag();
}
holder.ans.setTag(position);
holder.ans.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
int m_position = Integer.parseInt(p_view.getTag().toString());
holder.textview.setText("Answer is correct");
}
});
}
public class Holder
{
TextView textview;
Button ans;
}
If textview is sibling of button. Using its parent get the corresponding textview. here is the code.
holder.ans.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
View vi = (View) v.getParent();
TextView texview = (TextView) vi.findViewById(R.id.txt_ans);
textview.setText("Answer is correct");
textview.setVisiblity(View.VISIBLE);
}
});
There are more than one view getting visible is because ListView reuse your view if it is no longer visible on the screen. To tell ListView to don't do this for a view: itemView.setHasTransientState(true)
When you are done with that view (e.g the item has been removed) you can call the method again and set it to false.
To Learn more. https://www.youtube.com/watch?v=8MIfSxgsHIs
P/s: It seems like you do not follow the viewHolder Pattern as #kishore-jethava said.

Listview with checkbox, show the checked entries on another listview

I have a listview, which shows the picture, type/description of car and a checkbox. When the user now selected a car/s, I want to show them in another listview. When he unselected him from the CarListAdapter, it should also disappear from the new listview. Can I realize this with using the same Viewholder?
class CarListAdapter extends BaseAdapter
{
ArrayList<ItemsHolder> holderList;
BitmapHelper bitmapHelper = BitmapHelper.getInstance();
public CarListAdapter(ArrayList<ItemsHolder> holderList)
{
this.holderList = holderList;
}
#Override
public int getCount()
{
return holderList.size();
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
final ViewHolder holder;
if (convertView == null)
{
LayoutInflater inflater = LayoutInflater.from(RentingDataActivity.this);
convertView = inflater.inflate(R.layout.listview_renting_entry, null, true);
holder = new ViewHolder();
holder.rowCarId = (TextView) convertView.findViewById(R.id.rowCarId);
holder.rowImageView = (ImageView) convertView
.findViewById(R.id.rowImageView);
holder.rowCarType = (TextView) convertView.findViewById(R.id.rowCarType);
holder.rowCarDescription = (TextView) convertView
.findViewById(R.id.rowCarDescription);
holder.rowCheckbox = (CheckBox) convertView.findViewById(R.id.rowCheckBox);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
// holder.rowCheckbox.setOnCheckedChangeListener(null);
// holder.rowCheckbox.setTag(R.id.rowCheckBox, position);
}
// auto = holderList.get(position);
final ItemsHolder ih = holderList.get(position);
holder.rowImageView.setImageBitmap(bitmapHelper.getBigImage(ih.getImagePath(), 248));
holder.rowCarType.setText(ih.getCarType());
holder.rowCarDescription.setText(ih.getCarDescription());
// holder.rowCheckbox.setTag(ih);
holder.rowCarId.setText(ih.getCarId());
holder.rowCheckbox.setChecked(ih.isSelected());
holder.rowCheckbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CheckBox checkbox = (CheckBox)v.findViewById(R.id.rowCheckBox);
// Toast.makeText(getApplicationContext(), holder.rowCarType.getText().toString(),
// Toast.LENGTH_LONG).show();
ih.setSelected(checkbox.isChecked());
}
});
return convertView;
}
}
I would add this as a comment but alas I haven't got enough points:
add to your adapter;
notifyDataSetChanged( );
This refreshes the list asynchronously.

listview with checkbox single clickable

i have a listivew with custom arrayadapter and custom layout, it have a Checkbox.
I want to make this listview only one checkbox enabled but i dont know how can i do it.
This is my adapter, but the check and uncheck dont works fine. Any exmple please.
public class gremioAdapter extends ArrayAdapter<Gremio> {
Context context;
int layoutResourceId;
ArrayList<Gremio> data = null;
protected String comentarioAlEdiText;
public gremioAdapter(Context context, int layoutResourceId, ArrayList<Gremio> data)
{
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
public void updateDataSet(Gremio objetoGremioAnadir) {
this.data.add(objetoGremioAnadir);
notifyDataSetChanged();
}
public void updateDataSet(Gremio objetoGremioAnadir, int posicion) {
this.data.add(posicion, objetoGremioAnadir);
notifyDataSetChanged();
}
public void updateDataSet(String comentario, int posicion) {
this.data.get(posicion).comentario = comentario;
notifyDataSetChanged();
}
public void updateDataSet() {
notifyDataSetChanged();
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
GremioHolder holder = null;
final GremioHolder h2 = holder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(layoutResourceId, parent, false);
holder = new GremioHolder();
holder.tvGremio = (TextView) row.findViewById(R.id.tvGremio);
holder.cbActivo = (CheckBox) row.findViewById(R.id.cbGremioActivo);
holder.tvComentario = (TextView) row.findViewById(R.id.tvComentario);
row.setTag(holder);
}
else {
holder = (GremioHolder) row.getTag();
}
Gremio gremioFinal = data.get(position);
holder.tvGremio.setText(gremioFinal.literal);
holder.tvComentario.setText(gremioFinal.comentario);
if (data.get(position).getActivo().equals("1")) {
holder.cbActivo.setChecked(true);
}
else {
holder.cbActivo.setChecked(false);
}
holder.cbActivo.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
if (data.get(position).activo.equals("0"))
h2.cbActivo.setChecked(true);
if (data.get(position).activo.equals("1"))
h2.cbActivo.setChecked(false);
}
});
holder.cbActivo.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
for (int i = 0; i < data.size(); i++) {
data.get(i).activo = "0";
}
data.get(position).activo = "1";
}
if (isChecked) {
for (int i = 0; i < data.size(); i++) {
data.get(i).activo = "0";
}
}
notifyDataSetChanged();
}
});
return row;
}
public class GremioHolder {
TextView tvGremio;
CheckBox cbActivo;
TextView tvComentario;
}
}
}
This should take care of your requirement. A very good tutorial on how you can achieve this.
http://tokudu.com/2010/android-checkable-linear-layout/
So the basic funda is that,
Extend a Layout (Linear/Relative) and implement the checkable interface.
After inflating the layout, find out which child was selected and check the corresponding checkbox. Uncheck any other previously checked item.
Make sure that your ListView's XML has the following marked [android:choiceMode="singleChoice"]
hop it is useful to you...
just change in your getview() method .
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
final Holder holder;
if (vi == null) {
vi = inflater.inflate(R.layout.raw_customelabel_list, null);
holder = new Holder();
holder.txtname = (TextView) vi
.findViewById(R.id.raw_customlabel_txt_name);
holder.checkbox = (CheckBox) vi
.findViewById(R.id.raw_customlabel_checkbox);
vi.setTag(holder);
} else {
holder = (Holder) vi.getTag();
}
vi.setTag(holder);
holder.txtname.setText(strarry[position]);
holder.checkbox.setId(position);
holder.checkbox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < strarry.length; i++) {
if (v.getId() == i) {
checkarry[i] = true;
Log.v("check", ""+position);
} else {
checkarry[i] = false;
}
}
notifyDataSetChanged();
}
});
if (checkarry[position]) {
holder.checkbox.setChecked(true);
} else {
holder.checkbox.setChecked(false);
}
return vi;
}
}

Categories

Resources