after remove item selected check box auto check next item.
i try to override getcount method but no result
CountryAdapter.java
CountryAdapter extends ArrayAdapter<MyCountry>{
Context context; int layoutResourceId; ArrayList<MyCountry> countries; ContextualActionMode activity;
public CountryAdapter(Context context, int layoutResourceId,
ArrayList<MyCountry> countries) {
}
#Override
public int getCount() {
return countries.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final MyCountry country = countries.get(position);
ViewHolder viewHolder = null;
if(convertView == null)
{
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(layoutResourceId, null);
viewHolder.nameEn = (TextView) convertView.findViewById(R.id.tvNameEn);
viewHolder.nameVi = (TextView) convertView.findViewById(R.id.tvNameVi);
viewHolder.flag = (ImageView) convertView.findViewById(R.id.ivFlag);
viewHolder.check = (CheckBox) convertView.findViewById(R.id.checkBox1);
convertView.setTag(viewHolder);
}
else
viewHolder = (ViewHolder) convertView.getTag();
viewHolder.nameEn.setText(countries.get(position).getNameEn());
viewHolder.nameVi.setText(countries.get(position).getNameVi());
viewHolder.flag.setImageDrawable(countries.get(position).getFlag());
viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
count = 0;
for (MyCountry country : countries) {
if(country.isCheck()) count++;
}
if(isChecked)
{
if(activity.actionMode == null || count == 0)
activity.actionMode = activity.startActionMode(activity.callback);
count++;
country.setCheck(true);
}
else
{
country.setCheck(false);
count--;
if(count == 0) activity.actionMode.finish();
}
}
});
return convertView;
}
int count = 0;
public class ViewHolder{
TextView nameEn;
TextView nameVi;
ImageView flag;
CheckBox check;
}
ContextualActionMode.java
public class ContextualActionMode extends Activity {
ArrayList<MyCountry> countries = new ArrayList<MyCountry>();
ListView listView;
CountryAdapter adapter;
ActionMode.Callback callback = new ActionMode.Callback() {
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.mnDelete:
for (int i = 0; i < countries.size(); i++)
{
if (countries.get(i).isCheck()) {
countries.remove(countries.get(i));
countries.get(i).setChecked(false)
}
}
adapter.notifyDataSetChanged();
mode.finish();
return true;
default:
break;
}
return false;
}
};
maybe error here, i find some solutions, but nothing work
i try to change the loop, because the list start index from 0
// i can fix it, thank a lot to Armaan Stranger
link to my source for who has the same problem with me
mediafire.com/?agnvic06c69cvw0
and edit in CountryAdapter.java
viewHolder.flag.setImageDrawable(countries.get(position).getFlag());
viewHolder.check.setChecked(false); --> right here, i forgot to add set check false as default.
viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
Try this:
Just add this line before your onCheckChanged() event like this.
viewHolder.nameEn.setText(countries.get(position).getNameEn());
viewHolder.nameVi.setText(countries.get(position).getNameVi());
viewHolder.flag.setImageDrawable(countries.get(position).getFlag());
viewHolder.check.setChecked(false);
viewHolder.check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
count = 0;
for (MyCountry country : countries) {
if(country.isCheck())
count++;
}
if(isChecked)
{
if(activity.actionMode == null || count == 0)//chua co
activity.actionMode = activity.startActionMode(activity.callback);
count++;
country.setCheck(true);
}
else
{
country.setCheck(false);
count--;
if(count == 0)
activity.actionMode.finish();
}
}
});
Hope it Helps!!
You have to just set your listview before notifyDataSetChanged();
public void dellistview() {
listviewdata.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
SparseBooleanArray selected = listAdapterData.getCheckedItemPositions();
if (selected != null) {
try {
for (int i = (selected.size() - 1); i >= 0; i--)
{
if (selected.valueAt(i)) {
String str[] = arrList.get(selected.keyAt(i));
fmdbAccess.removelistitm(sourceTable, str[1]);
if (arrList != null)
arrList.remove(str);
}
}
} catch (Exception e) {
e.printStackTrace();
}
selected.clear();
arrList = fmdbAccess.getGenricTable(sourceTable, colName);
if (arrList != null && arrList.size() > 0)
setListAdapter();
listAdapterData.notifyDataSetChanged();
// finish();
}
}
Try to use android:checked="false" to CheckBox in your xml file please
Related
Ive a listview with a MultiChoiceModeListener and a ChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL). This is working as expectet.
When I slect an item, the item is highlighted, even when I choose multiple ones.
I have a customArrayAdapter within I set alternate row colors in the listview.
And here the problem lies: At time when I start using the alternate row colors in the customArrayAdapter, the highlighting when selecting an item isnt working anymore!
It seemes that the highlighting happens in background of my alternate row colors.
setMultiChoiceModeListener
private void initializeContextualActionBar() {
final ListView shoppingMemosListView = (ListView) findViewById(R.id.listview_shopping_memos);
shoppingMemosListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
shoppingMemosListView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
int selCount = 0;
#Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
if (checked) {
selCount++;
} else {
selCount--;
}
String cabTitle = selCount + " " + getString(R.string.cab_checked_string);
mode.setTitle(cabTitle);
mode.invalidate();
}
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
getMenuInflater().inflate(R.menu.menu_contextual_action_bar, menu);
return true;
}
#Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
MenuItem item = menu.findItem(R.id.cab_change);
if (selCount == 1) {
item.setVisible(true);
} else {
item.setVisible(false);
}
return true;
}
#Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
boolean returnValue = true;
SparseBooleanArray touchedShoppingMemosPositions = shoppingMemosListView.getCheckedItemPositions();
switch (item.getItemId()) {
case R.id.cab_delete:
for (int i = 0; i < touchedShoppingMemosPositions.size(); i++) {
boolean isChecked = touchedShoppingMemosPositions.valueAt(i);
if (isChecked) {
int postitionInListView = touchedShoppingMemosPositions.keyAt(i);
ShoppingMemo shoppingMemo = (ShoppingMemo) shoppingMemosListView.getItemAtPosition(postitionInListView);
dataSource.setDeleteShoppingMemo(shoppingMemo);
}
}
showAllListEntries();
mode.finish();
break;
case R.id.cab_change:
for (int i = 0; i < touchedShoppingMemosPositions.size(); i++) {
boolean isChecked = touchedShoppingMemosPositions.valueAt(i);
if (isChecked) {
int postitionInListView = touchedShoppingMemosPositions.keyAt(i);
ShoppingMemo shoppingMemo = (ShoppingMemo) shoppingMemosListView.getItemAtPosition(postitionInListView);
AlertDialog editShoppingMemoDialog = createEditShoppingMemoDialog(shoppingMemo);
editShoppingMemoDialog.show();
Button theButton = editShoppingMemoDialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListenerEditShoppingMemoDialogPositiv(editShoppingMemoDialog, shoppingMemo));
}
}
mode.finish();
break;
default:
returnValue = false;
break;
}
return returnValue;
}
#Override
public void onDestroyActionMode(ActionMode mode) {
selCount = 0;
}
});
}
customArrayAdapter
public class ShoppingMemoArrayAdapter extends ArrayAdapter<ShoppingMemo> {
public static final String LOG_TAG = ShoppingMemoArrayAdapter.class.getSimpleName();
private Context mContext;
private List<ShoppingMemo> mShoppingMemo;
private LayoutInflater mLayoutInflater;
public ShoppingMemoArrayAdapter(Context context, List<ShoppingMemo> shoppingMemoList) {
super(context, R.layout.list_row, shoppingMemoList);
mContext = context;
mShoppingMemo = shoppingMemoList;
mLayoutInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SharedPreferences sharedPrefs = mContext.getSharedPreferences("de.tobias.shoppinglist_preferences", mContext.MODE_PRIVATE);
Boolean prefShowCount = sharedPrefs.getBoolean("show_count", false);
ShoppingMemo memo = mShoppingMemo.get(position);
View rowView;
if (convertView == null) {
rowView = mLayoutInflater.inflate(R.layout.list_row, parent, false);
}
else {
rowView = convertView;
}
TextView tvProduct = (TextView) rowView.findViewById(R.id.listview_row_product);
TextView tvCount = (TextView) rowView.findViewById(R.id.listview_row_count);
CheckBox tvChecked = (CheckBox) rowView.findViewById(R.id.listview_row_checked);
tvChecked.setChecked(memo.isChecked());
tvProduct.setText(memo.getProduct());
if(memo.getQuantity() > 1 || prefShowCount)
{
tvCount.setText(Integer.toString(memo.getQuantity()));
}else{
tvCount.setText("");
}
if (memo.isChecked()) {
tvProduct.setPaintFlags(tvProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
tvProduct.setTextColor(Color.rgb(175,175,175));
tvCount.setTextColor(Color.rgb(175,175,175));
rowView.setBackgroundColor(Color.BLACK);
}
else {
tvProduct.setPaintFlags( tvProduct.getPaintFlags() & (~ Paint.STRIKE_THRU_TEXT_FLAG));
tvProduct.setTextColor(Color.DKGRAY);
tvCount.setTextColor(Color.DKGRAY);
if (position % 2 == 1) {
rowView.setBackgroundColor(Color.LTGRAY);
} else {
rowView.setBackgroundColor(Color.WHITE);
}
}
return rowView;
}
}
expectet
expectet
not working
not working
I tried setting the highlighting whithin the customArrayAdapter, but the seletcted state was not probebly there.
I tried setting the highlighting whithin the setMultiChoiceModeListener but that was alwayse very weired und overwritten from the customArrayAdapter.
Does anyone has an idear what to do here?
I have radio button in list-view which is working fine with my custom adapter.
The problem is I want to set radio button checked if answer already exists in my model.
Following is adapter class:
public class options_data_adapter extends ArrayAdapter<ChildListDataModel> {
private Activity context;
private List<ChildListDataModel> data;
private int resId;
MySharedPreferences mpref;
private RadioButton mSelectedRB;
private int mSelectedPosition = -1;
boolean[] itemChecked;
public options_data_adapter(Context context, int resource, List<ChildListDataModel> objects) {
super(context, resource, objects);
this.context = (Activity) context;
this.resId = resource;
this.data = objects;
mpref = new MySharedPreferences(context);
itemChecked = new boolean[objects.size()];
}
#Override
public View getView(final int position, View v, ViewGroup parent) {
final Holder h;// = null;
if (v == null) {
h = new Holder();
LayoutInflater inflater = context.getLayoutInflater();
v = inflater.inflate(resId, parent, false);
h.check = (CheckBox) v.findViewById(R.id.c1);
h.radio=(RadioButton)v.findViewById(R.id.r1);
h.spinner=(Spinner)v.findViewById(R.id.s1);
h.btn=(Button)v.findViewById(R.id.btn_spiner);
v.setTag(h);
} else {
h = (Holder) v.getTag();
}
Log.d("soh_answer", data.get(position).answere);
if(mpref.GetquestionType().equals("Radio")){
h.check.setVisibility(View.GONE);
h.radio.setVisibility(View.VISIBLE);
h.spinner.setVisibility(View.GONE);
h.btn.setVisibility(View.GONE);
h.radio.setText(data.get(position).answere);
if(data.get(position).checked.equals("1")){
h.radio.setBackgroundResource(R.drawable.button_background);
h.radio.setChecked(true);
}else{
h.radio.setBackgroundResource(R.drawable.button_ackgroun1);
h.radio.setChecked(false);
}
h.radio.setTag(position);
h.radio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Log.i("ID of radiobutton","Order Edit # position : " + pos);
if (position != mSelectedPosition && mSelectedRB != null) {
mSelectedRB.setChecked(false);
((VasNBIActivity) context).removeChecked(data.get(mSelectedPosition).id);
mSelectedRB.setBackgroundResource(R.drawable.button_ackgroun1);
/*data.get(mSelectedPosition).checked="0";*/
}
mSelectedPosition = position;
((VasNBIActivity) context).addChecked(data.get(mSelectedPosition).id);
h.radio.setBackgroundResource(R.drawable.button_background);
/* data.get(mSelectedPosition).checked="1";*/
mSelectedRB = (RadioButton) v;
}
});
if (mSelectedPosition != position) {
h.radio.setChecked(false);
}else {
h.radio.setChecked(true);
if(mSelectedRB != null && h.radio != mSelectedRB){
mSelectedRB = h.radio;
}
}
}else if(mpref.GetquestionType().equals("Checkbox")){
h.check.setVisibility(View.VISIBLE);
h.radio.setVisibility(View.GONE);
h.check.setText(data.get(position).answere);
h.btn.setVisibility(View.GONE);
//=============================================================================
if(data.get(position).checked.equals("1")) {
itemChecked[position] = true;
}else{
itemChecked[position] = false;
}
if (itemChecked[position]) {
h.check.setChecked(true);
((VasNBIActivity) context).addChecked(data.get(position).id);
// h.check.setBackgroundResource(R.drawable.button_background);
}else {
h.check.setChecked(false);
}
h.check.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (h.check.isChecked()&& data.get(position).checked.equals("0")) {
itemChecked[position] = true;
((VasNBIActivity) context).addChecked(data.get(position).id);
// h.check.setBackgroundResource(R.drawable.button_background);
data.get(position).checked = "1";
}
else {
itemChecked[position] = false;
((VasNBIActivity) context).removeChecked(data.get(position).id);
// h.check.setBackgroundResource(R.drawable.button_ackgroun1);
data.get(position).checked="0";
}
}
});
//for spinner type questions
}else {
h.check.setVisibility(View.GONE);
h.radio.setVisibility(View.GONE);
h.spinner.setVisibility(View.VISIBLE);
h.btn.setVisibility(View.VISIBLE);
h.btn.setText(data.get(position).answere);
h.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
data.get(position).spinner_value = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
return v;
}
private class Holder {
CheckBox check;
RadioButton radio;
Spinner spinner;
Button btn;
}
}
And here is the code for radio button:
if(mpref.GetquestionType().equals("Radio")){
h.check.setVisibility(View.GONE);
h.radio.setVisibility(View.VISIBLE);
h.spinner.setVisibility(View.GONE);
h.btn.setVisibility(View.GONE);
h.radio.setText(data.get(position).answere);
if(data.get(position).checked.equals("1")){
h.radio.setBackgroundResource(R.drawable.button_background);
h.radio.setChecked(true);
}else{
h.radio.setBackgroundResource(R.drawable.button_ackgroun1);
h.radio.setChecked(false);
}
h.radio.setTag(position);
h.radio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = (Integer) v.getTag();
Log.i("ID of radiobutton","Order Edit # position : " + pos);
if (position != mSelectedPosition && mSelectedRB != null) {
mSelectedRB.setChecked(false);
((VasNBIActivity) context).removeChecked(data.get(mSelectedPosition).id);
mSelectedRB.setBackgroundResource(R.drawable.button_ackgroun1);
/*data.get(mSelectedPosition).checked="0";*/
}
mSelectedPosition = position;
((VasNBIActivity) context).addChecked(data.get(mSelectedPosition).id);
h.radio.setBackgroundResource(R.drawable.button_background);
/* data.get(mSelectedPosition).checked="1";*/
mSelectedRB = (RadioButton) v;
}
});
if (mSelectedPosition != position) {
h.radio.setChecked(false);
}else {
h.radio.setChecked(true);
if(mSelectedRB != null && h.radio != mSelectedRB){
mSelectedRB = h.radio;
}
}
I am trying to check button if its checked value=1 in list model.
addChecked and remove checked functions:
public void addChecked(String val) {
checkedValue.add(val);
}
public void removeChecked(String val) {
checkedValue.remove(val);
}
How can I achieve this??
You check button if its checked value=1 in this code.
// #1
if(data.get(position).checked.equals("1")){
h.radio.setBackgroundResource(R.drawable.button_background);
h.radio.setChecked(true);
}else{
h.radio.setBackgroundResource(R.drawable.button_ackgroun1);
h.radio.setChecked(false);
}
But, you change the state again this code
// #2
if (mSelectedPosition != position) {
h.radio.setChecked(false);
}else {
h.radio.setChecked(true);
if(mSelectedRB != null && h.radio != mSelectedRB){
mSelectedRB = h.radio;
}
}
So, remove your code(#2) and try again
I have a listview that clicking on an element shows a DialogFragmen with several options, options that take the user will be shown in the item of listview TextView and I get the following error in execution:
Activity app.gepv.Inventario has leaked IntentReceiver com.immersion.android.haptics.HapticFeedbackManager$HapticFeedbackBroadcastReceiver#426e94b8 that was originally registered here. Are you missing a call to unregisterReceiver()?
Enter the code that I think is important, if you need anything else edit the question :)
This is mi DialogFragment:
final String[] items= equiDisp.toArray(new String[equiDisp.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Asigne equipo/equipos:")
.setOnKeyListener(new Dialog.OnKeyListener(){
public boolean onKey(DialogInterface arg0, int keyCode,KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK)
{
finish();
//dialog.dismiss();
actualizarDisplay();
}
return true;
}
})
.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int item, boolean isChecked) {
Log.i("Dialogos", "OpciĆ³n elegida: " + items[item]);
if(isChecked)
{
marcado.add(items[item]);
Log.i("Dialogos", "Marcado: " + items[item]);
obras.get(pulsado).equiA.add(Integer.parseInt(items[item]));
for( int k=0; k< obras.get(pulsado).equiA.size(); k++)
{
Log.i("Dialogos", "Equipos: " + obras.get(pulsado).equiA.get(k) );
}
}
}
});
So far everything is running well because I check with Log.i
This is the function ActualizarDisplay():
public void actualizarDisplay()
{
adapter = new ObrasAdapter(this, obras);
lvObras = (ListView) findViewById(R.id.lvItems);
lvObras.setAdapter(adapter);
lvObras.setOnItemClickListener(this);
}
And this is my custom dataApdapter for the listview:
public class ObrasAdapter extends ArrayAdapter<Obra> {
private Context context;
private ArrayList<Obra> datos;
public ObrasAdapter(Context context, ArrayList<Obra> datos) {
super(context, R.layout.listview_item, datos);
this.context = context;
this.datos = datos;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View item = convertView;
ObrasHolder holder;
if (item == null) {
item = LayoutInflater.from(context).inflate(R.layout.listview_item,
null);
holder = new ObrasHolder();
holder.foto = (ImageView) item.findViewById(R.id.imgAnimal);
holder.num = (TextView) item.findViewById(R.id.numC);
holder.iden = (TextView) item.findViewById(R.id.idenC);
holder.ubi = (TextView) item.findViewById(R.id.ubiC);
holder.hombres = (TextView) item.findViewById(R.id.homC);
holder.material = (TextView) item.findViewById(R.id.matC);
holder.eq1 = (TextView) item.findViewById(R.id.eq1);
holder.eq2 = (TextView) item.findViewById(R.id.eq2);
holder.eq3 = (TextView) item.findViewById(R.id.eq3);
holder.eq4 = (TextView) item.findViewById(R.id.eq4);
holder.fondo = (RelativeLayout) item.findViewById(R.id.fondobra);
item.setTag(holder);
}
holder = (ObrasHolder) item.getTag();
holder.foto.setImageResource(datos.get(position).getDrawableImageID());
if(datos.get(position).getPrioridad()==1)
{
holder.num.setTextColor(Color.RED);
holder.iden.setTextColor(Color.RED);
holder.ubi.setTextColor(Color.RED);
holder.hombres.setTextColor(Color.RED);
holder.material.setTextColor(Color.RED);
holder.eq1.setTextColor(Color.RED);
holder.eq2.setTextColor(Color.RED);
holder.eq3.setTextColor(Color.RED);
holder.eq4.setTextColor(Color.RED);
}
if(datos.get(position).getPrioridad()==2)
{
holder.num.setTextColor(Color.parseColor("#FF8000"));
holder.iden.setTextColor(Color.parseColor("#FF8000"));
holder.ubi.setTextColor(Color.parseColor("#FF8000"));
holder.hombres.setTextColor(Color.parseColor("#FF8000"));
holder.material.setTextColor(Color.parseColor("#FF8000"));
holder.eq1.setTextColor(Color.parseColor("#FF8000"));
holder.eq2.setTextColor(Color.parseColor("#FF8000"));
holder.eq3.setTextColor(Color.parseColor("#FF8000"));
holder.eq4.setTextColor(Color.parseColor("#FF8000"));
}
if(datos.get(position).getPrioridad()==3)
{
holder.num.setTextColor(Color.GREEN);
holder.iden.setTextColor(Color.GREEN);
holder.ubi.setTextColor(Color.GREEN);
holder.hombres.setTextColor(Color.GREEN);
holder.material.setTextColor(Color.GREEN);
holder.eq1.setTextColor(Color.GREEN);
holder.eq2.setTextColor(Color.GREEN);
holder.eq3.setTextColor(Color.GREEN);
holder.eq4.setTextColor(Color.GREEN);
}
holder.num.setText(datos.get(position).getNum());
holder.iden.setText(datos.get(position).getIden());
holder.ubi.setText(datos.get(position).getUb());
holder.hombres.setText(datos.get(position).getHom());
holder.material.setText(datos.get(position).getMat());
if(datos.get(position).getEstado()==1)
{
holder.fondo.setBackgroundColor(Color.GREEN);
holder.num.setTextColor(Color.WHITE);
holder.iden.setTextColor(Color.WHITE);
holder.ubi.setTextColor(Color.WHITE);
holder.hombres.setTextColor(Color.WHITE);
holder.material.setTextColor(Color.WHITE);
holder.eq1.setTextColor(Color.WHITE);
holder.eq1.setTextColor(Color.WHITE);
holder.eq1.setTextColor(Color.WHITE);
holder.eq1.setTextColor(Color.WHITE);
}
if(! datos.get(position).equiA.isEmpty())
{
for(int i=0; i<datos.get(position).equiA.size();i++)
{
if(i == 0)
{
holder.eq1.setText(String.valueOf(datos.get(position).equiA.get(i)));
}
if(i == 1)
holder.eq2.setText(String.valueOf(datos.get(position).equiA.get(i)));
if(i == 2)
holder.eq3.setText(String.valueOf(datos.get(position).equiA.get(i)));
if(i == 3)
holder.eq4.setText(String.valueOf(datos.get(position).equiA.get(i)));
}
}
else
{
holder.eq1.setVisibility(View.INVISIBLE);
holder.eq2.setVisibility(View.INVISIBLE);
holder.eq3.setVisibility(View.INVISIBLE);
holder.eq4.setVisibility(View.INVISIBLE);
}
return item;
}
}
Can anyone help me?
I think i must to do something as:
#Override
protected void onStop()
{
unregisterReceiver(sendBroadcastReceiver);
unregisterReceiver(deliveryBroadcastReceiver);
super.onStop();
}
I have displayed all the contacts within the phone along with a check box in a list view. Now when the user checks say A and B and clicks on "ok" button, then what I want is to display the list again when making all the check box checked value to false. For that, I have created a method but when I call this method the value of the selected contacts is set to unchecked only when the list is scrolled, else it remains unchecked.
Whats the problem with my code???
Code
public void getContactSync(Context context, ArrayList<ContactModel> data) {
setListAdapter(null);
contactListAdapter = new ContactListAdapter(context, data);
setListAdapter(contactListAdapter);
// contactListAdapter.notifyDataSetChanged();
}
On OK button click
Arrays.fill(ContactListAdapter.contacts, 0);
contactListFragment.getContactSync(getActivity(), dbHandler.getAlGetContacts());
Custom Adapter
public class ContactListAdapter extends BaseAdapter {
private Context context;
private ArrayList<ContactModel> data;
DbHandler dbHandler;
public static int[] contacts;
static ArrayList<String> contactsSepetrated;
public static ArrayList<String> contactsId;
public ContactListAdapter(Context context, ArrayList<ContactModel> data) {
this.context = context;
this.data = data;
contacts = new int[data.size()];
contactsSepetrated = new ArrayList<String>();
contactsId = new ArrayList<String>();
}
#Override
public int getCount() {
return data.size();
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(final int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
dbHandler = new DbHandler(context);
if (view == null) {
holder = new ViewHolder();
view = LayoutInflater.from(context).inflate(R.layout.contact_custom_list, viewGroup, false);
holder.tvContact = (TextView) view.findViewById(R.id.tv_contact_name);
holder.checkBox = (CheckBox) view.findViewById(R.id.cb_contact_checkbox);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (compoundButton == holder.checkBox) {
if (b) {
contacts[i] = 1;
//dbHandler.updateContactList(data.get(i).getUserID(), 1);
//
} else {
contacts[i] = 0;
}
}
}
}
);
holder.checkBox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (contacts[i] == 1) {
contactsSepetrated.add(data.get(i).getContactName());
Log.e("Contact values", contactsSepetrated.toString());
contactsId.add(data.get(i).getUserID());
Log.e("Position", "" + i);
} else if (contacts[i] == 0) {
contactsSepetrated.remove(data.get(i).getContactName());
contactsId.remove(data.get(i).getUserID());
Log.e("Contact values", contactsSepetrated.toString());
Log.e("Position", "" + i);
}
ShareWithinpocketDocs.etContactsList.setText(contactsSepetrated.toString().subSequence(1, contactsSepetrated.toString().length() - 1));
}
});
if (contacts[i] == 0) {
holder.checkBox.setChecked(false);
// emailSeperated.remove(data.get(i).getEmail());
// Log.e("Email values", emailSeperated.toString());
// ShareWithinpocketDocs.etEmailLists.setText(emailSeperated.toString());
} else {
holder.checkBox.setChecked(true);
// emailSeperated.add(data.get(i).getEmail());
// Log.e("Email values", emailSeperated.toString());
}
holder.tvContact.setText(data.get(i).getContactName());
return view;
}
private class ViewHolder {
TextView tvContact;
CheckBox checkBox;
}
}
on click of checkbox inside adapter just call notifydatasetchanged() it will solve your problem
holder.checkBox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton,
boolean b) {
if (compoundButton == holder.checkBox) {
if (b) {
contacts[i] = 1;
// dbHandler.updateContactList(data.get(i).getUserID(),
// 1);
//
notifyDataSetChanged();
} else {
contacts[i] = 0;
notifyDataSetChanged();
}
}
}
}
);
if you want to refresh adapter on ok button click add this to ok button click
adapter.notifydatasetchagned()
Try to call setListAdapter(contactListAdapter); again inside your onClick()
Hi i have listview of songs with checkbox using Aquery i am unable to save the State of chekckbox while list scrolldown. i have two arraylist one is for by clicking song image & other is for checkbox if user select the chekbox it should play song accordingly to checkbox arraylist. i have used this also check.setChecked(SelectedBox.get(clickedposition).getisCheck()); . But no help
so far what i have done is here
private void createList() {
listAq = new AQuery(this);
adapter = new ArrayAdapter<Audio>(SongList.this, R.layout.list_row,details) {
#Override
public View getView(final int position, View convertView,final ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_row, parent, false);
}
final int clickedposition = position;
final AQuery aq = listAq.recycle(convertView);
Audio item = getItem(position);
aq.id(R.id.MovieName).text(item.getAlbumName());
aq.id(R.id.SongName).text(item.getTitle());
aq.id(R.id.checkBox1).getCheckBox().setSelected(item.getisCheck());
aq.id(R.id.nameLayout).clicked(new OnClickListener() {
#Override
public void onClick(View v) {
DialogBuilder(position);
}
});
check = (CheckBox) convertView.findViewById(R.id.checkBox1);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
try {
if (isChecked) {
checkbox = true;
if (details.get(clickedposition).getisCheck() == false) {
SelectedBox.add(details.get(clickedposition));
System.out.println(SelectedBox.size());
details.get(clickedposition).setisCheck(true);
}
} else {
final String name = details.get(clickedposition).getFilename();
for (int i = 0; i <= SelectedBox.size(); i++) {
if (name.equals(SelectedBox.get(i).getFilename())) {
SelectedBox.remove(i);
details.get(clickedposition).setisCheck(false);
}
}
}
notifyDataSetChanged();
}
catch (Exception e) {
e.getMessage();
}
}
});
try{
}
catch (Exception e) {
// TODO: handle exception
}
url = "drawable/" + item.getThumbnail();
imageResource = getResources().getIdentifier(url, null,getPackageName());
aq.id(R.id.song_image).image(imageResource);
if (position != playingValue) {
aq.id(R.id.imageplay).invisible();
aq.id(R.id.imageplay).getImageView().bringToFront();
} else {
aq.id(R.id.imageplay).visible();
}
ImageView img = (ImageView)convertView.findViewById(R.id.song_image);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
checkbox = false;
SelectedBox.clear();
currentSongIndex = position;
try {
if (ChannelService.playerFlag == 0) {
playSong(currentSongIndex);
} else if ((ChannelService.playerFlag == 1)
|| ((ChannelService.playerFlag == 2))) {
if (currentSongIndex != playingValue) {
try {
final Intent serviceIntent = new Intent(
getApplicationContext(),
ChannelService.class);
stopService(serviceIntent);
mPlay.setBackgroundResource(R.drawable.play_btn);
playSong(currentSongIndex);
} catch (final IllegalStateException e) {
e.printStackTrace();
}
}
mPlay.setBackgroundResource(R.drawable.pause_btn);
final Intent in = new Intent("updateNotification");
sendBroadcast(in);
}
} catch (final IllegalStateException e) {
e.getMessage();
}
notifyDataSetChanged();
}
});
return convertView;
}
};
aq.id(R.id.listView1).adapter(adapter);
}
When you scroll down a list in Android, it destroys the rows that doesn't fit in the screen.
I allways use an array of objects to save the state of each row so when android calls getView you can ask the state of them:
public class AudioRow{
public int id;
public boolean checked;
...
}
Then at getView you can do something like that:
public View getView(final int position, View convertView,final ViewGroup parent) {
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.list_row, parent, false);
}
final int clickedposition = position;
AudioRow ar = audioRows.get(position);
if(ar.checked) {
//do something;
}
...
#Override
public void onClick(View v) {
if(ar.checked) ar.checked = false;
else ar.checked = true;
DialogBuilder(position);
}
});
I hope it helps you.