gpt4 book ai didi

android - 我如何在我的应用程序 fragment 应用程序中调用 crouton.makeText?

转载 作者:太空狗 更新时间:2023-10-29 16:41:20 31 4
gpt4 key购买 nike

我想使用 Crouton 库打印异步任务后收到的文章数。

我的应用使用 navigationDrawer 和不同的 fragment 。

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

有关信息,我的应用程序的每个 fragment 都显示带有列表或文章的 ListView 。

这是 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);
}


}

感谢您的帮助。

最佳答案

要显示面包丁,您需要做的就是引用您的 Activity,您确实拥有该 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),您的服务基本上也可以运行。

关于android - 我如何在我的应用程序 fragment 应用程序中调用 crouton.makeText?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17327355/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com