获取一个ListView从MySQL服务器抓取数据时工作 [英] Getting a ListView to work when grabbing data from a mySQL server

查看:94
本文介绍了获取一个ListView从MySQL服务器抓取数据时工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题所说,我有上有数据的MySQL数据库。我希望我的Andr​​oid应用程序检索数据,并将其插入到一个ListView。我不认为我正确地这样做,因为我现在的code;虽然不产生任何错误,不能正常工作。这里是我的code:

As the title says, I have a mySQL database that has data on it. I want my android app to retrieve that data and insert it into a ListView. I don't think I'm doing it correctly because my current code; although not producing any errors, doesn't work. Here is my code:

http://pastebin.com/sZy5dAzS

我只是想指出的是,当我将数据转换成一个TextView它显示在我的应用程序就好了。因此,它必须是我做错了我的ListView。我只是不知道是什么。

I'd just like to point out that when I insert that data into a TextView it shows up on my app just fine. So it must be that I'm doing something wrong with my ListView. I just don't know what.

只是为了更容易阅读这些都是我的ListView的部分:

Just to make it easier to read these are the parts of my ListView:

在我的code的顶部

List<String> nameData = new ArrayList<String>();

for循环为我的JSON解析:

The for loop for my JSON Parsing:

            try {
                jArray = new JSONArray(result);
                JSONObject json_data = null;
                currentList = (ListView) findViewById(R.id.list);



                for (int i = 0; i < jArray.length(); i++) {

                    json_data = jArray.getJSONObject(i);
                    nameData.add(drivername = json_data.getString("Driver_full_name"));
                    nameData.add(drivesfor = json_data.getString("Drives_for"));

                }

我有一个catch这里,你可以在引擎收录的链接看到,只是还没有公布它。

I do have a catch here as you can see in the pastebin link, just haven't posted it.

和onPostExecute

and the onPostExecute

protected void onPostExecute(String output){

        currentList.setAdapter(new ArrayAdapter<String>    (CurrentSeasonDrivers.this, android.R.layout.simple_list_item_2, nameData));
        Toast.makeText(CurrentSeasonDrivers, "DONE!",     Toast.LENGTH_LONG).show();

    }

只是重申;拉动数据时,以使code的其余部分是正确的一个TextView正常工作。

Just to reiterate; a TextView works fine when pulling the data so the rest of the code is correct.

真的AP preciate给予任何帮助。谢谢。

Really appreciate any help given. Thanks.

推荐答案

我不知道确切的问题是在你的code什么,但我有一种感觉它与您的适配器做。我有一个办法做到这一点,应该工作的建议,那就是更好,这基本上是创建自己的适配器更加灵活。

I am not sure what the exact problem is in your code but I have a feeling it has to do with your adapter. I have suggestion on a way to do it that should work and that is better and more flexible which is basically creating your own adapter.

首先使将要用作数据源

public class DriverItem {

    private String name;
    private String driversFor;

    public DriverItem(String n, String d) {
        super();
        this.name = n;
        this.driversFor = d;
    }

    // Getter and setter methods for all the fields.
    public String getName() {
        return name;
    }

    public String getDriversFor() {
        return driversFor;
    }

}

现在创建一个将用于创建视图的适配器

public class DriverListItemAdapter extends BaseAdapter {
    private List<DriverItem> listDriverItem;
    private Context context;

    public DriverListItemAdapter(Context ctx, List<DriverItem> list)
    {
        context = ctx;
        listDriverItem = list;
    }

    public int getCount() {
        // TODO Auto-generated method stub
        listDriverItem.size();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return listDriverItem.get(position);
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup viewGroup) {
        // TODO Auto-generated method stub
        DriverItem driver = listDriverItem.get(position);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        convertView = inflater.inflate(R.layout.list, null);
        TextView name = (TextView)convertView.findViewById(R.id.txtName);
        name.setText(driver.getName());

        return convertView
    }
}

现在,您需要将用于显示每个列表视图行的看法

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/txtName"
        android:layout_height="wrap_parent"
        android:layout_width="fill_parent" 
        />
</LinearLayout>

现在在你的code您只需创建驱动程序的ArrayList中,创建一个适配器出来的,并设置该适配器上的列表视图。

DriverListItemAdapter adapter;
ListView currentList = (ListView) findViewById(R.id.list);
ArrayList<DriverItem> listOfDriverListItem;

for(int i = 0; i < jArray.length(); i++){
            jObj = jArray.getJSONObject(i);        
            listOfDriverListItem.add(newDriverItem(jObj.getString("Driver_full_name"),jObj.getString("Drives_for")));
        }

adapter = new DriverListItemAdapter(this, listOfDriverListItem);
currentList.setAdapter(adapter);

您应该可以只复制所有这些事情粘贴到自己的类,它应该工作..和一个我在底部给更换您的ListView code ..
这应该工作,让我知道如果您有任何疑问

You should be able to just copy paste the all of these things into their own classes and it should work.. and replace your listview code with the one I gave at the bottom.. This should work, let me know if you have any questions

这篇关于获取一个ListView从MySQL服务器抓取数据时工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