我正在尝试从 mysql 获取数据并在 GridView 中显示,但我无法做到。结果是错误。我希望有人能帮助我。提前致谢。
这是我的 AllIconsActivity.java 代码:
public class AllIconsActivity extends Activity {
GridView gridView;
SimpleAdapter adapter;
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> iconsList;
// url to get all products list
private static String url_all_icons = "http://192.168.1.130:8080/android_connect/get_all_icons.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_TB_MainContent = "TB_MainContent";
private static final String TAG_ID = "id";
private static final String TAG_ICONNAME = "iconname";
// products JSONArray
JSONArray TB_MainContent = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_icons);
// Hashmap for ListView
iconsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllIcons().execute();
gridView = (GridView) findViewById(R.id.gridView1);
// Get listview
// ListView lv = getListView();
// GridView lv = getGridView();
// on seleting single product
// launching Edit Product Screen
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long pid) {
// getting values from selected ListItem
String id = ((TextView) view.findViewById(R.id.id)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
EditIconActivity.class);
// sending pid to next activity
in.putExtra(TAG_ID, id);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
}
// Response from Edit Product Activity
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if result code 100
if (resultCode == 100) {
// if result code 100 is received
// means user edited/deleted product
// reload this screen again
Intent intent = getIntent();
finish();
startActivity(intent);
}
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllIcons extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllIconsActivity.this);
pDialog.setMessage("Loading icons. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_icons, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Icons: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
TB_MainContent = json.getJSONArray(TAG_TB_MainContent);
// looping through All Products
for (int i = 0; i < TB_MainContent.length(); i++) {
JSONObject c = TB_MainContent.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String iconname = c.getString(TAG_ICONNAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_ICONNAME, iconname);
// adding HashList to ArrayList
iconsList.add(map);
}
} else {
// no products found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
NewIconActivity.class);
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
adapter = new SimpleAdapter(
AllIconsActivity.this, iconsList,
R.layout.screen_list, new String[] { TAG_ID,
TAG_ICONNAME},
new int[] { R.id.id, R.id.iconname });
// updating listview
gridView.setAdapter(adapter);
}
});
}
}
}
这是我的 activity_all_icon.xml 代码
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="3" >
</GridView>
这是我的 screen_list.xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/id"
android:layout_width="50px"
android:layout_height="50px"
android:layout_gravity="top"
android:layout_marginRight="10px"/>
<TextView
android:id="@+id/iconname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5px"
android:text="@+id/label"
android:textSize="15px" />
</LinearLayout>
这是我的错误:
Activity com.example.digital_catalog.AllIconsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40524628 that was originally added here
android.view.WindowLeaked: Activity com.example.digital_catalog.AllIconsActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40524628 that was originally added here
at android.view.ViewRoot.<init>(ViewRoot.java:266)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:174)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:117)
at android.view.Window$LocalWindowManager.addView(Window.java:424)
at android.app.Dialog.show(Dialog.java:241)
at com.example.digital_catalog.AllIconsActivity$LoadAllIcons.onPreExecute(AllIconsActivity.java:126)
at android.os.AsyncTask.execute(AsyncTask.java:391)
at com.example.digital_catalog.AllIconsActivity.onCreate(AllIconsActivity.java:63)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3693)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)