带重复记录的数据库

时间:2015-10-10 08:06:01

标签: android sqlite

我的app中有sqlite数据库。当我有Internet连接时,数据被保存,当我没有Internet连接时,我从数据库加载数据并显示在ListView.But中这个ListView我有很多记录,我想想当我有互联网时我创建了多少次记录,我是如何启动应用程序的。但是现在我怎样才能删除所有记录,创建新记录,并且每次启动应用程序时都不创建相同的记录? MainList:

SIGBREAK

SqlHelper类:

public class MainList extends ListFragment{
    SqlHelper dbHelper;
    ListView mainList;
    ProgressBar progBar;
    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>> jsonlist1 = new ArrayList<HashMap<String, String>>();
    ArrayList<HashMap<String,String>> bdList = new ArrayList<HashMap<String, String>>();
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.list_fragme, null);
        mainList = (ListView)v.findViewById(android.R.id.list);
    progBar = (ProgressBar) v.findViewById(R.id.progressBar);
        return v;
    }

    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        if (isNetworkConnected()==true) {
            new ProgressTask().execute();
        }
        else{
            displaysavedlv();
        }
    }

    @Override
    public void onListItemClick(ListView l, View view, int position, long id) {
        super.onListItemClick(l, view, position, id);
        String title = jsonlist1.get(position).get("title");
        String description= jsonlist1.get(position).get("description");
        String image = jsonlist1.get(position).get("image");


        MyDetailFragment detailFragment = new MyDetailFragment();
        Bundle bundle = new Bundle();
        if(isNetworkConnected()==true) {
            bundle.putString("title", title);
            bundle.putString("description", description);
            bundle.putString("image", image);
        }
        else
        {
            String dbtitle= bdList.get(position).get("title");
            String dbdescription = bdList.get(position).get("description");
            String dvimage = bdList.get(position).get("image");
            bundle.putString("title",dbtitle);
            bundle.putString("description",dbdescription);
            bundle.putString("image",dvimage);
        }
        detailFragment.setArguments(bundle);
        FragmentTransaction fragmentTransaction = getActivity().getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.FragmentCont,detailFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    private class ProgressTask extends AsyncTask<String,Void,Boolean> {
        private ProgressDialog dialog;
        private Activity activity;
        private MainActivity context;
        private String[] params;

        public ProgressTask(MainActivity activity) throws SQLException {
            this.activity = getActivity();
            context = activity;
            dialog = new ProgressDialog(getActivity().getApplicationContext());

        }

        public ProgressTask()  {
            dialog = new ProgressDialog(getActivity().getApplicationContext());
            try {
                dbHelper = new SqlHelper(getActivity().getApplicationContext());
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        @Override
        protected Boolean doInBackground(String... params) {
            this.params = params;
            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.open();
                    dbHelper.createEntry(vtitle, vimage, vdescription);
                    dbHelper.close();

                    HashMap<String, String> map = new HashMap<>();
                    map.put(TITLE, vtitle);
                    map.put(DESCRIPTION, vdescription);
                    map.put(IMAGE, vimage);

                    jsonlist1.add(map);

                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }


        protected void onPreExecute(){
            progBar.setVisibility(View.VISIBLE);
        }
        protected void onPostExecute(final Boolean success){

            if (isNetworkConnected()==true) {
                progBar.setVisibility(View.GONE);
                CustomListAdapter adapter = new CustomListAdapter(getActivity(), jsonlist1, R.layout.list_item, new String[]{TITLE, DESCRIPTION}, new int[]{R.id.title, R.id.description});
                mainList.setAdapter(adapter);
            }
            else {
                displaysavedlv();
            }
        }
    }

    private void displaysavedlv() {
        try {
            dbHelper = new SqlHelper(getActivity().getApplicationContext());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        bdList = dbHelper.getAllData();

        CustomListAdapter adapter1 = new CustomListAdapter(getActivity(), bdList, R.id.list_item, new String[]{TITLE, DESCRIPTION}, new int[]{R.id.title, R.id.description});
        mainList.setAdapter(adapter1);
    }
    private boolean isNetworkConnected() {
        ConnectivityManager cm = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if (ni == null) {
            // There are no active networks.
            return false;
        } else
            return true;
    }
}

1 个答案:

答案 0 :(得分:1)

以下是我对您的理解,每当您建立连接时,您将所有数据放入数据库中,以便稍后在没有连接的情况下显示它们 但是在您的数据库包含许多数据之后,所以您只想显示检索到的最后一个数据,基本上答案是在将新数据放入数据库之前确保通过创建删除表或删除来删除数据库中的所有内容方法查询..然后你很高兴。