In my scenario i am having 3 records in sqlite which were fetched from websercive that contains id,name,description and image now i want to display the first record and second one after flipping the first record.and 3 one after second record flips..
What i have done so far is
while (cursor.moveToNext())
{
String temp_bookid = cursor.getString(0);
Log.i("temp_bookid",temp_bookid);
String temp_desc=cursor.getString(1);
byte[] temp_image1 = cursor.getBlob(2);
byte[] temp_image2 = cursor.getBlob(3);
String temp_id=cursor.getString(4);
String temp_name = cursor.getString(5);
String temp_bname=cursor.getString(6);
mapId.add(temp_id);
mapBname.add(temp_bname);
mapBookId.add(temp_bookid);
mapName.add(temp_name);
mapDesc.add(temp_desc);
map2.add(temp_image2);
map1.add(temp_image1);
Log.i("temp_id of the page in sqlite", temp_id);
TextView txtDesc = (TextView) findViewById(R.id.tDescription);
text = (TextView) findViewById(R.id.tTitle);
text.setText(temp_name);
txtDesc.setText(temp_desc);
ImageView img = (ImageView)findViewById(R.id.smallImage);
img.setImageBitmap(BitmapFactory.decodeByteArray(temp_image1, 0,temp_image1.length));
txtName1 = (TextView) findViewById(R.id.tvName);
txtName1.setText(temp_bname);
break;
}
mContext = this;
vf = (ViewFlipper) this.findViewById(R.id.vfShow);
vf.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
Log.i("ew","ae");
return true;
}
});
I am able to display first record data when i set break; statement otherwise it is displaying last record data..How can proceed for this approach?
First fetch all the records and store in List<YourDataClass> myList object from onCreate method. As I have just giving you the examples
Now after fetching the data and stored in your list object you need to implement flipper change event so when flipper was change get the current position and using this position get the data as object from your list object. As you move right/left it will giving you the current position and you just need to use this position to fetch the records from list object
For example
Your POJO class will store the data MyData.java
public class MyData{
public long id = -1;
public String name = "";
public String description = "";
public String imagePath = ""; /// I don't know you want exactly, you need to implement image as per your requirement.
}
In your activity
private List<MyData> myList = new ArrayList<MyData>();
oncreate(){
fetchData(); // you need to implement this in AsyncTask so UI was not block just implement AsyncTask and call this method in doInBackground().
// after this you can implment View flipper and add view and set the data by fetch the list object using current view flipper position.
}
private void fetchData(){
while (cursor.moveToNext()){
MyData myData = new MyData();
myData.id = cursor.getLong(0);
myData.desc=cursor.getString(1);
myData.name = cursor.getString(5);
list.add(myData);
myData = null;
}
}
Related
I am trying to store and retrieve image data in Sqlite Db.
To do so I firstly stored in local device memory an example pic (path: storage/emulated/0/Download/).
public class MainActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks<Cursor> {
private final String SAMPLE_IMAGE_PATH = "/storage/emulated/0/Download/image.jpg";
Then I set up an insert method to feed the db with these example data:
private void insertProduct() {
// Create a ContentValues object where column names are the keys,
// and sample attributes are the values.
ContentValues values = new ContentValues();
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_NAME, sampleName);
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_QTY, sampleQty);
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_PRICE, SamplePrice);
values.put(InventoryContract.ProductEntry.COLUMN_EMAIL, sampleMail);
values.put(InventoryContract.ProductEntry.COLUMN_PHONE, samplePhone);
values.put(InventoryContract.ProductEntry.COLUMN_PRODUCT_PIC, SAMPLE_IMAGE_PATH);
//insert a new row
Uri newUri = getContentResolver().insert(InventoryContract.ProductEntry.CONTENT_URI,values);
}
and I define the onCreateLoader method as follows:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// Define a projection that specifies the columns from the table we care about.
String[] projection = {
InventoryContract.ProductEntry._ID,
InventoryContract.ProductEntry.COLUMN_PRODUCT_PIC,
InventoryContract.ProductEntry.COLUMN_PRODUCT_PRICE,
InventoryContract.ProductEntry.COLUMN_PRODUCT_QTY,
InventoryContract.ProductEntry.COLUMN_PRODUCT_NAME};
// This loader will execute the ContentProvider's query method on a background thread
return new CursorLoader(this,
InventoryContract.ProductEntry.CONTENT_URI,
projection,
null,
null,
null);
}
In the CursorAdapter class I updated the listView adding the data from db in bindView() method:
public void bindView(View view, Context context, Cursor cursor) {
// Find individual views that we want to modify in the list item layout
TextView nameTextView = (TextView) view.findViewById(R.id.prod_name);
TextView priceTextView = (TextView) view.findViewById(R.id.prod_price);
TextView qtyTextView = (TextView) view.findViewById(R.id.prod_qty);
ImageView prodImageView = (ImageView) view.findViewById(R.id.prod_img);
// Find the columns of attributes that we're interested in
int nameColumnIndex = cursor.getColumnIndex(InventoryContract.ProductEntry.COLUMN_PRODUCT_NAME);
int priceColumnIndex = cursor.getColumnIndex(InventoryContract.ProductEntry.COLUMN_PRODUCT_PRICE);
int qtyColumnIndex = cursor.getColumnIndex(InventoryContract.ProductEntry.COLUMN_PRODUCT_QTY);
int picColumnIndex = cursor.getColumnIndex(InventoryContract.ProductEntry.COLUMN_PRODUCT_PIC);
// Read the attributes from the Cursor for the current product
String prodName = cursor.getString(nameColumnIndex);
Double prodPrice = cursor.getDouble(priceColumnIndex);
int prodQty = cursor.getInt(qtyColumnIndex);
byte [] prodImg = cursor.getBlob(picColumnIndex);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[1024 * 32];
Bitmap bmp = BitmapFactory.decodeByteArray(prodImg, 0, prodImg.length, options);
//Update Views
nameTextView.setText(String.valueOf(prodName));
priceTextView.setText(prodPrice.toString());
qtyTextView.setText(String.valueOf(prodQty));
prodImageView.setImageBitmap(bmp);
}
}
When I try execute this code everything goes ok, but I see a blank image instead of both the selected pic and placer pic.
So I think that there is some problem with inserting data into db.
I am trying to store and retrieve image data in Sqlite Db
I do not recommend this. Store the images in files. Store data in the rows that identifies the files.
Then I set up an insert method to feed the db with these example data
You are storing a string in COLUMN_PRODUCT_PIC. You are not storing a byte[]. This is good, relative to my recommendation. This is bad relative to your data-retrieval code, where you are attempting to retrieve a byte[].
I am trying to use some data from my database, but function only use data from first entry. What should I add to go through all the entries from database?
public void drawdayplan(){
DatabaseConnector dbConnector = new DatabaseConnector(Clock.this);
Intent a = getIntent();
String d_m_y = a.getStringExtra("d_m_y");
dbConnector.open();
Cursor result = dbConnector.gethour(d_m_y);
if(result.moveToFirst()){
int hourfromIndex = result.getColumnIndex("inthourfrom");
int hourtoIndex = result.getColumnIndex("inthourto");
int colourIndex = result.getColumnIndex("colour");
hourfrom = result.getInt(hourfromIndex);
hourto = result.getInt(hourtoIndex);
colour = result.getString(colourIndex);
// here are some steps with painting which are using this variables
}
}
result.close();
dbConnector.close();
}
But function only use data from first entry.
You never ask for more than the first row. Call moveToNext() in a loop like this:
while(result.moveToNext()){
// Read each row
}
In my scenario i am having 3 records in sqlite which were fetched from websercive that contains id,name,description and image now i want to display the first record and second one after flipping the first record.and 3 one after second record flips..
What i have done so far is
while (cursor.moveToNext())
{
String temp_bookid = cursor.getString(0);
Log.i("temp_bookid",temp_bookid);
String temp_desc=cursor.getString(1);
byte[] temp_image1 = cursor.getBlob(2);
byte[] temp_image2 = cursor.getBlob(3);
String temp_id=cursor.getString(4);
String temp_name = cursor.getString(5);
String temp_bname=cursor.getString(6);
mapId.add(temp_id);
mapBname.add(temp_bname);
mapBookId.add(temp_bookid);
mapName.add(temp_name);
mapDesc.add(temp_desc);
map2.add(temp_image2);
map1.add(temp_image1);
Log.i("temp_id of the page in sqlite", temp_id);
TextView txtDesc = (TextView) findViewById(R.id.tDescription);
text = (TextView) findViewById(R.id.tTitle);
text.setText(temp_name);
txtDesc.setText(temp_desc);
ImageView img = (ImageView)findViewById(R.id.smallImage);
img.setImageBitmap(BitmapFactory.decodeByteArray(temp_image1, 0,temp_image1.length));
txtName1 = (TextView) findViewById(R.id.tvName);
txtName1.setText(temp_bname);
break;
}
mContext = this;
vf = (ViewFlipper) this.findViewById(R.id.vfShow);
vf.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
Log.i("ew","ae");
return true;
}
});
I am able to display first record data when i set break; statement otherwise it is displaying last record data..How can proceed for this approach?
First fetch all the records and store in List<YourDataClass> myList object from onCreate method. As I have just giving you the examples
Now after fetching the data and stored in your list object you need to implement flipper change event so when flipper was change get the current position and using this position get the data as object from your list object. As you move right/left it will giving you the current position and you just need to use this position to fetch the records from list object
For example
Your POJO class will store the data MyData.java
public class MyData{
public long id = -1;
public String name = "";
public String description = "";
public String imagePath = ""; /// I don't know you want exactly, you need to implement image as per your requirement.
}
In your activity
private List<MyData> myList = new ArrayList<MyData>();
oncreate(){
fetchData(); // you need to implement this in AsyncTask so UI was not block just implement AsyncTask and call this method in doInBackground().
// after this you can implment View flipper and add view and set the data by fetch the list object using current view flipper position.
}
private void fetchData(){
while (cursor.moveToNext()){
MyData myData = new MyData();
myData.id = cursor.getLong(0);
myData.desc=cursor.getString(1);
myData.name = cursor.getString(5);
list.add(myData);
myData = null;
}
}
In my scenario i am having 3 records in sqlite which were fetched from websercive that contains id,name,description and image now i want to display the first record and second one after flipping the first record.and 3 one after second record flips..
What i have done so far is
while (cursor.moveToNext())
{
String temp_bookid = cursor.getString(0);
Log.i("temp_bookid",temp_bookid);
String temp_desc=cursor.getString(1);
byte[] temp_image1 = cursor.getBlob(2);
byte[] temp_image2 = cursor.getBlob(3);
String temp_id=cursor.getString(4);
String temp_name = cursor.getString(5);
String temp_bname=cursor.getString(6);
mapId.add(temp_id);
mapBname.add(temp_bname);
mapBookId.add(temp_bookid);
mapName.add(temp_name);
mapDesc.add(temp_desc);
map2.add(temp_image2);
map1.add(temp_image1);
Log.i("temp_id of the page in sqlite", temp_id);
TextView txtDesc = (TextView) findViewById(R.id.tDescription);
text = (TextView) findViewById(R.id.tTitle);
text.setText(temp_name);
txtDesc.setText(temp_desc);
ImageView img = (ImageView)findViewById(R.id.smallImage);
img.setImageBitmap(BitmapFactory.decodeByteArray(temp_image1, 0,temp_image1.length));
txtName1 = (TextView) findViewById(R.id.tvName);
txtName1.setText(temp_bname);
break;
}
mContext = this;
vf = (ViewFlipper) this.findViewById(R.id.vfShow);
vf.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(final View view, final MotionEvent event) {
detector.onTouchEvent(event);
Log.i("ew","ae");
return true;
}
});
I am able to display first record data when i set break; statement otherwise it is displaying last record data..How can proceed for this approach?
First fetch all the records and store in List<YourDataClass> myList object from onCreate method. As I have just giving you the examples
Now after fetching the data and stored in your list object you need to implement flipper change event so when flipper was change get the current position and using this position get the data as object from your list object. As you move right/left it will giving you the current position and you just need to use this position to fetch the records from list object
For example
Your POJO class will store the data MyData.java
public class MyData{
public long id = -1;
public String name = "";
public String description = "";
public String imagePath = ""; /// I don't know you want exactly, you need to implement image as per your requirement.
}
In your activity
private List<MyData> myList = new ArrayList<MyData>();
oncreate(){
fetchData(); // you need to implement this in AsyncTask so UI was not block just implement AsyncTask and call this method in doInBackground().
// after this you can implment View flipper and add view and set the data by fetch the list object using current view flipper position.
}
private void fetchData(){
while (cursor.moveToNext()){
MyData myData = new MyData();
myData.id = cursor.getLong(0);
myData.desc=cursor.getString(1);
myData.name = cursor.getString(5);
list.add(myData);
myData = null;
}
}
Im Developing an Application where the ListView with Images will be displayed the text data and Images are downloaded from the services. I want to save the data in SQLite also I want to save the thumbnail Images my code is
public class MainActivity extends Activity {
// TAG for LogCat
public static final String TAG = "SQLiteDebug";
// list view control
private ListView mListUserInfo;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
// load list view control
mListUserInfo = (ListView)findViewById(R.id.listUsers);
// create data creator
UserInfoCreator creator = new UserInfoCreator(this);
// open connection
creator.open();
// insert data
creator.insert();
// add data to list view through adapter
mListUserInfo.setAdapter(new UserInfoAdapter(this, creator.queryAll()));//,thumbURLStrings));
// close connection
creator.close();
}
}
Data inserted in
public class UserInfoCreator {
static String [] thumbURLStrings = null;
// db adapter
private UserDbAdapter mDbAdapter;
/*
* constructor
*/
public UserInfoCreator(Context c) {
mDbAdapter = new UserDbAdapter(c);
}
/*
* open DBAdapter connection
*/
public void open() {
mDbAdapter.open();
}
/*
* insert random data
*/
public void insert() {
mDbAdapter.insertUser("Toteninsel 100. Fall", 1, "André Marx");
mDbAdapter.insertUser("Geisterbucht 150. Fall", 2, "Astrid Vollenbruch");
mDbAdapter.insertUser("Feuermond 125. Fall", 3, "André Marx");
mDbAdapter.insertUser("Top Secret Edition", 4, "Peter Lerangis");
mDbAdapter.insertUser("Der dreiTag", 5, "Hendrik Buchna");
mDbAdapter.insertUser("Die blutenden Bilder", 6, "Kari Erlhoff");
String xml = XMLfunction.getXML("http://ddfzentrale.europa-websites.de/Zentrale_php/XML/Buecher.xml");
Document doc = XMLfunction.XMLfromString(xml);
NodeList nodes = doc.getElementsByTagName("book");
String [] titleStrings = new String[nodes.getLength()];
String [] authorStrings = new String[nodes.getLength()];
Log.d("nodeLength","len: "+nodes.getLength());
for(int i=0;i < nodes.getLength();i++){
Element eleBook = (Element)nodes.item(i);
Log.d("Node_TagName", eleBook.getTagName());
NodeList titleNode = eleBook.getElementsByTagName("Title");
Element TitleEle = (Element) titleNode.item(0);
Log.i("Title", "Title - "+TitleEle.getFirstChild().getNodeValue());
titleStrings[i]= TitleEle.getFirstChild().getNodeValue();
NodeList AuthorFName1Node = eleBook.getElementsByTagName("AutorFName1");
Element AuthorFName1Ele = (Element) AuthorFName1Node.item(0);
Log.i("AuthorFName1","AuthorFName1 - "+AuthorFName1Ele.getFirstChild().getNodeValue());
NodeList AuthorLName1Node = eleBook.getElementsByTagName("AutorLName1");
Element AuthorLName1Ele = (Element) AuthorLName1Node.item(0);
Log.i("AuthorLName1","AuthorLName1 - "+AuthorLName1Ele.getFirstChild().getNodeValue());
authorStrings[i]=AuthorFName1Ele.getFirstChild().getNodeValue()+" "+AuthorLName1Ele.getFirstChild().getNodeValue();
mDbAdapter.insertUser(titleStrings[i], i, authorStrings[i]);
}
}
/*
* query all user info from db
*/
public List<UserInfo> queryAll() {
return mDbAdapter.fetchAllUsers();
}
/*
* close connection
*/
public void close() {
mDbAdapter.close();
}
}
Im parsing and inserting data in same class But with the Text I want to display and show thumbnail Image to display thumbnail with the images I used tutorial https://github.com/thest1/LazyList Please anyone have an Idea about how to store the Images in SQLite Please help me
Check this Code for Inserting images into Database downloading from Server :
Download Image from Server store it as BLOB and Retrieve it back.
It is not advisable to store images in SQLite database. But still it is possible to to so. You can convert the image to a byte array and store it as a BLOB in database. I hope it helps!