gpt4 book ai didi

java - 如何将字符串值传递给 fragment 上的 AsyncTask

转载 作者:行者123 更新时间:2023-12-01 09:01:03 26 4
gpt4 key购买 nike

如何将 ProductFragment、lv 和 urlAddress 传递给 AsyncTask。当我继承到AppCompatActivity时,没有错误,但是如果我继承到Fragment,就会出现错误行中的错误

new Downloader(ProductFragment.this,urlAddress,lv).execute();

这是我的产品 fragment

    import android.app.Fragment;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.wawi.wawireg.m_MySQL.Downloader;


public class ProductFragment extends Fragment {

final static String urlAddress = "http://**.**.**.**/**/****.php";

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.calendar_main, container, false);





Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton)view.findViewById(R.id.fab);

final ListView lv = (ListView)view.findViewById(R.id.lv);

fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new Downloader(ProductFragment.this,urlAddress,lv).execute();
}
});


return view; }

}

This is my AsyncTask

public class Downloader extends AsyncTask<Void,Void,String> {


Context c;
String urlAddress;
ListView lv;

ProgressDialog pd;



public Downloader(Context c, String urlAddress, ListView lv) {
this.c = c;
this.urlAddress = urlAddress;
this.lv = lv; }


@Override
protected void onPreExecute() {
super.onPreExecute();

pd=new ProgressDialog(c);
pd.setTitle("Retrieve");
pd.setMessage("Retrieving...Please wait");
pd.show();
}

@Override
protected String doInBackground(Void... params) {
return downloadData();
}

@Override
protected void onPostExecute(String jsonData) {
super.onPostExecute(jsonData);

pd.dismiss();

if(jsonData==null)
{
Toast.makeText(c,"Unsuccessful, No data Retrieved",Toast.LENGTH_SHORT).show();
}else {
//PARSE
DataParser parser=new DataParser(c,jsonData,lv);
parser.execute();

}
}

private String downloadData()
{
HttpURLConnection con=Connector.connect(urlAddress);
if(con==null)
{
return null;
}

try
{
InputStream is=new BufferedInputStream(con.getInputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(is));

String line;
StringBuffer jsonData=new StringBuffer();


while ((line=br.readLine()) != null)
{
jsonData.append(line+"\n");
}

br.close();
is.close();

return jsonData.toString();

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


return null;
}
}

最佳答案

这将解决错误,

new Downloader(getActivity(), urlAddress, lv).execute();

您的下载器需要一个Context作为第一个参数。

public Downloader(Context c, String urlAddress, ListView lv) {
this.c = c;
this.urlAddress = urlAddress;
this.lv = lv;
}

您不需要两个 AsyncTasks 来下载和解析数据。您可以使用单个 AsyncTask 来完成此操作。

关于java - 如何将字符串值传递给 fragment 上的 AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41661393/

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