简体   繁体   English

Android:从mysql中检索数据

[英]Android: retrieving data from mysql

I've been coding PHP but learning how to build an android app. 我一直在编写PHP,但学习如何构建一个Android应用程序。 In PHP I was able to retrieve data from mysql and then I can design my web layout by setting data to a variable... 在PHP中,我能够从mysql中检索数据,然后我可以通过将数据设置为变量来设计我的Web布局......

$name = $SQL["name"];

Something like that and then I can mess with HTML. 这样的东西,然后我可以搞乱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. 我知道android完全不同,但我想知道是否有人可以解释我需要调查的内容,或者有一些例子可以证明我可以设计一个漂亮的用户界面。

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. 然后,而不是android.R.layout.simple_list_item_1你引用了你想要的布局。 The Adapter can be from your JSONObject array - jArray, holding all the data you want to display. 适配器可以来自您的JSONObject数组 - jArray,包含您要显示的所有数据。 Then in its getView method it inflates the custom view and puts all of the data you want into it. 然后在其getView方法中,它会扩展自定义视图并将所需的所有数据放入其中。

Sorry if this isn't clear enough. 对不起,如果这还不够清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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