2

我想使用 Crouton Library 打印 asyntask 后收到的文章数量。

我的应用程序使用 navigationDrawer 和不同的片段。

我从不使用这个库,我想知道我必须在哪里执行对 Crouton.makeText 的调用才能在我的应用程序中打印 Crouton 通知?

有关信息,我的应用程序的每个片段都显示一个带有列表或文章的列表视图。

这是 RssService 类的代码:

import java.io.IOException;
import java.net.URL;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;

public class RssService extends AsyncTask<String, Void, List<Article>> {

    private ProgressDialog progress;
    private Context context;
    private BretagneNewsFragment derniereNewsListFrag;

    public RssService(BretagneNewsFragment derniereNewsListFragment) {
        context = derniereNewsListFragment.getActivity();
        derniereNewsListFrag = derniereNewsListFragment;
        progress = new ProgressDialog(context);
        progress.setMessage("Chargement en cours ...");
    }

    protected void onPreExecute() {
        Log.e("ASYNC", "PRE EXECUTE");
        progress.show();
    }

    protected  void onPostExecute(final List<Article>  articles) {
        Log.e("ASYNC", "POST EXECUTE");
        derniereNewsListFrag.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ArticleListAdapter adapter = new ArticleListAdapter(derniereNewsListFrag.getActivity(), articles);
                derniereNewsListFrag.setListAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        });
        progress.dismiss();

        Crouton.makeText(derniereNewsListFrag.getActivity(), "DD", Style.INFO, ???? ).show();

    }


    @Override
    protected List<Article> doInBackground(String... urls) {

        String feed1 = urls[0];
        String feed2 = urls[1];

        URL url1 = null;
        URL url2 = null;

        try {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            url1 = new URL(feed1);
            url2 = new URL(feed2);

            RssHandler rh = new RssHandler();

            xr.setContentHandler(rh);
            xr.parse(new InputSource(url1.openStream()));

            Log.d("RssService", "doInBackground : URL2 maintenant utilisée");

            xr.setContentHandler(rh);
            xr.parse(new InputSource(url2.openStream()));

            Log.d("RssService", "doInBackground : PARSING FINISHED");
            return rh.getArticleList();

        } catch (IOException e) {
            Log.e("RSS Handler IO", e.getMessage() + " >> " + e.toString());
        } catch (SAXException e) {
            Log.e("RSS Handler SAX", e.toString());
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            Log.e("RSS Handler Parser Config", e.toString());
        }

        return null;

    }
}

这是我的 ArticleListAdapter 的代码:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import com.rss.R;
import com.rss.utils.DateUtils;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;


public class ArticleListAdapter extends ArrayAdapter<Article> {

    private ArrayList<String> colorList = new ArrayList<String>();

    public ArticleListAdapter(Activity activity, List<Article> articles) {
        super(activity, 0, articles);
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Activity activity = (Activity) getContext();
        LayoutInflater inflater = activity.getLayoutInflater();

        View rowView = inflater.inflate(R.layout.fragment_article_list, null);
        Article article = getItem(position);


        TextView textView = (TextView) rowView.findViewById(R.id.article_title_text);
        textView.setText(article.getTitle());

        TextView src = (TextView) rowView.findViewById(R.id.article_source);
        src.setText(article.getSource());

        if (article.getSource().equals(" LE TELEGRAMME ")) {
            src.setBackgroundColor(Color.parseColor("#D91E00"));
        } else if (article.getSource().equals(" OUEST FRANCE ")) {
            src.setBackgroundColor(Color.parseColor("#FFFFFF"));
            src.setTextColor(Color.parseColor("#EB030C"));
            src.setTypeface(null, Typeface.BOLD);
        } else {
            Log.d("INFO", "SPECIAL CASE !!!");
        }

        TextView dateView = (TextView) rowView.findViewById(R.id.article_listing_smallprint);
        String pubDate = article.getPubDate();

        TextView descriptionView = (TextView) rowView.findViewById(R.id.article_title_description);

        String completeString = article.getDescription();
        String[] strArray = completeString.split("<img");
        String result = strArray [0];

        // Permet de mettre une chaine dans la cas ou nous n'avons pas de description.
        if (result == null || result.trim().equals("")){
            descriptionView.setText("Cet article ne contient pas de description supplémentaire.");
        } else {
            String firstLetterDesc = result.substring(0,1);
            if (firstLetterDesc.equals(" ")) {
                String resultToSet = changeCharInPosition(0, '\0', result);
                descriptionView.setText(resultToSet);
            } else {
                descriptionView.setText(result);
            }
        }

        TextView premiereLettre = (TextView) rowView.findViewById(R.id.premiere_lettre);
        String titleTmp = article.getTitle();
        String firstLetter = titleTmp.substring(0,1);
        premiereLettre.setText(firstLetter.toUpperCase());

        SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy kk:mm:ss Z", Locale.ENGLISH);
        Date pDate;
        try {
            pDate = df.parse(pubDate);
            pubDate = "   IL Y A " + DateUtils.getDateDifference(pDate);
        } catch (ParseException e) {
            Log.e("DATE PARSING", "Error parsing date..");
            pubDate = "published by " + article.getAuthor();
        }
        dateView.setText(pubDate);

        // Change color of the background of first Letter
        LinearLayout rl = (LinearLayout) rowView.findViewById(R.id.color_letter);
        String codeCouleur = getRandomColorFromList();
        rl.setBackgroundColor(Color.parseColor(codeCouleur));

        return rowView;
    } 

    public String changeCharInPosition(int position, char ch, String str){
        char[] charArray = str.toCharArray();
        charArray[position] = ch;
        return new String(charArray);
    }


}

谢谢你的帮助。

4

2 回答 2

5

为了显示Crouton,您需要做的就是参考您的活动,您确实拥有。

Crouton.makeText(derniereNewsListFrag.getActivity(), "DD", Style.INFO, ???? ).show();

可以替换为

Crouton.makeText(derniereNewsListFrag.getActivity(), "DD", Style.INFO, (ViewGroup) derniereNewsListFrag.getView()).show();

或者

Crouton.showText(derniereNewsListFrag.getActivity(), "DD", Style.INFO, (ViewGroup) derniereNewsListFrag.getView());

但请注意,即使您的 Fragment 不可用(null),您的 Service 基本上也可以运行。

于 2013-07-03T09:38:19.883 回答
0

Crouton.makeText(getActivity(), "请选择一个类别", Style.INFO).show();

使用getActivity()而不是this

于 2015-01-09T09:32:03.303 回答