Android - How to get checkbox to work with sqlite database - android

I am trying to create a listview with checkbox, and user can check the options to select which customer to pay the particular expense. I created the listview with checkbox, the problem is how to access database from my custom adapter class? It seems like i can't declare my sqlite database in that class. But i would like to add the expense to checked customer by referring to the customerId. How can i do that? I stuck in onCheckChanged method in my custom adapter class. Sorry for my bad english. Thanks
This is my AddExpense class:
public class AddExpense extends ListActivity implements OnClickListener {
EditText expenseName;
Spinner expenseType;
EditText expensePrice;
EditText expenseQuantity;
EventController controller = new EventController(this);
Button btnadd;
ListView lv;
#SuppressWarnings("unchecked")
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addexpense);
expenseName = (EditText) findViewById(R.id.expenseName);
expenseType = (Spinner) findViewById(R.id.expenseType);
// Create an ArrayAdapter using the string array and a default spinner
// layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.type, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
expenseType.setAdapter(adapter);
expensePrice = (EditText) findViewById(R.id.expensePrice);
expenseQuantity = (EditText) findViewById(R.id.expenseQuantity);
btnadd = (Button) findViewById(R.id.btnaddexp);
btnadd.setOnClickListener(this);
#SuppressWarnings("rawtypes")
ArrayList person = new ArrayList();
person.clear();
Intent objIntent = getIntent();
String eventId = objIntent.getStringExtra("eventId");
String query = "SELECT * FROM friends WHERE friendEvent = " + eventId;
Cursor c1 = controller.selectQuery(query);
if (c1 != null & c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
getParticipant person1 = new getParticipant();
person1.setfriendId(c1.getString(c1
.getColumnIndex("friendId")));
person1.setfriendName(c1.getString(c1.getColumnIndex("friendName")));
person.add(person1);
} while (c1.moveToNext());
}
}
c1.close();
ListAdapter listadapter = new ListAdapter(AddExpense.this,person);
adapter.notifyDataSetChanged();
setListAdapter(listadapter);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
HashMap<String, String> queryValues = new HashMap<String, String>();
Intent objIntent = getIntent();
String eventId = objIntent.getStringExtra("eventId");
queryValues.put("eventId", eventId);
queryValues.put("expenseName", expenseName.getText().toString());
queryValues
.put("expenseType", expenseType.getSelectedItem().toString());
queryValues.put("expensePrice", expensePrice.getText().toString());
queryValues
.put("expenseQuantity", expenseQuantity.getText().toString());
controller.insertExpense(queryValues);
this.callHomeActivity(v);
finish();
}
public void callHomeActivity(View view) {
super.onResume();
}
This is my custom adapter code:
public class ListAdapter extends BaseAdapter implements OnCheckedChangeListener{
Context ctx;
LayoutInflater lInflater;
ArrayList<getParticipant> objects;
ListAdapter(Context context, ArrayList<getParticipant> friendList) {
ctx = context;
objects = friendList;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return objects.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return objects.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) {
getParticipant person = objects.get(position);
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.view_expenses_participant_entry, parent, false);
}
TextView friendId = (TextView)view.findViewById(R.id.friendId);
friendId.setText(person.getfriendId());
TextView friendName = (TextView)view.findViewById(R.id.friendName);
friendName.setText(person.getfriendName());
CheckBox cbperson = (CheckBox)view.findViewById(R.id.list_checkbox);
cbperson.setOnCheckedChangeListener(this);
return view;
}
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
}

why can't you access your database here, You have Context of your activity in ctx variable, you can access db in OnCheckedChanged() method, See below
#Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
YOURSQLITEdatabase db = new YOURSQLITEdatabse(ctx);
db.performYourOperationsHere();
db.close();
}

Cursor Adapter should simplify things for you or you can add the id as tag in the check box view, take a look at setTag and then get the tag in your onCheckedChanged and
do operations in onCheckedChanged OR
keep storing it in an array or removing it depending on weather it's checked or unchecked and use this array of ids to perform db operations when adding expense on click of submit button, if you have something like that on your screen as doing db operation on every click will slow things for the user
Am not sure if you are doing this the correct way, Better would be if you study cursor adapters first.

