I am trying to refresh my list view after deleting items from it, but i am not geting the desired result. The items are getting deleted when i reload the activity but not at the time i press the delete button. Here is my code:
public void deletelist()
{
mylist = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<favtitle.size();i++)
{
map = new HashMap<String, Object>();
map.put("train",favtitle.get(i));
map.put("value",favloc.get(i));
map.put("employer",favemp.get(i));
mylist.add(map);
}
mSchedule = new SimpleAdapter(this, mylist, R.layout.listdelete,
new String[] {"train","value","employer"}, new int[] {R.id.dept,R.id.jobloc,R.id.employer});
lv.setAdapter(mSchedule);
lv.setDividerHeight(2);
lv.setCacheColorHint(Color.WHITE);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,View view,int position,long id)
{
String str3 = favtitle.get(position);
db.delete("Favorites7","title= '"+str3+"'",null);
db.close();
//Data Base Select
db = openOrCreateDatabase(
"ClassifiedAds.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
//End-----
cur= db.query("Favorites7",
null, null, null, null, null, null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
favtitle.add(cur.getString(1));
favloc.add(cur.getString(3));
favemp.add(cur.getString(2));
lat.add(cur.getString(7));
log.add(cur.getString(8));
cur.moveToNext();
}
cur.close();
//mylist = new ArrayList<HashMap<String,Object>>();
HashMap<String,Object> map = new HashMap<String, Object>();
for(int i=0;i<favtitle.size();i++)
{
map = new HashMap<String, Object>();
map.put("train",favtitle.get(i));
System.out.println("==="+favtitle.get(i));
map.put("value",favloc.get(i));
System.out.println("==="+favloc.get(i));
map.put("employer",favemp.get(i));
System.out.println("==="+favemp.get(i));
mylist.add(map);
}
mSchedule.notifyDataSetChanged();
mSchedule = new SimpleAdapter(Favorites.this, mylist, R.layout.listdelete,
new String[] {"train","value","employer"}, new int[] {R.id.dept,R.id.jobloc,R.id.employer});
lv.setAdapter(mSchedule);
lv.setDividerHeight(2);
lv.setCacheColorHint(Color.WHITE);
}
});
}
Any help would be appreciated.
Thanx in advance.
Use notifyDataSetChanged() method with the listview within the condition where you are deleting the record , just after deleting the record put the listview.notifyDataSetChanged() code.
hope this will solve your problem.
Did you tried invalidateViews, it will redrawn you list view.
My problem is solved. What i had to do was that whenever the arraylist was reloading i had to clear the previous one.
Like:
array-list name.clear();
before adding items into the hashmap.
Related
When item of listview is clicked, it gets opened in another activity which has edittext. After editing an item, when I save it, item does not get updated in the listview but it inserts a new entry in listview.
How can I update existing item without inserting new?
Here is my code
Activity: TRList.class
ArrayList<HashMap<String, String>> items = new ArrayList<>();
List<TRListFormat> list = trDb.getAllReminders();
for (TRListFormat val : list) {
HashMap<String, String> map = new HashMap<>();
map.put("title",val.getTitle());
map.put("description", val.getDes());
map.put("date", val.getDate());
map.put("time", val.getTime());
items.add(map);
}
adapter = new SimpleAdapter(this, items, R.layout.tr_list_format,
new String[] { "title", "description", "date", "time" },
new int[] {R.id.tbr_title, R.id.tbr_des, R.id.tbr_date, R.id.tbr_time });
lv = (ListView)findViewById(R.id.tbr_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
Intent intent = new Intent(TRList.this, TRTimeReminder.class);
intent.putExtra("remId", (int)id);
startActivity(intent);
}
});
When listView item is clicked, it opens another activity
TRTimeReminder.class I have set save button in menu
case R.id.menu_tbr_done:
String title = edTitle.getText().toString();
String des = edDes.getText().toString();
String time = timeView.getText().toString();
String date = dateView.getText().toString();
Bundle extras = getIntent().getExtras();
if(extras != null) {
int value = extras.getInt("remId");
if (value > 0) {
trDb.updateReminder(new TRListFormat(value, title, des, date, time));
}
}
else{
trDb.addReminder(new TRListFormat(title, des, date, time));
}
Intent i = new Intent(getApplicationContext(), TRList.class);
startActivity(i);
edTitle.getText().clear();
edDes.getText().clear();
dateView.setText(currDate);
timeView.setText(currTime);
return true;
TRDBHelper.class
//Add new reminder
void addReminder(TRListFormat format){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_TITLE, format.getTitle());
values.put(COLUMN_DES, format.getDes());
values.put(COLUMN_DATE, format.getDate());
values.put(COLUMN_TIME, format.getTime());
db.insert(TABLE_NAME, null, values);
db.close();
}
//Update single reminder
public void updateReminder(TRListFormat format){
SQLiteDatabase db = this.getWritableDatabase();
int id = format.getId();
ContentValues values = new ContentValues();
values.put(COLUMN_ID, format.getId());
values.put(COLUMN_TITLE, format.getTitle());
values.put(COLUMN_DES, format.getDes());
values.put(COLUMN_DATE, format.getDate());
values.put(COLUMN_TIME, format.getTime());
db.update(TABLE_NAME, values, COLUMN_ID+ " = " +id, null);
db.close();
}
I think there's problem with your adapter. There are 2 possible issues.
Data of adapter are not updated. You're saying opposite, new value is added to ListView. Are you really sure?
You haven't called your adapter's method notifyDataSetChanged() after you've changed values.
I am using maps in my app. Everything is fine except adapter. I don't know why it is showing only the last element in all tabs.
Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
List<Map<String,String>> list = new ArrayList<Map<String,String >>();
Map<String,String> map = new HashMap<String, String>();
EmployeeDatabase empClick = new EmployeeDatabase(getApplicationContext());
Cursor cursor = empClick.getDetails();
if(cursor.moveToFirst()){
do{
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
map.put("name",name);
map.put("age",age);
list.add(map);
}while(cursor.moveToNext());
cursor.close();
}
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.show_data, new String [] {"name", "age"}, new int[] {R.id.name,R.id.age});
setListAdapter(adapter);
}
if(cursor.moveToFirst()){
do{
map = new HashMap<String, String>();<-------Add this line
String name = cursor.getString(cursor.getColumnIndex("name"));
String age = cursor.getString(cursor.getColumnIndex("age"));
map.put("name",name);
map.put("age",age);
list.add(map);
}while(cursor.moveToNext());
cursor.close();
}
public class ABC extends ListActivity {
static final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.abc_list);
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.abc_list_row,
new String[] {"AAA","BBB","CCC"},
new int[] {R.id.aa,R.id.bb, R.id.cc,}
);
add();
setListAdapter(adapter);
}
private void add() {
DatabaseA databaseHelper = new DatabaseA(this);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor c= db.rawQuery("select * from data", null);
HashMap<String,String> temp = new HashMap<String,String>();
while(c.moveToNext()){
Log.d("Debug", c.getString(2));
temp.put("AAA", c.getString(1));
temp.put("BBB", c.getString(2));
temp.put("CCC", c.getString(3));
list.add(temp);
}
c.close();
db.close();
for (Map.Entry<String, String> entry : temp.entrySet()) {
String xx= entry.getKey();
String yy = entry.getValue();
Log.d("data", xx+" : "+yy);
}
}
My Logcat:
D/Debug(657): New york
D/Debug(657): New moon
D/data(657): AAA : Aug 20, 2012 3:57:02 PM
D/data(657): BBB : New moon
D/data(657): CCC : Title
My problem is it always left out the first data from sqlite in this list. Besides, everytime i press a button to view this list, it will add those data into this list although they are already exist. I just want to view those data in sqlite in list form. Any way to solve this. Thanks.
You need to create your Map inside your while loop
while(c.moveToNext()){
Map<String,String> temp = new HashMap<String,String>();
Log.d("Debug", c.getString(2));
temp.put("AAA", c.getString(1));
temp.put("BBB", c.getString(2));
temp.put("CCC", c.getString(3));
list.add(temp);
}
Add cursor moveToFirst() like this:
Cursor c= db.rawQuery("select * from data", null);
HashMap<String,String> temp = new HashMap<String,String>();
c.moveToFirst();
while(c.moveToNext()){
Log.d("Debug", c.getString(2));
temp.put("AAA", c.getString(1));
temp.put("BBB", c.getString(2));
temp.put("CCC", c.getString(3));
list.add(temp);
}
And please post the source code when the button is clicked!
i m trying to get data from my database and present them to a listView.i m using two classes for my database:
1st class is the DBAdapter and the method i use to get data is:
public String[] getData()
{
// String[] columns =new String[]{DBHelper.ROWID, DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
String[] columns =new String[]{DBHelper.TITLE , DBHelper.AUTHOR, DBHelper.ISBN };
Cursor c=ourDatabase.query(DBHelper.DATABASE_TABLE, columns, null, null, null, null, null);
String result="";
String sa = null;
String sb = null;
String sc = null;
//int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
sa=c.getString(is1);
sb=c.getString(is2);
sc=c.getString(is3);
}
//Toast.makeText(HotOrNot.this, sa, Toast.LENGTH_SHORT).show();
return new String[] {sa,sb,sc};
}
2.The secind class in SQLView and thats the way i m trying to create my list
public class SQLView extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
HotOrNot entry2=new HotOrNot(this);
entry2.open();
String[] data2=entry2.getData();
entry2.close();
Toast.makeText(SQLView.this, data2[1].toString(), Toast.LENGTH_SHORT).show();
ListView list = (ListView) findViewById(R.id.list);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map = new HashMap<String, String>();
map.put("name",data2[0].toString());
map.put("address", data2[1].toString());
map.put("address2", data2[2].toString());
mylist.add(map);
// ...
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
data2, new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
list.setAdapter(mSchedule);
What i have to do in order to work?This is my first try to use database in android!
Now i just get an empty background...
EDIT:
With my toast i m getting the last item in position 1 that i added to db
Could anybody help me with the return statement in my DBHelper please?
Below is the fixed code. Try this out.
ListAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.row,
new String[] { "name", "address", "address2"},
new int[] {R.id.rtextView1,R.id.rtextView2,R.id.rtextView3});
list.setAdapter(mSchedule);
Basically you have to specify the columns/key of the map that you added. So it will map key from map to the corresponding view.
For looping issue, this will do the job:
for(int i=0; i<data2.length; i+=3) {
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("name",data2[i].toString());
map.put("address", data2[i+1].toString());
map.put("address2", data2[i+2].toString());
mylist.add(map);
}
The getData fix:
ArrayList<String> arrForData = new ArrayList<String>();
//int iRow=c.getColumnIndex(DBHelper.ROWID);
int is1=c.getColumnIndex(DBHelper.TITLE);
int is2=c.getColumnIndex(DBHelper.AUTHOR);
int is3=c.getColumnIndex(DBHelper.ISBN);
for (c.moveToFirst();!c.isAfterLast();c.moveToNext()){
//result=result+c.getString(is1)+" "+c.getString(is2)+" "+c.getString(is3)+"\n";
arrForData.add(c.getString(is1));
arrForData.add(c.getString(is2));
arrForData.add(c.getString(is3));
}
//Toast.makeText(HotOrNot.this, sa, Toast.LENGTH_SHORT).show();
return arrForData.toArray();
I have done Muticolum listview onListItemClick .Its working fine.Problem is
This line String selection = l.getItemAtPosition(position).toString(); return this
{routeName=TestRoute2, outlets=5, routeCode=VRT002} .I want to get it selected row's 3rd column value.How to get it.
Please help me..
Thanks in advance..
What kind of Adapter? E.g. for a SimpleCursorAdapter you would do the following:
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
long aLongValue = cursor.getLong(cursor.getColumnIndexOrThrow("anintegercolumn"));
ArrayList<HashMap<String, String>> historyArrayList;
SimpleAdapter histroyListAdapter;
HashMap<String, String> historyObjectMap;
for (CheckInCheckOutHistory checkOutHistoryObj : checkInCheckOutHistoryList) {
historyObjectMap = new HashMap<String, String>();
historyObjectMap.put("assetTag", checkOutHistoryObj.getAssetTag());
historyObjectMap.put("action", checkOutHistoryObj.getAction());
historyObjectMap.put("actionTime", checkOutHistoryObj.getActionDate());
if (checkOutHistoryObj.getAction().equals("Checked out")) {
historyObjectMap.put("gif", R.drawable.radio_button_yellow+ "");
} else {
historyObjectMap.put("gif", R.drawable.radio_button_green+ "");
}
historyArrayList.add(historyObjectMap);
}
histroyListAdapter = new SimpleAdapter(
ViewCheckInCheckOutHistory.this, historyArrayList,
R.layout.multi_colummn_list_text_style_small, new String[] {
"assetTag", "gif" , "action", "actionTime"},
new int[] { R.id.list_content_column1,
R.id.list_content_imagecolumn,
R.id.list_content_column3,
R.id.list_content_column4});
historyListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> historyObjectMapLocal = historyArrayList
.get(position);
final String assetTag = historyObjectMapLocal
.get("assetTag");
System.out.println("assetTag : " + assetTag);
}
});
In the above code the listView contents are populated using an ArrayList historyArrayList
and so the items at any column could be accessed using key ("assetTag", "gif" , "action", "actionTime").