gpt4 book ai didi

java - 在 AsyncTask onPostExecute 中动态添加一个按钮

转载 作者:行者123 更新时间:2023-11-29 22:02:53 27 4
gpt4 key购买 nike

我试图在 AsyncTaskonPostExecute 方法中动态添加一个 Button。我在扩展 Fragment 的类中执行此操作。我可以使用这段代码在 AsyncTask 之外动态创建一个 Button:

public class Tab2Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
LinearLayout theLayout = (LinearLayout) inflater.inflate(R.layout.tab2, container, false);
Context mFragmentContext=getActivity().getApplicationContext();
Button btn=new Button(mFragmentContext);
btn.setText("Hello Button");
RelativeLayout.LayoutParams paramsd = new RelativeLayout.LayoutParams(150,30);
paramsd.height = paramsd.WRAP_CONTENT;
paramsd.width = paramsd.WRAP_CONTENT;
btn.setLayoutParams(paramsd);
theLayout.addView(btn);

Button test = (Button)theLayout.findViewById(R.id.test41);
test.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v("response", "Button Clicked");
new loadSomeStuff().execute();
Intent log = new Intent();
log.setClass(getActivity(), Assignment.class);
startActivity(log);
}
});

return theLayout;
}

// Method to load stuff using async task. Grabs information from URL and
// calls read stream
public class loadSomeStuff extends AsyncTask<String, Integer, String> {
protected String doInBackground(String... arg0) {
try {
int limit = 100;
String accessToken = "";
URL url = new URL( "SomeSite" + accessToken
+ "&status=active" + "&limit=" + limit);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Accept", "application/json");
readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

protected void onPostExecute( String result ) {
super.onPostExecute(result);

Context mFragmentContext=getActivity().getApplicationContext();
Button btn=new Button(mFragmentContext);
btn.setText("Hello Button");
RelativeLayout.LayoutParams paramsd = new RelativeLayout.LayoutParams(150,30);
paramsd.height = paramsd.WRAP_CONTENT;
paramsd.width = paramsd.WRAP_CONTENT;
btn.setLayoutParams(paramsd);
//theLayout.addView(btn);

Log.v("response", "on post biatch");
}
}

// Processes information from URL and prints it in format
private void readStream(InputStream in) throws JSONException {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));

String line = reader.readLine();
String total_results = new JSONObject(line).getJSONObject(
"response").getString("total_results");
int assignCount = Integer.parseInt(total_results.toString());
Log.v("response", total_results);
JSONObject data;

for (int i = 0; i < assignCount; i++) {
data = new JSONObject(line).getJSONObject("response");
String id = data.getJSONArray("data").getJSONObject(i).getString("id");
String title = data.getJSONArray("data").getJSONObject(i).getString("title");
Log.v("response", title);

}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

}

问题是,当我尝试将此代码放入 AsyncTaskonPostExecute 时,addview(btn) 行有错误,因为 Layout 未定义。我不知道如何传递 Layout。有没有办法用某种内置方法获取 Activity 的 Layout

最佳答案

为什么不向您的 loadSomeStuff 类添加一个构造函数?在构造函数上,传递要将 Button 添加到的 View

public class loadSomeStuff extends AsyncTask<String, Integer, String> {
private View view;

public loadSomeStuff(View v) {
view = v;
}

public String doInBackground(String... strings) {
//...
}

public void onPostExecute(String result) {
//...
view.addView(btn);
}
}

关于java - 在 AsyncTask onPostExecute 中动态添加一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11509642/

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