gpt4 book ai didi

android - RecyclerView 适配器内部的 AsyncTask,如何同步?

转载 作者:行者123 更新时间:2023-11-29 01:23:42 25 4
gpt4 key购买 nike

我在我的 RecyclerView 的适配器中使用了一个 AsyncTask。这是到目前为止的代码:

    public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.FeedsViewHolder>
{
private static final String CHECK = "http";

DataHolder d1 = new DataHolder();

public class FeedsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
CardView cv;

ImageView profileDp;
TextView userName;
TextView nameOfUser;
TextView tweetText;
String url;


FeedsViewHolder(final View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);

profileDp = (ImageView)itemView.findViewById(R.id.profile_pic);
userName = (TextView)itemView.findViewById(R.id.tweet_user_name);
nameOfUser = (TextView)itemView.findViewById(R.id.tweet_name_of_user);
tweetText = (TextView)itemView.findViewById(R.id.tweet_text);

Typeface face= Typeface.createFromAsset(itemView.getContext().getAssets(), "fonts/Roboto-Regular.ttf");
tweetText.setTypeface(face);
nameOfUser.setTypeface(face);
userName.setTypeface(face);

if(!d1.feeds.get(getLayoutPosition()+1).getLinks().isEmpty())
{
url = d1.feeds.get(getLayoutPosition()+1).getLinks().get(0);
new SoupeX().execute(url);

tweetText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
itemView.getContext().startActivity(i);

}
});
}

itemView.setOnClickListener(this);

}


@Override
public void onClick(View view)
{

}

}

private class DataHolder
{
List<Status> feeds;

}

CustomAdapter(List<Status> mpost){
this.d1.feeds = mpost;
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}


@Override
public FeedsViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card,
viewGroup, false);
FeedsViewHolder pvh = new FeedsViewHolder(v);
return pvh;
}

@Override
public void onBindViewHolder(FeedsViewHolder feedViewHolder, int i)
{

if(d1.feeds.get(i).getUser().getProfileImageUrlHttps()!=null)
{

Picasso.with(feedViewHolder.profileDp.getContext())
.load(d1.feeds.get(i).getUser().getProfileImageUrlHttps())
.into(feedViewHolder.profileDp);

}

if(d1.feeds.get(i).getUser().getName().length()>12)
{
feedViewHolder.nameOfUser.setText(d1.feeds.get(i).getUser().getName().substring(0,11));
}
else
{
feedViewHolder.nameOfUser.setText(d1.feeds.get(i).getUser().getName());
}


feedViewHolder.userName.setText("@"+d1.feeds.get(i).getScreenName());
try
{
int temp = d1.feeds.get(i).getText().indexOf(CHECK);

if (temp != -1)
{
String A = d1.feeds.get(i).getText().substring(0, temp);
String B = d1.feeds.get(i).getText().substring(temp);


if(B.length()>30)
B = B.substring(0,30)+"...";

String C = "<font color='#008FB3'>" + B + "</font>";
feedViewHolder.tweetText.setText(Html.fromHtml(A + C));

}
else
{
feedViewHolder.tweetText.setText(d1.feeds.get(i).getText());
}
}
catch (Exception e)
{e.printStackTrace();}

//feedViewHolder.profileDp.setImageUrl(d1.feeds.get(i).getUser().getProfileImageUrlHttps(),imageLoader);

}

@Override
public int getItemCount()
{

if(d1.feeds!=null)
{
return d1.feeds.size();
}
else
{
return 0;
}
}

private class SoupeX extends AsyncTask <String, String,String>
{

@Override
protected void onPreExecute()
{

}

protected String doInBackground(String... urls)
{
try
{
Log.w("LOL:","WORK_TEST:"+urls[0]);
Document doc = Jsoup.connect(urls[0]).userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0")
.referrer("http://www.google.com").timeout(1000*5).get();
Elements images = doc.select("img[src~=(?i)\\.(png|jpe?g|gif)]");
//images.get(0).attr("src");
for (Element image : images) {

Log.w("INCREDIBLE::", image.attr("src"));
}

}
catch (Exception e){e.printStackTrace();}

return "";
}

protected void onPostExecute(String jsonString)
{

}
}

}

现在的问题是,如何将结果填充回RecyclerView,说我想将一个String填充到一个TextView,过程是什么,如何获取getLayoutPosition()来填充在确切的地方?谢谢

最佳答案

你可以使用

((RecyclerView.LayoutParams)itemView.getLayoutParams()).getViewAdapterPosition()

用于获取布局位置。

已编辑
您可以像这样在 onPostExecute 中访问 textView。

private class SoupeX extends AsyncTask <String, String,String>
{
private TextView textView;
public SoupeX(TextView textView){
this.textView = textView;
}
...
@Override
protected void onPostExecute(String result) {
textView.setText(result);
}

已编辑

这里可以绑定(bind)xml

    private TextView textView;
FeedsViewHolder(final View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.yourtextviewid);

关于android - RecyclerView 适配器内部的 AsyncTask,如何同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35254964/

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