如何在重新启动活动

时间:2015-05-13 14:00:17

标签: android sqlite android-fragments android-listview sharedpreferences

我正在尝试实现下面的截图,从A到BI已经能够在列表项单击上更改文本颜色和图像资源。但是当我退出应用程序并再次打开它时,屏幕截图A恢复而不是BI将喜欢保存listview的实例,当我重新打开片段时,重新加载这个实例。

我的问题是:

我已经研究过并发现我应该使用sharedPreference保存实例并从那里重新加载。但是,请记住我正在将json解析为sqlite android并在此片段中的listview上显示数据。所以将从sharedPeferences重新加载影响我从数据库加载的listview?

我是一个新手,肯定不知道如何处理这个。任何帮助将不胜感激。 enter image description here 我的碎片:

public class Inbox extends Fragment {
    private RequestQueue mRequestQueue;
    private String TAG = this.getClass().getSimpleName();
    ListView listView;
    Database database;
    private static String Title = "title";
    private static String Desc = "desc";
    private ProgressDialog pd;
    ContactListAdapter contactListAdapter;
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

      //As you can see,my list is being loaded from database

        database = new Database(activity);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.inbox, container, false);
        listView=(ListView)rootView.findViewById(R.id.listView);

      //volley makes networking calls for json
        mRequestQueue = Volley.newRequestQueue(getActivity());
        final String url = "http://snapt.t15.org/emp7.js";
        pd = new ProgressDialog(getActivity());
        pd.setMessage("Please Wait...");
        pd.show();
       pd.setCancelable(false);
        pd.setCanceledOnTouchOutside(false);
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
        try {
            Thread.sleep(2000);
        } catch (Exception e) {

        }
        JsonObjectRequest jr = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.i(TAG, response.toString());
                parseJSON(response);
                pd.dismiss();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
             pd.dismiss();
            }
        });
        mRequestQueue.add(jr);
            }
        });
        thread.start();
        ArrayList<ContactListItems> contactList =database.fetchData();
        ContactListAdapter contactListAdapter = new ContactListAdapter(
                getActivity().getApplicationContext(), contactList);
        listView.setAdapter(contactListAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

          //Here is where i am changing the image and text color,but i need to save this to shared preference but i dont know how to handle this

                ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
                TextView txt=(TextView)view.findViewById(R.id.tv_name);
                TextView txt1=(TextView)view.findViewById(R.id.tv_phone);
                txt1.setTextColor(Color.parseColor("#bdbdbd"));
                txt.setTextColor(Color.parseColor("#bdbdbd"));
                imageView.setImageResource(R.drawable.open);

                txt.invalidate();
                txt1.invalidate();
                imageView.invalidate();

                String desc = ((TextView) view.findViewById(R.id.tv_phone))
                        .getText().toString();
                String title = ((TextView) view.findViewById(R.id.tv_name))
                        .getText().toString();
                Intent intent=new Intent(getActivity().getApplicationContext(),InboxDetails.class);
                intent.putExtra(Title, title);
                intent.putExtra(Desc, desc);
                startActivity(intent);
            }
        });

        return rootView;
    }

  private void parseJSON(JSONObject json) {
        try{
            JSONArray items = json.getJSONArray("Employee");
            for(int i=0;i<items.length();i++){

                JSONObject item = items.getJSONObject(i);
                String id=item.getString("id").toString();

                String name=item.getString("name").toString();

                String salary=item.getString("salary").toString();

                database.insertData(id,name,salary);
            }
        }
        catch(Exception e){
            e.printStackTrace();
        }

    }

}

修改 我发现这个方法我可以用于图像,但无法在我的代码中修复它:

// method for bitmap to base64
public static String encodeTobase64(Bitmap image) {
    Bitmap immage = image;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);

    Log.d("Image Log:", imageEncoded);
    return imageEncoded;

}

//Pass your bitmap inside this method like something in your preference-

SharedPreferences.Editor editor = myPrefrence.edit();
    editor.putString("namePreferance", itemNAme);
    editor.putString("imagePreferance", encodeTobase64(yourbitmap));              editor.commit();

//3)And when you want any where display your image just convert it into bitmap again using decode method-

// method for base64 to bitmap
  public static Bitmap decodeBase64(String input) {
   byte[] decodedByte = Base64.decode(input, 0);
      return BitmapFactory
      .decodeByteArray(decodedByte, 0, decodedByte.length);
    }

0 个答案:

没有答案