我需要调用Riverfragment.java中的courseprof.java(扩展片段)。我在courseprof.java中有我的硬编码设计,这就是为什么我需要在片段内调用它。我使用意图显示它,它显示类,但导航菜单消失。
Riverfragment.java:
package in.wptrafficanalyzer.navigationdrawerdemo;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class RiverFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Retrieving the currently selected item number
int position = getArguments().getInt("position");
// List of rivers
String[] rivers = getResources().getStringArray(R.array.rivers);
// Creating view correspoding to the fragment
View v = inflater.inflate(R.layout.welcome, container, false);
if(rivers[position].equals("Courses"))
{
Intent intent = new Intent(getActivity(), Courseprof.class);
startActivity(intent);
}
}
Courseprof.java
package in.wptrafficanalyzer.navigationdrawerdemo;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TableRow.LayoutParams;
public class Courseprof extends Activity{String data = "";
TableLayout tl;
TableRow tr;
Button btcourse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.courses);
tl = (TableLayout) findViewById(R.id.maintable);
final getCoursephp getdb = new getCoursephp();
new Thread(new Runnable() {
public void run() {
data = getdb.getCourseFromDB();
System.out.println(data);
runOnUiThread(new Runnable() {
@Override
public void run() {
ArrayList<getCourse> courseusers = parseJSON(data);
addData(courseusers);
}
});
}
}).start();
}
public ArrayList<getCourse> parseJSON(String result) {
ArrayList<getCourse> courseusers = new ArrayList<getCourse>();
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
getCourse user = new getCourse();
user.setRoom(json_data.getInt("rooms_id")); //ipinasa dito
user.setCourse(json_data.getInt("course_id"));
user.setSection(json_data.getInt("sections_id"));
courseusers.add(user);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return courseusers;
}
@SuppressWarnings({ "rawtypes" })
public void addData(ArrayList<getCourse> courseusers) {
for (Iterator i = courseusers.iterator(); i.hasNext();) {
getCourse p = (getCourse) i.next();
/** Create a TableRow dynamically **/
tr = new TableRow(this);
/** Creating a TextView to add to the row **/
btcourse=new Button(this);
btcourse.setText(p.getCourseId()+p.getRoom()+p.getSection());
btcourse.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
btcourse.setPadding(5, 5, 5, 5);
btcourse.setBackgroundColor(Color.parseColor("#D8BFD8"));
LinearLayout Ll = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.setMargins(5, 2, 2, 2);
//Ll.setPadding(10, 5, 5, 5);
Ll.addView(btcourse,params);
tr.addView((View)Ll);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
}
}
courses.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_below="@+id/textView1">
<TableLayout android:layout_width="wrap_content"
android:layout_height="0dp"
android:stretchColumns="1,1,1"
android:id="@+id/maintable" >
</TableLayout>
</ScrollView>
</RelativeLayout>