Related

How to update values in database using an update image button in custom list adapter?

This is my custom list adapter. I want to update the values in table using the update ImageButton in the list. On clicking it, the old values should be shown in a new activity and then the edited value must be stored in the database. However, I am unable to pass an intent inside the onClick() method.
Please suggest me a solution
public class CustomListAdapter extends BaseAdapter implements ListAdapter
{
private ArrayList<String> list = new ArrayList<String>();
private Context context;
OnItemSelectedListener onItemSelectedListener;
public int pos;
String pass,pass2,edit,epass;
public CustomListAdapter(List list, Context context) {
this.list = (ArrayList<String>) list;
this.context = context;
}
#Override
public int getCount() {
return list.size();
}
#Override
public Object getItem(int pos) {
//pass2 = list.toString();
return list.get(pos);
}
//#Override
//public Long getItemId(int pos) {
//
// //just return 0 if your list items do not have an Id variable.
//}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.layout_custom_list, null);
}
//Handle TextView and display string from your list
final TextView listItemText = (TextView)view.findViewById(R.id.list_item_string);
listItemText.setText(list.get(position));
//Handle buttons and add onClickListeners
ImageButton deleteBtn = (ImageButton)view.findViewById(R.id.delete_btn);
ImageButton editBtn = (ImageButton)view.findViewById(R.id.edit_btn);
//Button addBtn = (Button)view.findViewById(R.id.add_btn);
deleteBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something
list.remove(position);
pass = listItemText.getText().toString();
notifyDataSetChanged();
pass2 = pass.substring(0,pass.indexOf(' '));
System.out.println(pass2);
Moneydb.delete(pass2);
}
});
editBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v2) {
// TODO Auto-generated method stub
edit=listItemText.getText().toString();
epass = listItemText.getText().toString();
edit = epass.substring(0,epass.indexOf(' '));
Moneydb.edit(edit);
}
});
return view;
}
protected Context getContext() {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
//return list.get(position).getId();
return 0;
}
public void clear() {
//CustomListAdapter collection = null;
// TODO Auto-generated method stub
list.clear();
notifyDataSetChanged();
}
I suggest you to assign and ContextMenu to your list view with two MenuItem, Edit and Delete and write associated code outside of adapter
or you can start Activity by :
Intent new_intent = new Intent(v.getRootView().getContext(),edit_activity.class);
new_intent.putExtra("Key","Value");
v.getRootView().getContext().startActivity(new_intent);
i think the first method is best ;)

How to refresh Listview within BaseAdapter? [duplicate]

