0

私は実際の問題を抱えており、それを解決する方法がわかりません。アプリケーションで sharedpreferences を使用して、リストビューに項目を保存しようとしています。私はインターネットを持っていないとき、アプリケーションに例外があるので、接続がないときに情報を保存しようとします.sharedpreferencesを使用しましたが、メインまたは自分でそれを使用する方法がわかりませんでした.アダプターの原因私は多くの textview を持っています.ここでメインからの私の非同期タスク:

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;
    }
4

2 に答える 2

0

参考までに、アプリケーションに大量のデータが含まれている場合は、ShatedPreferences の代わりに SQLITE を使用することをお勧めします。また、アプリケーションをオフラインで使用したい場合は、1 つのことを行うこともできます。

これは、アクティブなデータ接続を確認するのに役立ちます

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

ケース1)データ接続がWebサービス呼び出しからレコードを取得し、それを繰り返して各レコードをデータベースに挿入し、共通のアダプターを作成する場合は、オンラインとオフラインの両方のデータで動作し、リストに表示するか、あなたが何をしたかを確認してください欲しいです

ケース 2) アクティブなデータ接続がない場合は、データベースからレコードをフェッチし、アダプターを準備して、リストに表示するか、必要なものを表示します

于 2013-04-26T13:20:16.037 に答える
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 Array に保存される場合があります。詳細については、 http://www.b2creativedesigns.com//sharedpreferences.php を参照してください。アプリケーションをオンラインで起動するたびにアレイをクリアしたい場合があります。その後、必要なデータをこれらのアレイに保存できます。次に、sharedPreferences 配列からリストビューに情報を読み込む必要があることは明らかです。

于 2013-04-24T17:01:55.087 に答える