当我单击一个选项卡时,您必须链接到另一个类别。问题是它失败了。当我在我的代码中加载 json 时,它不会从我希望它返回到主类的类中加载。
这是我的Java代码:
public class Sport extends ExampleActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.refresh:
loadData();
break;
}
return true;
}
public void loadData() {
Utils.init();
progressDialog = ProgressDialog.show(Sport.this, getString(R.string.app_name), "Laden...");
new GetDataTask().execute();
}
private void _showMessage(String msg) {
Toast.makeText(Sport.this, msg, Toast.LENGTH_LONG).show();
}
private boolean _hasConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
// test for connection
if (cm.getActiveNetworkInfo() != null
&& cm.getActiveNetworkInfo().isAvailable()
&& cm.getActiveNetworkInfo().isConnected()) {
return true;
} else {
System.out.println("Internet Connection Not Present");
return false;
}
}
private class GetDataTask extends AsyncTask<Void, Void, Integer> {
protected Integer doInBackground(Void... params) {
Private class GetDataTask extends AsyncTask<Void, Void, Integer>
{
protected Integer doInBackground(Void... params)
{
if(_hasConnection())
{
dataList.clear();
System.out.println("Start loading JSON");
JSONArray json =JSONFunctions.getJSONfromURL("http://www.example.com/report.phpcategory=sport&image=50x50&limit=40");
System.out.println("End loading JSON");
for (int i = 0; i < json.length(); i++) {
try {
JSONObject row = json.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", row.getString("id"));
map.put("title", row.getString("title"));
map.put("image", row.getString("image"));
map.put("date", row.getString("date"));
dataList.add(map);
}
catch (JSONException e)
{
e.printStackTrace();
}
}
System.out.println("End putting in dataList");
And here's is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</FrameLayout>
</LinearLayout>
</TabHost>