This question already has answers here:
how to refresh custom listview using baseadapter in android
(5 answers)
Closed 8 years ago.
I have application this application contain listview , Listview retrieve data from local database sqlite , by using adapter this is my Adapter class code :
public class CommandsListAdapter extends BaseAdapter {
public static String ID = null;
private List<String> data;
ArrayList<HashMap<String, String>> commandList = new ArrayList<HashMap<String, String>>();
private ArrayAdapter<String> listAdapter;
Context context;
public CommandsListAdapter(Context con,
ArrayList<HashMap<String, String>> list) {
commandList = list;
context = con;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return commandList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return commandList.get(arg0);
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(final int index, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.people_list, null);
}
TextView tvTime = (TextView) convertView.findViewById(R.id.timeOfBlock);
TextView tvName = (TextView) convertView.findViewById(R.id.name);
tvName.setText(commandList.get(index).get(MyDatabase.Number_Block));
tvTime.setText(commandList.get(index).get(MyDatabase.Time_Of_Block));
Typeface tf = Typeface.createFromAsset(context.getAssets(),
"segoeuil.ttf");
tvTime.setTypeface(tf);
tvName.setTypeface(tf);
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// Message.message(context,
// commandList.get(index).get(MyDatabase.Block_Table_Id));
Intent i = new Intent(context, MessageDetail.class);
i.putExtra(ID,
commandList.get(index).get(MyDatabase.Block_Table_Id)
.toString());
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// context.startActivity(i);
}
});
convertView.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
ID=commandList.get(index).get(MyDatabase.Block_Table_Id);
removeListItem(v, index);
return false;
}
});
return convertView;
}
protected void removeListItem(View rowView, final int positon) {
final Animation animation = AnimationUtils.loadAnimation(
context, android.R.anim.slide_out_right);
rowView.startAnimation(animation);
Handler handle = new Handler();
handle.postDelayed(new Runnable() {
#Override
public void run() {
commandList.remove(positon);
MyDatabase db=new MyDatabase(context);
db.open();
// db.deleteBlock(ID);
db.close();
Message.message(context, ID);
}
}, 1000);
}
}
and this is my Activity Class code :
public class ListOfBlock extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_of_block);
ActionBar actionBar = getActionBar();
// for color
actionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#a20000")));
if (actionBar != null) {
actionBar.setTitle(getResources().getString(
R.string.title_activity_list_of_block));
actionBar.setDisplayHomeAsUpEnabled(true);
// actionBar.setIcon(R.drawable.ic_launcher);
}
ListView lvCommands = (ListView) findViewById(R.id.ListOfBlockerListView);
lvCommands.setAdapter(new CommandsListAdapter(getApplicationContext(),
new MyDatabase(getApplicationContext()).getBlockers()));
}
}
now I want to refresh the listview within getview method , I don't know how to do this , and I found some answer but really this is not helped me , thanks for any help .
YourActivity.this.recreate();
This will recreate your list. Keep a variable say n=false in the actvity where you perfom those operations. Then when you call back the activity with list, just pass this as true.
In the activiy where you perfom those operations,
Intent i = new Intent(Operations.this,ListActivity.class);
ListActivity.n=true;
startActivity(i);
In onCreate of ListActivity create a class variable boolean n=false;
if(n){
ListActivity.this.recreate();
}
Hope it helps. Cheers!
I hope this is help you , within the class adapter do this :-
ArrayList<HashMap<String, String>> commandList = new ArrayList<HashMap<String, String>>();
private ArrayList<HashMap<String, String>> searchArrayList;
public CommandsListAdapter(Context con,
ArrayList<HashMap<String, String>> list) {
commandList = list;
context = con;
searchArrayList=list;
}
and create a function for update your listview like this , also within Adapter Class :-
public void updateResults(ArrayList<HashMap<String, String>> results) {
searchArrayList = results;
//Triggers the list update
notifyDataSetChanged();
}
and any where you want to update your listview call this function , and I did this for you Sister , like this :-
updateResults(commandList);
one thing for sure these codes above this is all your code but I made some modify , and the parameter of updateResults function commandList this is your ArrayAdapter you defined already .
I hope this is work
Simply calling notifyDataSetChanged() of BaseAdapter class refresh the list view items. Call it appropriately based upon your requirement.
Cheers.

Update listview when run onResume() in Android

