使用php安装android mysql数据库的ListView

I am making an android application that connects to an external mysql database and i have a login and registration function working on it.

I have a table within my database called lessons and has the columns name and time in it. i have been searching for days of a way to read the database and display the contents of that table in listview but i can not find anything that works for me.

I realise there are a lot of similar question but i have not found any of them helpful and am really starting to struggle.

Here is my activity page:

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);
            }
        });

    }

}
} 

Here are my two xml files:

<?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>

The problem atm is that uid in the list adapter

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

can not be resolved or is not a field. But i have had this example without that error on another project but nothing shows up on the page.

dongyutan1703
dongyutan1703 发布你的logcat
7 年多之前 回复

3个回答



我不是肯定的(在Android上使用MySql时),但在使用sqlite时,你必须有一个名为_id的文件 或者它不起作用。 我将从你的mysql数据库开始。 您还必须在显示数据时选择该字段,否则它将无法工作。 我花了几个小时处理这个具体问题。 </ p>
</ div>

展开原文

原文

I'm not positive (when working with MySql on Android), but when working with sqlite, you have to have a filed called _id or it will not work. I would start with that in your mysql database. You also have to select that field when displaying data, or it will not work. I spent several hours dealing with this specific issue.



也许这个博客可以帮到你: http://nanang18nurhidayat.files.wordpress.com/2013/03/list-view-with-image- from-url-mysql-json-php.pdf </ p>
</ div>

展开原文

dongtuo7364
dongtuo7364 请将此内容发布为评论。
大约 7 年之前 回复



你应该使用R.id.name来广告第二个xml上的R.id.uid你为什么不创建那个id?< / p>
</ div>

展开原文

原文

you should ad R.id.uid on second xml with R.id.name.why you don't create that id?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