我想使用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);
}
}
感谢您的帮助。
答案 0 :(得分:5)
为了显示一个Crouton,您需要做的就是引用您的Activity。
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),您的服务基本上也可以运行。
答案 1 :(得分:0)
Crouton.makeText(getActivity(),“请选择一个类别”,Style.INFO).show();
使用getActivity()
代替this