Hi, Friends,
I had a problem in displaying data in a custom list view,
I am attaching data from database(sqlite) to list view dynamically,
for the first time the list is having three rows, when we run the
application again it is having six and next nine etc.
Even i Tried with SQL commands like
Delete from Table
and also
DROP TABLE IF EXISTS even then also it is displaying duplicate data
MY Code:
public class DisplayList extends ListActivity{
private static String[] FROM = {AGE, IN_KGS, IN_LBS, };
public static ArrayList<String> results = new ArrayList<String>();
public static final ArrayList<HashMap<String,String>> list
= new ArrayList<HashMap<String,String>>();
SQLiteDatabase db=null;
Cursor cursor=null;
@Override
public void onCreate(Bundle savedInstanceState)
{
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
addData();
cursor= getDataToCursor();
getDataArrayList(cursor);
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.custom_row_view,
new String[] {"age","wkgs","wlbs"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
setDataToList();
setListAdapter(adapter);
}
catch(Exception e){
Log.e("createerr",e.toString());
Log.e(getClass().getSimpleName(), "Could not
create or Open the
database");
} finally {
if (db != null)
db.execSQL("DELETE FROM " + TABLE_NAME);
db.close();
}
}
// other methods of this class
public void addData() {
db=(new DataHelper(this)).getWritableDatabase();
ContentValues values = new ContentValues();
values.put(AGE,"1-2 Yrs" );
values.put(IN_KGS, "2-6 Kgs");
values.put(IN_LBS, "5-15 lbs");
db.insertOrThrow(TABLE_NAME, null, values);
db.execSQL("DELETE FROM "+TABLE_NAME);
// i tried the above line of code remove all the records after records
and trying to add all records //again
values.clear();
values.put(AGE,"1-2 Yrs" );
values.put(IN_KGS, "2-6 Kgs");
values.put(IN_LBS, "5-15 lbs");
db.insertOrThrow(TABLE_NAME, null, values);
values.clear();
values.put(AGE,"3-4 Yrs" );
values.put(IN_KGS, "6-10 Kgs");
values.put(IN_LBS, "15-25 lbs");
db.insertOrThrow(TABLE_NAME, null, values);
values.clear();
values.put(AGE,"5-6 Yrs" );
values.put(IN_KGS, "10-16 Kgs");
values.put(IN_LBS, "25-35 lbs");
db.insertOrThrow(TABLE_NAME, null, values);
}
public Cursor getDataToCursor()
{
db=(new DataHelper(this)).getWritableDatabase();
Cursor cursor = db.query(TABLE_NAME, FROM, null, null, null,
null,
null);
startManagingCursor(cursor);
return cursor;
}
public void getDataArrayList(Cursor c)
{
if (c != null ) {
if (c.moveToFirst()) {
do {
String Age =
c.getString(0);
String Wkgs =
c.getString(1);
String Wlbs =
c.getString(2);
results.add(Age
+"@"+Wkgs+"@"+Wlbs+"@");
}while (c.moveToNext());
}
}
}
public void setDataToList()
{
for(int i=0; i<results.size();i++)
{
HashMap<String,String> temp = new
HashMap<String,String>();
StringTokenizer tempStringTokenizer = new
StringTokenizer(results.get(i),"@");
if (tempStringTokenizer.hasMoreTokens())
{
temp.put("age",tempStringTokenizer.nextElement().toString());
}
if (tempStringTokenizer.hasMoreTokens())
{
temp.put("wkgs",tempStringTokenizer.nextElement().toString());
}
if (tempStringTokenizer.hasMoreTokens())
{
temp.put("wlbs",tempStringTokenizer.nextElement().toString());
}
list.add(temp);
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en