I create a listview get all contact from address book of Android. I try to update listview by onResume() but It's not working.
Here is my code:
public class Display extends Activity implements OnItemClickListener{
List<String> name1 = new ArrayList<String>();
List<String> phno1 = new ArrayList<String>();
List<String> email1 = new ArrayList<String>();
MyAdapter ma ;
ListView lv;
int countContact;
TextView textViewManyCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
getAllContacts(this.getContentResolver());
lv= (ListView) findViewById(R.id.lv);
ma = new MyAdapter();
lv.setAdapter(ma);
lv.setOnItemClickListener(this);
lv.setItemsCanFocus(false);
lv.setTextFilterEnabled(true);
textViewManyCount = (TextView) findViewById(R.id.textViewManyCount);
}
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
}
public void getAllContacts(ContentResolver cr) {
Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
countContact = phones.getCount();
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String email = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
System.out.println(".................."+phoneNumber);
name1.add(name);
phno1.add(phoneNumber);
email1.add(email);
}
phones.close();
}
class MyAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener
{ private SparseBooleanArray mCheckStates;
LayoutInflater mInflater;
TextView textViewName,textViewPhone,textViewEmail;
MyAdapter()
{
mCheckStates = new SparseBooleanArray(name1.size());
mInflater = (LayoutInflater)Display.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return name1.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = mInflater.inflate(R.layout.list_user, null);
textViewName = (TextView) vi.findViewById(R.id.textViewName);
textViewPhone = (TextView) vi.findViewById(R.id.textViewPhone);
textViewEmail = (TextView) vi.findViewById(R.id.textViewEmail);
textViewName.setText("Name :"+ name1.get(position));
textViewPhone.setText("Phone No :"+ phno1.get(position));
textViewEmail.setText("Email :"+ email1.get(position));
textViewManyCount.setText("Contacts: "+countContact);
return vi;
}
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
}
}
When I running app, It's load all contact but when I click button Home and change some information in Address book then click icon app to open app again. And onResume() method not working.
Here my onResume()
#Override
protected void onResume()
{
super.onResume();
if (lv != null)
{
updateData();
}
}
private void updateData()
{
getAllContacts(this.getContentResolver());
ma = new MyAdapter();
lv.setAdapter(ma);
ma.notifyDataSetChanged();
}
So some body help me! Plz!
Try by Replacing your updateData():
private void updateData()
{
getAllContacts(this.getContentResolver());
ma.notifyDataSetChanged();
}
You may be referencing an old ListView (lv) that is not the new ListView you see when you resume your Activity. Try getting the reference to lv again using Activity.findViewById(R.id.xxxx) in onResume().
You could also try reusing the old adapter instead of creating a brand new one (by clearing it and updating it's content). If you're reusing the old adapter, just make sure you give the old adapter to the new ListView using ListView.setAdapter(ma).
You should also have MyAdapter.getItemId(int position) return position instead of 0.

save editbox values in the custom listview

am having a two editbox. the values enter in that has to placed in the custom listview its working fine for this...
but i want to save this values in the list view permanently.so that if the phones switches off and again run the app then the values have to be saved ..like phone contact number and name have to be saved..
here am ataching the code
public class Editcard extends Activity{
EditText cardname,cardDescription ;
MyApplication app;
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.editcredit);
app = ((MyApplication) getApplicationContext());
cardname =(EditText)findViewById(R.id.cardash);
cardDescription =(EditText)findViewById(R.id.editdescription);
Button save =(Button)findViewById(R.id.save);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
app.carddata =cardname.getText().toString();
System.out.println("Gotcardname"+app.carddata);
app.descriptiondata =cardDescription.getText().toString();
System.out.println("gotDescription"+app.descriptiondata);
app.arryList.add(app.carddata);
app.arryList1.add(app.descriptiondata);
Intent saveIntent =new Intent(Editcard.this,Newcard.class);
startActivity(saveIntent);
}
});
}
}
public class MyApplication extends Application{
ArrayList<String> arryList = new ArrayList<String>();
ArrayList<String> arryList1 = new ArrayList<String>();
String carddata,descriptiondata,=null;
}
public class Newcard extends Activity {
MyAdapter adapter;
ListView listView;
LayoutInflater lay;
MyApplication app;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newcard);
app = ((MyApplication) getApplicationContext());
TextView newcard =(TextView)findViewById(R.id.newcard);
newcard.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent card = new Intent(Newcard.this,Editcreditcard.class);
startActivity(card);
}
});
listView = (ListView) findViewById(R.id.cardlist);
adapter =new MyAdapter(this, app.arryList,app.arryList1);
listView.setAdapter(adapter);
}
public class MyAdapter extends BaseAdapter {
Context context = null;
ArrayList<String> items= null;
ArrayList<String> items1= null;
public MyAdapter(Newcard newcard, ArrayList<String> items,
ArrayList<String> items1) {
// TODO Auto-generated constructor stub
this.items = items;
this.items1 = items1;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items;
//return items.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View layout = null;
TextView produ = null;
TextView desc = null;
if (convertView == null) {
lay = LayoutInflater.from(getApplicationContext());
layout = lay.inflate(R.layout.customlist, null);
} else {
layout = convertView;
}
produ = (TextView) layout.findViewById(R.id.card);
produ.setText("" +app.arryList.get(position));
desc= (TextView) layout.findViewById(R.id.descrip);
desc.setText("" +app.arryList1.get(position));
return layout;
}
}
}
use can use SharedPreferences, that will save your values permanently, untill they are changed(by changing values in editbox) or user reinstall (clear data) application. Shared Preferences works like this
// save string in sharedPreferences
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = settings.edit();
editor.putString("some_key", string); // here string is the value you want to save
editor.commit();
// restore string in sharedPreferences
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
string = settings.getString("some_key", "");

