how to remove duplicate entries when displayed in listview - android

i have textfile and i manage to display it in listview , my problem is that i want to remove the duplicate entries in column Category, Code,Description when displayed in listview. Thank you for helping! //here is my textfile with duplicate entries
//here is my code in displaying
public ArrayList<HashMap<String, String>> getAllProducts() {
ArrayList<HashMap<String, String>> proList;
proList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM Countsheet";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("Category", cursor.getString(1));
map.put("Code", cursor.getString(2));
map.put("Description", cursor.getString(3));
map.put("Unit", cursor.getString(4));
map.put("Quantity", cursor.getString(5));
proList.add(map);
} while (cursor.moveToNext());
}
return proList;
}

Related

JSON data arraylist and sqlite data arraylist compared to each other after compare data not same after inserted in sqlite database in android

JSON data stored in ArrayList and Sqlite Data Stored Also Another Arraylist. THis Two Database Compare to Each other.in Json Arraylist only New Updated data Stored in Database.JSON and sqlite database data not same after stored in sqlite database. or give me some easy way to JSON data stored in sqlite database.
MainActivity
try
{
JSONArray jsonArray = new JSONArray(s);
if(jsonArray != null){
for (int i = 0; i < 1 ;i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
StudentName = jsonObject.getString("StudentName");
RollNo = jsonObject.getDouble("RollNo");
Standard = jsonObject.getString("Standard");
Division = jsonObject.getString("Division");
FatherNo = jsonObject.getDouble("SMSNo");
Address = jsonObject.getString("Std_Address");
School = jsonObject.getString("SchoolName");
Image = jsonObject.getString("ImagePath");
StdID = jsonObject.getLong("stdid");
DivID = jsonObject.getLong("DivisionID");
String date = jsonObject.getString("DOB");
String replaceDate = date.replace("/Date(", "").replace(")/", "");
Long getDate = Long.valueOf(replaceDate);
DOB = dateFormat.format(getDate);
HashMap<String, String> map = new HashMap<String, String>();
// map.put("Id", cursor.getString(0));
map.put("UserName", StudentName);
map.put("DOB", DOB);
map.put("RollNo", String.valueOf(RollNo));
map.put("STD", Standard);
map.put("DIV", Division);
map.put("MobileNO", String.valueOf(FatherNo));
map.put("Address", Address);
map.put("School", School);
jsonList = new ArrayList<>();
jsonList.add(map);
}
DBHelper db = new DBHelper(getApplicationContext());
ArrayList<HashMap<String,String>> list = db.getAllRegistrationData();
for (int j = 0; j< jsonList.size(); j++ ){
for (int k = 0; k < list.size(); k++){
if (jsonList.get(j).equals(list.get(k))){
Toast.makeText(Registration_Activity.this, "Alredy exist", Toast.LENGTH_SHORT).show();
}else {
long id = db.insertRegistration(StudentName,RollNo,Standard,Division,FatherNo,Address,School,DOB);
Toast.makeText(Registration_Activity.this, "Insert id : " + id, Toast.LENGTH_SHORT).show();
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
DBHelper
public ArrayList<HashMap<String,String>> getAllRegistrationData(){
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("select * from "+TABLE_REGISTRATION,null);
if (cursor.moveToFirst()) {
do {
HashMap<String, String> map = new HashMap<String, String>();
map.put("Id", cursor.getString(0));
map.put("UserName", cursor.getString(1));
map.put("DOB", cursor.getString(2));
map.put("RollNo", cursor.getString(3));
map.put("STD", cursor.getString(4));
map.put("DIV", cursor.getString(5));
map.put("MobileNO", cursor.getString(6));
map.put("Address", cursor.getString(7));
map.put("School", cursor.getString(8));
list.add(map);
} while (cursor.moveToNext());
}
return list;
}

Hashmap into database

I stored hashmap in database but when the items in hashmap are more than one, the last value is shown multiple times... for example in hashmap i have
bag
pen
book
but when I retrieve the values I get
book
book
book
How do I fix this?
DatabaseHandler.java
public void addQuote(Quote q, ArrayList<HashMap<String, String>> put_q_list)
{
myDataBase=this.getWritableDatabase();
try{
ContentValues values=new ContentValues();
values.put("q_customer_name", q.getCustomer_name());
values.put("q_customer_email", q.getCustomer_email());
values.put("q_b_street", q.getBilling_street());
values.put("q_b_city", q.getBilling_city());
values.put("q_b_state", q.getBilling_state());
values.put("q_b_zipcode", q.getBilling_zipcode());
values.put("q_b_country", q.getBilling_country());
values.put("q_s_street", q.getShipping_street());
values.put("q_s_city", q.getShipping_city());
values.put("q_s_state", q.getShipping_state());
values.put("q_s_zipcode", q.getShipping_zipcode());
values.put("q_s_country", q.getShipping_country());
values.put("q_day", q.getDay());
values.put("q_month", q.getMonth());
values.put("q_year", q.getYear());
values.put("q_total", q.getTotal());
values.put("q_discount", q.getTotal_discount());
myDataBase.insert("quote",null,values);
}catch(Exception e)
{
Log.e("bderror",e.toString());
e.printStackTrace();
}
String selectQuery = "SELECT quote_id FROM quote WHERE q_customer_email=?";
Cursor cursor = myDataBase.rawQuery(selectQuery, new String[] {q.getCustomer_email()});
int i=0;
while(cursor.moveToNext())
{
i=cursor.getInt(0);
}
cursor.close();
for (HashMap<String, String> map : put_q_list) {
ContentValues cv = new ContentValues();
cv.put("quote_id", i);
cv.put("q_item_code", map.get("item_code_final"));
cv.put("q_item_desc", map.get("desc_final"));
cv.put("q_item_price", map.get("item_price_final"));
cv.put("q_item_qty", map.get("item_qty_final"));
cv.put("q_item_tax", map.get("item_tax_final"));
cv.put("q_item_discount", map.get("item_discount_final"));
myDataBase.insert("q_item", null, cv);
}
}
Add.java`
HashMap<String, String> map_item_final = new HashMap<String, String>();
map_item_final.put("item_code_final",autoComplete_item.getText().toString());
map_item_final.put("desc_final", item_desc.getText().toString());
map_item_final.put("item_price_final", String.valueOf(item_price.getText()));
map_item_final.put("item_tax_final",String.valueOf(item_tax.getText()));
map_item_final.put("item_discount_final",String.valueOf(item_discount.getText()));
map_item_final.put("item_qty_final", item_qty.getText().toString());
final_items.add(map_item_final);
dataBase.addQuote(q, final_items);
Retrieving....
public ArrayList<HashMap<String, String>> getQuoteItem(String quote_id)
{
ArrayList<HashMap<String, String>> put_list= new ArrayList<HashMap<String, String>>();
myDataBase=this.getWritableDatabase();
Cursor cursor=myDataBase.rawQuery("SELECT * from q_item where quote_id="+Integer.parseInt(quote_id),null);
HashMap<String, String> map = new HashMap<String, String>();
while(cursor.moveToNext())
{
map.put("i_item_code", cursor.getString(1));
map.put("i_item_desc", cursor.getString(2));
map.put("i_item_price", String.valueOf(cursor.getInt(3)));
map.put("i_item_qty", String.valueOf(cursor.getInt(4)));
map.put("i_item_tax", String.valueOf(cursor.getFloat(5)));
map.put("i_item_discount", String.valueOf(cursor.getInt(6)));
put_list.add(map);
}
cursor.close();
return put_list;
}
SQLite database browser
The first items is becoming null!!! Only last item is stored
[img]
https://drive.google.com/file/d/0B8fPYT_K7J81OXl0ejFiZlpFRDQ/edit?usp=sharing
Please HELP!!!!
I think your problem is this bit of code in add quote():
int i=0;
while(cursor.moveToNext()) {
i=cursor.getInt(0);
}
You're storing the value in the cursor into an int, then overwriting it each time before you do anything with it.
create hashmap object inside of while loop
HashMap<String, String> map = null;
while(cursor.moveToNext())
{
map = new HashMap<String, String>();
map.put("i_item_code", cursor.getString(1));
map.put("i_item_desc", cursor.getString(2));
map.put("i_item_price", String.valueOf(cursor.getInt(3)));
map.put("i_item_qty", String.valueOf(cursor.getInt(4)));
map.put("i_item_tax", String.valueOf(cursor.getFloat(5)));
map.put("i_item_discount", String.valueOf(cursor.getInt(6)));
put_list.add(map);
}

Getting the value of the ArrayList<HashMap<String,String>> for display

Hi I just want to know better on how to use this. Since I'm using this kind of method in doing my custom listView now I wanted to apply it since I love the approach on this but I don't know how I should do it. Anyway What I have is a database query where I get all the result from a search query in SQLite Database. Now I'm sure about the content of my query since I already tested it but when I display it the result always return the same output which is the last row of the query. For better understanding here's my code:
public ArrayList<HashMap<String,String>> getList(String search_param){
SQLiteDatabase db = this.getWritableDatabase();
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> hashmap = new HashMap<String,String>();
String query_select = "SELECT column1, column2 FROM tablename WHERE column2 LIKE '%"+ search_param +"%';";
Cursor cursor = db.rawQuery(query_select,null);
if (cursor.moveToFirst()) {
do {
item_list.put("column1", cursor.getString(0));
item_list.put("column2",cursor.getString(1));
list.add(hashmap);
} while (cursor.moveToNext());
}
cursor.close();
return list;
}
now to retrieve the list here's what I tried so far:
ArrayList<HashMap<String,String>> new_list;
DatabaseClass db = new DatabaseClass(getActivity());
new_list = db.getList("");
for(int i=0;i<new_list.size();i++){
HashMap<String,String> content = new HashMap<String, String>();
content = new_list.get(i);
Log.v("The hashmap",content.get("column1").toString());
Log.v("The hashmap",content.get("column2").toString());
}
now the content of my Database should be 1,2,3 for column1 and test1,test2,test3 for column2. but what I get from the result are 3,3,3 and test3,test3,test3
I also tried the method of doing
newlist.get(i).get("column1").toString; but it doesn't even solve the problem.
Any idea on what I should do about this?
Within do while you need to create instance for hashmap,
public ArrayList<HashMap<String,String>> getList(String search_param){
SQLiteDatabase db = this.getWritableDatabase();
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> hashmap;
String query_select = "SELECT column1, column2 FROM tablename WHERE column2 LIKE '%"+ search_param +"%';";
Cursor cursor = db.rawQuery(query_select,null);
if (cursor.moveToFirst()) {
do {
hashMap = new HashMap<String,String>();
hashMap.put("column1", cursor.getString(0));
hashMap.put("column2",cursor.getString(1));
list.add(hashmap);
} while (cursor.moveToNext());
}
cursor.close();
return list;
}
This works like charm with me
public ArrayList<HashMap<String, String>> getList(String search_param){
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
String query_select = "SELECT column1, column2 FROM tablename WHERE column2 LIKE '%"+ search_param +"%';";
Cursor cursor = db.rawQuery(query_select,null);
int colCount = cursor.getColumnCount();
int rowCount = cursor.getCount();
if (cursor.moveToFirst()) {
do {
HashMap<String, String> col = new HashMap<String, String>();
int size =cursor.getColumnCount();
for (int i = 0; i < size; i++) {
col.put(cursor.getColumnName(i), cursor.getString(i));
}
list.add(col);
} while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
return list;
}

Null Pointer Exception print in needs a message error from sqlite

I'm getting a NullPointerException: println needs a message Error when trying to start an activity. I can't see the error.
Here is my database concerning the error
public HashMap<String, String> getsaleInfo(String saleId) {
HashMap<String, String> wordList = new HashMap<String, String>();
SQLiteDatabase database = this.getReadableDatabase();
String selectQuery = "SELECT * FROM SUPERSALE where supersaleId='"+saleId+"'";
Cursor cursor = database.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
//HashMap<String, String> map = new HashMap<String, String>();
wordList.put("saleCust", cursor.getString(1));
wordList.put("saleName", cursor.getString(2));
wordList.put("saleTime", cursor.getString(4));
wordList.put("saleTax", cursor.getString(5));
wordList.put("saleNotes", cursor.getString(6));
//wordList.add(map);
} while (cursor.moveToNext());
}
return wordList;
}
And here is how I retrieve in the activity
saleCust = (TextView) findViewById(R.id.saleCust);
saleNotes = (TextView) findViewById(R.id.saleNotes);
saleName = (TextView) findViewById(R.id.saleName);
saleTime = (TextView) findViewById(R.id.saleTime);
saleTax = (TextView) findViewById(R.id.saleTax);
Intent objIntent = getIntent();
String saleId = objIntent.getStringExtra("supersaleId");
Log.d("Reading: ", "Reading all contacts..");
HashMap<String, String> saleList = controller.getsaleInfo(saleId);
Log.d("saleCust",saleList.get("saleCust"));
Log.d("saleName",saleList.get("saleName"));
Log.d("saleTime",saleList.get("saleTime"));
Log.d("saleTax",saleList.get("saleTax"));
Log.d("saleNotes",saleList.get("saleNotes"));
if(saleList.size()!=0) {
saleCust.setText(saleList.get("saleCust"));
saleName.setText(saleList.get("saleName"));
saleTime.setText(saleList.get("saleTime"));
saleTax.setText(saleList.get("saleTax"));
saleNotes.setText(saleList.get("saleNotes"));
}}
The error is coming from line
Log.d("saleCust",saleList.get("saleCust"));
Please post your logcat next time and your UI activity in question. Since you have textview from your layout, you could be using a Listview, ExpandedListView, etc. If so, android view's require that there is a column _id in your table and is retrieved in your query so that the view can keep in sync with the data adapter's data from your database cursor via the _id column.
Hope this helps.

Android : retrieve all data from database and store in ArrayList<HasMap<String, String>>

I try to retrieve all data from database and put into arraylist of hasmap
but i get only the last record of table.
here is my code :
databaseAirport myDbHelper = new databaseAirport(this);
myDbHelper.open();
Cursor c = myDbHelper.selectAll();
HashMap<String, String> map = new HashMap<String, String>();
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String,String>>();
if(c!=null && c.moveToFirst())
{
do
{
map.put(KEY_ACODE, c.getString(5).toString());
map.put(KEY_FNUM, c.getString(6).toString());
map.put(KEY_STOP_1, c.getString(8).toString());
map.put(KEY_FTIME, c.getString(7).toString());
map.put(KEY_REMARK, c.getString(10).toString());
map.put(KEY_REM_COM, c.getString(11).toString());
dataList.add(map);
}while(c.moveToNext());
}
At the end, my dataList(map) retrieve the last record only...
Can anyone help me please..
Move this line as follow: HashMap<String, String> map = new HashMap<String, String>();
databaseAirport myDbHelper = new databaseAirport(this);
myDbHelper.open();
Cursor c = myDbHelper.selectAll();
HashMap<String, String> map;
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String,String>>();
if(c!=null ){
c.moveToFirst();
while( cur.isAfterLast() == false )
{
map = new HashMap<String, String>(); // <---- moved here
map.put(KEY_ACODE, c.getString(5).toString());
map.put(KEY_FNUM, c.getString(6).toString());
map.put(KEY_STOP_1, c.getString(8).toString());
map.put(KEY_FTIME, c.getString(7).toString());
map.put(KEY_REMARK, c.getString(10).toString());
map.put(KEY_REM_COM, c.getString(11).toString());
dataList.add(map);
c.moveToNext();
}
}
Change your code like this
databaseAirport myDbHelper = new databaseAirport(this);
myDbHelper.open();
Cursor c = myDbHelper.selectAll();
ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String,String>>();
while(c.moveToNext());
{
HashMap<String, String> map = new HashMap<String, String>();
map.put(KEY_ACODE, c.getString(5).toString());
map.put(KEY_FNUM, c.getString(6).toString());
map.put(KEY_STOP_1, c.getString(8).toString());
map.put(KEY_FTIME, c.getString(7).toString());
map.put(KEY_REMARK, c.getString(10).toString());
map.put(KEY_REM_COM, c.getString(11).toString());
dataList.add(map);
}

Categories

Resources