【发布时间】:2015-11-18 14:11:03
【问题描述】:
在我的应用程序的一部分循环中,我为我的 LinearLayout conteach 分配了一个 onclick 侦听器,但我不能使用 FragmentManager 类,这就是我不能在我的片段上执行事务的原因。我知道问题是因为我在OnClickListener 类中,并且没有现有的FragmentManager,如何在onClickListener 类中使用导入的类FragmentManager?
conteach.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SpecificArticleFragment spcf = new SpecificArticleFragment();
Bundle args = new Bundle();
args.putString("art_id", v.getTag().toString());
spcf.setArguments(args);
FragmentManager fm = getFragmentManager();
}
});
这是我全班的代码。
package com.example.navigationdrawerexample;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.FragmentManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class Connector extends AsyncTask<String,Void,String>{
private Context context;
private TextView headline;
private int collegeactive;
private String flag;
private TextView senderofheadline;
private TextView shortdescofheadline;
private LinearLayout listofitems;
public Connector(Context context,int college, String flag,TextView headline, TextView shortdesc, TextView sender,LinearLayout ll){
this.context = context;
this.headline = headline;
this.collegeactive = college;
this.flag = flag;
this.senderofheadline = sender;
this.shortdescofheadline = shortdesc;
this.listofitems = ll;
}
protected void onPreExecute(){
//Toast.makeText(context, "Please Wait", Toast.LENGTH_LONG).show();
//ProgressBar pb = new ProgressBar(this.context);
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
try{
String link = "";
if (flag == "headline"){
link = "http://rtu-astronet.com/emag/functions.php?action=printHeadline&coll_id="+collegeactive;
} else if (flag == "list"){
link = "http://rtu-astronet.com/emag/functions.php?action=printArticlePerCollege&collegeid="+collegeactive+"&acadornot=1";
}
URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
while ((line = in.readLine()) != null) {
sb.append(line);
//break;
}
in.close();
return sb.toString();
} catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
result = "{ \n \"Data\": \n " + result + " \n }";
if (flag == "headline"){
try{
JSONObject jsonRootObj = new JSONObject(result);
JSONArray jsonArray = jsonRootObj.optJSONArray("Data");
JSONObject jsonObject = jsonArray.getJSONObject(0);
this.headline.setText(jsonObject.optString("title").toString());
this.shortdescofheadline.setText(jsonObject.optString("shortdesc").toString());
this.senderofheadline.setText(jsonObject.optString("penname").toString());
} catch(JSONException e){
this.headline.setText(e.getMessage().toString());
}
} else if (flag == "list"){
try {
JSONObject jsonRootObj = new JSONObject(result);
JSONArray jsonArray = jsonRootObj.optJSONArray("Data");
for (int p = 0; p < jsonArray.length(); p++){
JSONObject jsonObject = jsonArray.getJSONObject(p);
LinearLayout conteach = new LinearLayout(this.context);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
conteach.setLayoutParams(lp);
conteach.setOrientation(LinearLayout.VERTICAL);
TextView art_title = new TextView(this.context);
art_title.setText(jsonObject.optString("title").toString());
art_title.setTextSize(20);
art_title.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));
TextView art_shortdesc = new TextView(this.context);
art_shortdesc.setText(jsonObject.optString("shortdesc").toString());
art_shortdesc.setTextSize(14);
art_shortdesc.setTypeface(null, Typeface.ITALIC);
art_shortdesc.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));
TextView art_sender = new TextView(this.context);
art_sender.setText("Written by: " + jsonObject.optString("sender").toString());
art_sender.setTextSize(14);
art_sender.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));
art_sender.setPadding(0, 0, 0, 15);
conteach.setTag(jsonObject.optString("id").toString());
conteach.setClickable(true);
conteach.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/*
* CollegeBulletinListFragment cblf = new CollegeBulletinListFragment();
Bundle args = new Bundle();
args.putString("passingWord", passingword);
cblf.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, cblf).commit();
*/
SpecificArticleFragment spcf = new SpecificArticleFragment();
Bundle args = new Bundle();
args.putString("art_id", v.getTag().toString());
spcf.setArguments(args);
FragmentManager fm = getFragmentManager();
}
});
conteach.addView(art_title);
conteach.addView(art_shortdesc);
conteach.addView(art_sender);
this.listofitems.addView(conteach);
}
} catch (JSONException e){
}
}
}
}
【问题讨论】:
-
这是一个活动类吗?请发布更多代码。
-
一个扩展了一个
的类,反正使用这个类的Fragment类有一个构造函数,并赋值一个包含fragment类的类的上下文 -
或者只是假设它是一个活动类。
-
请发布您的活动代码
-
我已经做过了,看看答案。