ListView adapter with HashMap

I have a listview with textview and checkbox. The text values are stored in a HashMap. Data is retrieved from a DB. Map key is the ID and Map value is a Location. When I pass the HashMap to the adapter, it starts printing from position 0 (zero). Because of this I get a null text(with no value) and miss one value(the last one) in the HashMap. Can't I use a HashMap as the data source?
EDIT: Here is the code
public class LocationActivity extends Activity{
myAdapter myA;
Map<Integer,String> locLables;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.locationmain);
ListView listview = (ListView) findViewById(R.id.listView1);
String selectQuery = "SELECT * FROM " + DatabaseHandler.TABLE_LOCATIONLABLES;
SQLiteDatabase db = new DatabaseHandler(this).getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
locLables = new HashMap<Integer, String>();
if(cursor != null){
if (cursor.moveToFirst()) {
do {
locLables.put(new Integer(cursor.getString(0)),cursor.getString(1));
System.out.println(cursor.getString(0)+" "+ cursor.getString(1));
} while (cursor.moveToNext());
}
}
cursor.close();db.close();
//System.out.println(locLables);
myA = new myAdapter(this, locLables, listview);
listview.setAdapter(myA);
listview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if(arg2 == 0 )
arg2 = arg2+1;
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), locLables.get(arg2), Toast.LENGTH_SHORT).show();
}
});
}
Here is the Adapter class
class myAdapter extends BaseAdapter{
ListView listView;
Activity cntx;
Map<Integer,String> locations;
List<Integer> locids = new LinkedList<Integer>();
public myAdapter(Activity context,Map<Integer,String> locLables, ListView lv){
cntx = context;
locations = locLables;
listView = lv;
System.out.println(locations);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return locations.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row=null;
final int posi = position;
LayoutInflater inflater=cntx.getLayoutInflater();
row=inflater.inflate(R.layout.locationmain_entry, null);
TextView tv = (TextView)row.findViewById(R.id.textView12);
final CheckBox cb = (CheckBox)row.findViewById(R.id.checkBox12);
if(locids.contains(posi))
cb.setChecked(true);
//here I get a null value as the first element and misses the last value..
tv.setText(locations.get(position));
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
System.out.println("Chedked" + posi+ locations.get(posi));
//cb.setChecked(true);
if(!locids.contains(posi))
locids.add(posi);
//System.out.println(locids.get(posi));
}else if(!isChecked){
//cb.setChecked(false);
if(locids.contains(posi))
locids.remove(new Integer(posi));
System.out.println("NOt checked" +posi + locations.get(posi));
}
}
});
return row;
}
It runs for the number of elements in the Map. As it is using a 0 (by position variable) it misses the last value as well.
What you can do is
ArrayList<Hashmap<Integer,String>> myList = new ArrayList<Hashmap<Integer,String>>();
if(cursor != null){
if (cursor.moveToFirst()) {
do {
locLables = new HashMap<Integer, String>();
locLables.put(new Integer(cursor.getString(0)),cursor.getString(1));
myList.add(locLables);
} while (cursor.moveToNext());
}
}
cursor.close();db.close();
myA = new myAdapter(this, myList, listview);
listview.setAdapter(myA);
You will have to change your "myAdapter" Adapter's constructor to hold "ArrayList> myList" instead of "Map locLables"..
This will help... Let me know if the problem still persists..

Categories

Resources