gpt4 book ai didi

android - 在 RecyclerView 中使用 Activity

转载 作者:行者123 更新时间:2023-12-02 16:08:06 25 4
gpt4 key购买 nike

嗨,我希望用户asynctask在类中扩展RecyclerViewonPreExecute我应该使用一个 Activity viewDialog = new ViewDialog( );但我不知道应该用什么我的代码是:

public class MainCatRecycleAdapter extends RecyclerView.Adapter<viewholder> {

ArrayList<MainCatRecycleInfo> mainCatRecycleInfos = new ArrayList<>();

public MainCatRecycleAdapter(ArrayList<MainCatRecycleInfo> mainCatRecycleInfos) {
this.mainCatRecycleInfos = mainCatRecycleInfos;
}

@Override
public viewholder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_cat_recycle_product, parent, false);
return new viewholder(view);
}

@Override
public void onBindViewHolder(@NonNull viewholder holder, int position) {
MainCatRecycleInfo mainCatRecycleInfo = mainCatRecycleInfos.get(position);
holder.txtname.setText(mainCatRecycleInfo.name);

Picasso.with(G.Context).load(Paramater.pic + "maincatimage/" + mainCatRecycleInfo.image).into(holder.img);
Log.i("dsfsd", Paramater.pic + "maincatimage/" + mainCatRecycleInfo.image);

holder.id = mainCatRecycleInfo.id;
}

@Override
public int getItemCount() {
return mainCatRecycleInfos.size();
}
}


class viewholder extends RecyclerView.ViewHolder {
ViewDialog viewDialog;

public ImageView img;
public TextView txtname;
public String id = "";
public LinearLayout linearLayout;
Runnable r;

public viewholder(View itemView) {
super(itemView);
img = (ImageView) itemView.findViewById(R.id.mainimgrecycle);
txtname = (TextView) itemView.findViewById(R.id.maintextrcycle);
linearLayout = (LinearLayout) itemView.findViewById(R.id.linearrecycle);
linearLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

new AsyncTaskShowSubCats(Paramater.ip + "showSubCat.php",id).execute();

}
});
}



public class AsyncTaskShowSubCats extends AsyncTask {


public String link = "";
public String id="";


public AsyncTaskShowSubCats(String link, String id) {
this.link = link;
this.id = id;

}

@Override
protected Object doInBackground(Object[] objects) {
try {
String data = URLEncoder.encode("id", "utf-8") + "=" + URLEncoder.encode(id, "utf-8");


URL url = new URL(link);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(data);
writer.flush();


BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
builder.append(line);

}
ActivitySubCat.subcat = builder.toString();
ActivitySerch.serch = builder.toString();

Log.i("fdffs",builder.toString());



} catch (Exception e) {

}
return "";
}

@Override
protected void onPreExecute() {

viewDialog = new ViewDialog();

viewDialog.showDialog();

super.onPreExecute();
}

@Override
protected void onPostExecute(Object o) {
viewDialog.hideDialog();
super.onPostExecute(o);
}
}
}

最佳答案

要获取 Activity 的上下文,您可以通过适配器的构造函数传递上下文。像这样的事情:

public class MainCatRecycleAdapter extends RecyclerView.Adapter<viewholder> {

ArrayList<MainCatRecycleInfo> mainCatRecycleInfos = new ArrayList<>();
private Context context;

public MainCatRecycleAdapter(ArrayList<MainCatRecycleInfo> mainCatRecycleInfos, Context context) {
this.mainCatRecycleInfos = mainCatRecycleInfos;
this.context = context;
}
}

关于android - 在 RecyclerView 中使用 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59441919/

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