How to save full ListView Data (which i fetch from Internet) contained (bitmap and strings) to internal memory for Future Use when Internet is not Available.
This is My Main Code:DisplayList.java
public class DisplayList extends ListActivity {
List<Flower> flowerList;
ProgressBar progressBar;
List<GetData> task;
public static final String PHOTO_BASE_URL="http://services.hanselandpetal.com/photos/";
private static final int REQUEST_CODE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_list);
// textView=(TextView)findViewById(R.id.data);
// textView.setMovementMethod(new ScrollingMovementMethod());
progressBar=(ProgressBar)findViewById(R.id.progressBar);
progressBar.setVisibility(View.INVISIBLE);
task=new ArrayList<>();
if(isOnline())
{
requestData("http://services.hanselandpetal.com/feeds/flowers.json");
}
else
{
Toast.makeText(DisplayList.this, "Network is not Available", Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_display_list, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
// if (id == R.id.task1)
// {
//
// }
return super.onOptionsItemSelected(item);
}
private void requestData(String url) {
GetData getData=new GetData();
getData.execute(url);
}
public void update()
{
//Use FlowerAdapter to display data
FlowerAdapter adapter = new FlowerAdapter(this, R.layout.item_flower, flowerList);
setListAdapter(adapter);
}
protected boolean isOnline()
{
ConnectivityManager connectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
if(networkInfo!=null && networkInfo.isConnectedOrConnecting())
{
return true;
}
else
{
return false;
}
}
private class GetData extends AsyncTask<String,String,List<Flower>>
{
@Override
protected void onPreExecute()
{
// update("Task Started");
if(task.size()==0) {
progressBar.setVisibility(View.VISIBLE);
}
task.add(this);
}
@Override
protected List<Flower> doInBackground(String... params) {
String content="Internet is to slow";
content=HttpManager1.getDataByHttpUrlConnection(params[0]);
flowerList= FlowerJSONParser.parseFeed(content);
// for(Flower flower:flowerList)
// {
// try
// {
// String imageUrl=PHOTO_BASE_URL+flower.getPhoto();
// InputStream inputStream=(InputStream)new URL(imageUrl).getContent();
// Bitmap bitmap= BitmapFactory.decodeStream(inputStream);
// flower.setBitmap(bitmap);
// inputStream.close();
// }
// catch (Exception e)
// {
// e.printStackTrace();
// }
//
// }
return flowerList;
}
@Override
public void onPostExecute(List<Flower> result)
{
update();
task.remove(this);
if(task.size()==0) {
progressBar.setVisibility(View.INVISIBLE);
}
}
@Override
protected void onProgressUpdate(String... values) {
//update(values[0]);
}
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Flower flower=flowerList.get(position);
Intent intent=new Intent(this,DetailActivity.class);
intent.putExtra("flowerName",flower.getName());
intent.putExtra("imageBitmap",flower.getBitmap());
intent.putExtra("instruction",flower.getInstructions());
startActivityForResult(intent, REQUEST_CODE);
}
}
</pre>
In the above Program I am fetching flowerList (which contained bitmaps and Strings) from server in doInBackground-Method and pass it into FlowerAdapter to set ListView Items. Can you please give me a way to stored all bitmap images and Strings(contained names and description) to Internal Memory.If you take this example and solve my big problem then i will be very appreciable to you.