2013-04-24 61 views
0

我有一个真正的问题,我不知道如何解决它, 我尝试在我的应用程序上使用sharedpreferences来保存listview上的一些项目。 因为当我没有互联网我有我的apllication exeption所以我尝试保存信息,当我没有connexion,我使用sharedpreferences,但我不知道如何使用它在主或上我的适配器,因为我有很多的TextView从主我在这里的asynchtask:SharedPreferences与列表视图

class FetchRecentPosts extends AsyncTask<Void, Void, Void> { 

     private ProgressDialog progressDialog; 
     @Override 
     protected void onPreExecute() {   
      super.onPreExecute(); 
      progressDialog = ProgressDialog.show(MainActivity.this, "", getString(R.string.loading_message)); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      articles = Services.getRecentPosts(); 

      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      for (Article article : articles) { 
       System.out.println(article.getTitle()); 
      } 
      progressDialog.dismiss(); 

      ArticlesAdapter adapter = new ArticlesAdapter(MainActivity.this, R.layout.list_item, articles); 
      articlesList.setAdapter(adapter); 



     } 

,这是我的适配器

public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = convertView; 
     Article article = getItem(position); 

     if (convertView == null) { 

      view = inflater.inflate(R.layout.list_item, null); 

      image = (ImageView) view.findViewById(R.imageviews.imgArticle); 

      TextView txtArticleTitle = (TextView) view.findViewById(R.textviews.txtArticleTitle); 
      TextView txtArticleExcerpt = (TextView) view.findViewById(R.textviews.txtArticleExcerpt); 
      TextView txtArticleDate = (TextView) view.findViewById(R.id.textArticleDate); 

      txtArticleTitle.setText(ArabicUtilities.reshape(article.getTitle())); 
      txtArticleExcerpt.setText(ArabicUtilities.reshape(article.getExcerpt())); 
      txtArticleDate.setText(article.getDate().toGMTString()); 

      Typeface tf = Typeface.createFromAsset(view.getContext().getAssets(), "fonts/Roboto-Regular.ttf"); 
      txtArticleTitle.setTypeface(tf); 

      imageLoader.displayImage(article.getImg(), image, options); 
     } 
     return view; 
    } 
+1

你想在SharedPreferences保存什么什么都?你得到的例外是什么?究竟是什么问题? – SimonSays 2013-04-24 16:31:27

+0

我从json返回信息。当我有互联网时,一切都很好,但是当没有互联网时,应用程序停止,我想要保存这些信息,当我开始我的应用程序时显示最后一条信息,意味着有一个离线应用程序。 – Sherlock 2013-04-24 16:37:52

回答

0

所以我理解你想保存下载的数据从JSON返回到您的应用程序,当你离线启动它时,你希望它们从shaeredPreferences中出现。

这是我会怎么做:

首先,如果我没有连接我不想有一个例外,要么所以我会检查,如果应用程序有互联网连接:

public boolean isOnline() { 
     ConnectivityManager cm = 
      (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     NetworkInfo netInfo = cm.getActiveNetworkInfo(); 
     if (netInfo != null && netInfo.isConnectedOrConnecting()) { 
      return true; 
     } 
     return false; 
    } 

数据可能会被保存到sharedPreferences数组中。你可以在http://www.b2creativedesigns.com//sharedpreferences.php了解更多。您可能希望每次在线启动应用程序时清除阵列,然后可以将这些数据保存到这些阵列中。那么显然你必须从sharedPreferences数组中读取信息到列表视图中。

+0

谢谢你jani,我会试试这个,即使这让我发疯:D谢谢你。 – Sherlock 2013-04-24 17:28:25

+0

和我有问题,我应该使用我的适配器或mainActivity的sharedpreferenses?我很困惑 ? – Sherlock 2013-04-24 17:29:08

+0

您应该在您的MainActivity中使用 – 2013-04-24 17:36:04

0

仅供参考,如果你的应用程序中包含建议海量数据的使用,而不是ShatedPreferences SQLite和如果u想工作在离线应用程序,你也可以做一件事

这将帮助你检查活动数据连接

public boolean isNetworkAvailable() { 
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo activeNetworkInfo = connectivityManager 
      .getActiveNetworkInfo(); 
    return activeNetworkInfo != null && activeNetworkInfo.isConnected(); 
} 

情况1)如果您有数据连接,您可以通过Web服务调用记录,并遍历并装入每个记录到数据库,并编写一个通用适配器,请确保它会与在线和离线数据工作并将其显示在列表中或您想要什么

情况2)如果你没有有效的数据连接从数据库读取记录,并准备适配器,并在列表中显示或你想要