adding static data to existing listView where data is populated from sqlite

Multi tool use
adding static data to existing listView where data is populated from sqlite
I have the following codes to add data from sqlite to the listView. (The code works) However, I want to have static data on the listView and also have the function on adding from sqlite. Meaning that, I want to do a listView with data "A", "B","C" as default. So when user clicks on the "add" function user is able to add the data into sqlite and display into the same listView where the "A","B","C" data is.
Please help.
@nightmaregiba list.add("Singapore","4.772",R.mipmap.newsgd); this is the data i want to add, but i have a error, cannot resolve method 'add(java.lang.String, java.lang.String,int) please help thank you
– awa
Feb 5 at 2:33
you mean that the first 3 rows is different in data type from other?
– Liar
Feb 5 at 3:12
is of the same data type
– awa
Feb 5 at 4:31
4 Answers
4
you used imageViewToByte()
for change ImageView to byte, but R.mipmap.newsgd is not ImageView, it resource
imageViewToByte()
oh! then how do i do it? please help. thank you
– awa
Feb 5 at 2:54
All you do is :-
//get all data
Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM FOOD");
list.clear();
while (cursor.moveToNext())
{
int id = cursor.getInt(0);
String name = cursor.getString(1);
String price = cursor.getString(2);
byte image = cursor.getBlob(3);
list.add(new Food(id,name,price,image));
}
// Adding your three items for the list
list.add(new food(your_id_for_A,your_name_for_A,your_price_for_A,your_image_for_A));
list.add(new food(your_id_for_B,your_name_for_B,your_price_for_B,your_image_for_B));
list.add(new food(your_id_for_C,your_name_for_C,your_price_for_C,your_image_for_C));
adapter.notifyDataSetChanged();
Noting that you may have to be a little careful with what id you provide and how you subsequently handle that id as you may have to distinguish between a row from the database as opposed to an added row.
sorry, when i add : "list.add(new Food(50,"Singapore","4.772",R.mipmap.newsgd));" it still have error
– awa
Feb 5 at 4:29
The error still falls on the data type. Please suggest how can i do it. thank you
– awa
Feb 5 at 4:41
@awa that's a completely different question. I believe
list.add(new food(-10,"Singapore","4.772",new byte{0});
would work though.– MikeT
Feb 5 at 6:00
list.add(new food(-10,"Singapore","4.772",new byte{0});
Just add A, B, C after listView.setAdapter(adapter); -> list.add(A), list.add(B), list.add(C)
Ex:
list.add(new Food(someId, "Singapore","4.772",R.mipmap.newsgd, ...));
Hope this help!
step 1
declare one empty arraylist then add static data like in your case A,B,C and fill adapter with this data.
ArrayList<String> arrayList=new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
MyAdapter myAdapter=new MyAdapter(this);
listview.setAdapter(myAdapter);
myAdapter.addItem(arrayList);
in Adapter add this function
public void addItem(ArrayList<String> arraylist){
this.arraylist = arraylist;
notifyDataSetChanged();
}
step 2
in your add item function do 2 things
1 add those data in your database.
2 add those data into arraylist that you have created in step 1, and add it to adapter like below code.
databaseHelper.addData("D");
arrayList.add("D");
myAdapter.addItem(arrayList);
step 3
check after this when you come second time in screen check whether database has data init if yes then modify step one like below
ArrayList<String> arrayList=new ArrayList<>();
arrayList.add("A");
arrayList.add("B");
arrayList.add("C");
MyAdapter myAdapter=new MyAdapter(this);
listview.setAdapter(myAdapter);
if(databaseHelper.getDataList()!=null &databaseHelper.getDataList().size()>0{
arrayList.addAll(databaseHelper.getDataList());
myAdapter.addItem(arrayList);
}else{
myAdapter.addItem(arrayList);
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You want to add default value for your listview? Just add A, B, C after listView.setAdapter(adapter); -> list.add(A), list.add(B), list.add(C)
– Liar
Feb 5 at 2:24