简体   繁体   English

如何将完整的ListView数据(我从Internet提取)(位图和字符串)保存到内部存储器

[英]How to save full ListView Data (which i fetch from Internet) contained (bitmap and strings) to internal memory

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. 当Internet不可用时,如何将包含的完整ListView数据(我从Internet提取)(位图和字符串)保存到内部存储器中以备将来使用。
This is My Main Code: DisplayList.java 这是我的主要代码: 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. 在上面的程序中,我正在doInBackground-Method中从服务器中获取flowerList (其中包含位图和字符串),并将其传递到FlowerAdapter中以设置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. 您能给我一种将所有位图图像和字符串(包含名称和描述)存储到内部存储器的方法吗?如果您以本示例解决了我的大问题,那么我将非常感激您。

You just create a databse for local storage where just create a table with column name (id,flowerName,imageBitmap,instruction) and what ever you want to add after creating table just insert your data in localDB and when ever you need just fetch from there. 您只需为本地存储创建一个数据库,其中仅创建一个具有列名(id,flowerName,imageBitmap,指令)的表,创建表后您想要添加的内容只需将数据插入localDB中,并且只要您需要从那里获取。

for creating database just make a class which extends SQLiteOpenHelper and in that implement onCreate and onUpgrade method 创建数据库只需创建一个扩展SQLiteOpenHelper的类,并在该类中实现onCreate和onUpgrade方法

in oncreate you just execute your table creation query. 在oncreate中,您只需执行表创建查询。

Thanks! 谢谢!

http://www.vogella.com/tutorials/AndroidSQLite/article.html http://www.vogella.com/tutorials/AndroidSQLite/article.html

Please read the above tutorial for working with sqlite .The main issue of saving data would be resolved when there is no internet connection.Other that that use image library like Picasso or Fresco to save image in your local memmory. 请阅读以上有关使用sqlite的教程。当没有互联网连接时,保存数据的主要问题将得到解决;其他使用Picasso或Fresco等图像库将图像保存在本地内存中的其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM