のメソッドに をButton
動的に追加しようとしています。を拡張するクラス内でこれを行っています。このコードを使用して、の外側を動的に作成できます。onPostExecute
AsyncTask
Fragment
Button
AsyncTask
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();
}
}
}
}
}
onPostExecute
問題は、このコードをに入れようとすると、が定義されていないためAsyncTask
、addview(btn)
行にエラーが発生することです。Layout
を渡す方法がわかりませんLayout
。Layout
ある種の組み込みメソッドを使用してアクティビティを取得する方法はありますか?