保存的arrayList <hashmap <string,string>,在没有Internet的情况下显示它们

时间:2015-09-24 10:21:31

标签: android sqlite listview arraylist

我有arrayList&gt;在我的app.I有这个数组来自互联网,通过解析json数据。我想保存这个数组,然后我的应用程序可以在没有Internet的情况下工作。我该怎么办?我知道如何在SQLite中存储数据,但查询的响应是游标,我知道我可以创建使用cursor的自定义适配器。但是我可以找到更简单的方法吗? MainActivity:

public class MainActivity extends ListActivity {
private Context context;
SqlHelper dbHelper;
    Intent intent;
    private static String url = "https://fierce-citadel-4259.herokuapp.com/hamsters";
    private static final String TITLE = "title";
    private static final String DESCRIPTION = "description";
    private static final String IMAGE = "image";
    ArrayList<HashMap<String,String>> jsonlist = new ArrayList<HashMap<String, String>>();
   ListView lv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new ProgressTask(MainActivity.this).execute();
lv=(ListView) findViewById(android.R.id.list);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String title1 = jsonlist.get(position).get("title");
                String description1 = jsonlist.get(position).get("description");
                String url1 = jsonlist.get(position).get("image");
                intent = new Intent(MainActivity.this, DetailInfo.class);
                intent.putExtra("title", title1);
                intent.putExtra("description", description1);
                intent.putExtra("url", url1);
                startActivity(intent);
                dbHelper = new SqlHelper(MainActivity.this);
                try {
                    dbHelper.open();
                } catch (SQLException e) {
                    e.printStackTrace();
        }
    }
});



    }



    @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_main, 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.reload) {
         new ProgressTask(MainActivity.this).execute();
        }
else if(id == R.id.menu_item_share){
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT, "Put whatever you want");
            startActivity(Intent.createChooser(intent,"Share via"));
        }
        return super.onOptionsItemSelected(item);
    }

    private class ProgressTask extends AsyncTask<String,Void,Boolean> {
        private ProgressDialog dialog;
        private ListActivity activity;
        private Context context;
        public ProgressTask(MainActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }

        protected void onPreExecute(){
           this.dialog.setMessage("Progress start");
           this.dialog.show();
        }
        protected void onPostExecute(final Boolean success){
            try{
       if((this.dialog != null)&& this.dialog.isShowing()){
this.dialog.dismiss();
            }
            CustomListAdapter adapter =  new CustomListAdapter(MainActivity.this,jsonlist, R.layout.list_item,new String[]{TITLE,DESCRIPTION},new int[]{R.id.title,R.id.description});
lv.setAdapter(adapter);
//setListAdapter(adapter);


        }catch (final IllegalArgumentException e){e.printStackTrace();}
        }
        protected Boolean doInBackground(String... args) {
           JSONParser jParser = new JSONParser();
            JSONArray json = jParser.getJSONFromUrl(url);
            for(int i =0;i<json.length();i++) {
                try {
                    JSONObject c = json.getJSONObject(i);
                    String vtitle = c.getString(TITLE);
                    String vdescription = c.getString(DESCRIPTION);
                    String vimage = c.getString(IMAGE);
                  /*  dbHelper.createEntry(vtitle,vdescription,vimage);
                    dbHelper.close();*/
                    HashMap<String, String> map = new HashMap<>();
                    map.put(TITLE, vtitle);
                    map.put(DESCRIPTION, vdescription);
map.put(IMAGE, vimage);
                    jsonlist.add(map);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }
    }
/*private void displaysavedlv(){
    Cursor cursor = dbHelper.fetchAllCountries();
CustomCursorAdapter adapter1 = new CustomCursorAdapter(MainActivity.this,cursor);
lv.setAdapter(adapter1);


}*/
  /*  private boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            // There are no active networks.
            return false;
        } else
            return true;
    }*/
}

2 个答案:

答案 0 :(得分:0)

是的,你可以在没有sqlite的情况下做到这一点。 TinyDB会为您提供帮助。

结账时间:https://github.com/kcochibili/TinyDB--Android-Shared-Preferences-Turbo

答案 1 :(得分:0)

您可以在sqlite中创建一个方法,该方法返回带有hashmap的arrayList:

public ArrayList<HashMap<String, String>> getAllData()
        {
            ArrayList<HashMap<String, String>> array_list = new ArrayList<HashMap<String, String>>();

            //hp = new HashMap();
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor res =  db.rawQuery( "select * from tablename", null );
            res.moveToFirst();

            while(res.isAfterLast() == false){

                hashmap = new HashMap<String, String>();
                hashmap.put("columnname", res.getString(res.getColumnIndex(columnindex)));
                hashmap.put("columnname", res.getString(res.getColumnIndex(columnindex)));
                hashmap.put("columnname", res.getString(res.getColumnIndex(columnindex)));
                hashmap.put("columnname", res.getString(res.getColumnIndex(columnindex)));

                array_list.add(hashmap);
                res.moveToNext();
            }
            return array_list;
        }

干杯!