简体   繁体   中英

Android: retrieving data from mysql

I've been coding PHP but learning how to build an android app. In PHP I was able to retrieve data from mysql and then I can design my web layout by setting data to a variable...

$name = $SQL["name"];

Something like that and then I can mess with HTML. I know android is completely different, but I was wondering if someone can explain what I need to look into or has examples to show that I can mess around with designing a nice UI.

I currently have this:

try {
            JSONArray jArray = new JSONArray(result);
            int jArrayLength = jArray.length();
            List<String> listContents = new ArrayList<String>(jArrayLength);

            for(int i =0; i<jArray.length(); i++){
                JSONObject json_data = jArray.getJSONObject(i);
                listContents.add(json_data.getString("full_name"));
            }

            ListView myListView = (ListView) findViewById(R.id.front_page_listview);
            myListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listContents));

        }catch(JSONException e){
            Log.e("log_tag","Error parsin data "+e.toString());
}

Where

listContents.add(json_data.getString("full_name"));

Displays the full name, but what if I want to add additional information? like emails,phone numbers, some text, etc..

If I do this:

listContents.add(json_data.getString("full_name"));
listContents.add(json_data.getString("email"));

Its just going to give another row, but I want name and email to be in the same row, and I would like to be able to design the rows myself,

Any ideas?

Thanks!

You can make a custom adapter and a custom row layout. Then instead of android.R.layout.simple_list_item_1 you put a reference to the layout you want. The Adapter can be from your JSONObject array - jArray, holding all the data you want to display. Then in its getView method it inflates the custom view and puts all of the data you want into it.

Sorry if this isn't clear enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM