2013-04-02 52 views
2

我正在連接到外部mysql database的Android應用程序,我有一個登錄註冊功能做這個工作的android mysql數據庫。的ListView使用PHP

我在我的數據庫中有一個叫做課程的表格,裏面有列名和時間。我一直在尋找一種方法來讀取數據庫,並在listview中顯示該表的內容,但我找不到任何適合我的東西。

我意識到有很多類似的問題,但我還沒有找到任何有用的,我真的開始掙扎。

這是我的活動頁面:

package com.example.studentregister; 

//imports 

import com.example.studentregister.library.JSONParser; 
import com.example.studentregister.R; 

public class ProfilePage extends ListActivity { 
// Progress Dialog 
private ProgressDialog pDialog; 

// Creating JSON Parser object 
JSONParser jParser = new JSONParser(); 

ArrayList<HashMap<String, String>> lessonList; 

// url to get all products list 
private static String url_all_lessons = "http://192.168.0.6/android_login_api/get_all_lessons.php"; 

// JSON Node names 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_LESSONS = "lessons"; 
private static final String TAG_PID = "uid"; 
private static final String TAG_NAME = "name"; 

// products JSONArray 
JSONArray lessons = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.profilepage); 

    // Hashmap for ListView 
    lessonList = new ArrayList<HashMap<String, String>>(); 

    // Loading products in Background Thread 
    new LoadAllProducts().execute(); 

    // Get listview 
    ListView lv = getListView(); 



} 

// 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 LoadAllProducts extends AsyncTask<String, String, String> { 

    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(ProfilePage.this); 
     pDialog.setMessage("Loading products. 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_lessons, "GET", params); 

     // Check your log cat for JSON reponse 
     Log.d("All Products: ", json.toString()); 

     try { 
      // Checking for SUCCESS TAG 
      int success = json.getInt(TAG_SUCCESS); 

      if (success == 1) { 
       // products found 
       // Getting Array of Products 
       lessons = json.getJSONArray(TAG_LESSONS); 

       // looping through All Products 
       for (int i = 0; i < lessons.length(); i++) { 
        JSONObject c = lessons.getJSONObject(i); 

        // Storing each json item in variable 
        String id = c.getString(TAG_PID); 
        String name = c.getString(TAG_NAME); 

        // creating new HashMap 
        HashMap<String, String> map = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        map.put(TAG_PID, id); 
        map.put(TAG_NAME, name); 

        // adding HashList to ArrayList 
        lessonList.add(map); 
       } 
      } else { 
       // no products found 
       // Launch Add New product Activity 
       Intent i = new Intent(getApplicationContext(), 
         LoginActivity.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 
       * */ 
       ListAdapter adapter = new SimpleAdapter(
         ProfilePage.this, lessonList, 
         R.layout.eachlesson, new String[] { TAG_PID, 
           TAG_NAME}, 
         new int[] { R.id.uid, R.id.name }); 
       // updating listview 
       setListAdapter(adapter); 
      } 
     }); 

    } 

} 
} 

這裏是我的兩個XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <!-- Main ListView 
     Always give id value as list(@android:id/list) 
    --> 
<ListView 
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"/> 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <!-- Name Label --> 
    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="6dip" 
     android:paddingLeft="6dip" 
     android:textSize="17dip" 
     android:textStyle="bold" /> 

</LinearLayout> 

問題ATM在列表適配器UID

new int[] { R.id.uid, R.id.name }); 

無法解析或不是字段。但是我有這個例子沒有在另一個項目上的錯誤,但沒有顯示在頁面上。

+0

張貼您的logcat – Harshid

回答

0

我並不積極(在Android上使用MySql時),但使用sqlite時,必須有一個名爲_id的字段,否則它將無法工作。我會從你的mysql數據庫開始。顯示數據時,您還必須選擇該字段,否則它將無法工作。我花了幾個小時處理這個具體問題。

0

你應該廣告R.id.uid與R.id.name.why第二XML你不要創建ID?