2012-08-31 108 views
1

正如标题所说,我有一个有数据的mySQL数据库。我想我的android应用程序检索该数据并将其插入到ListView中。我不认为我做得正确,因为我目前的代码;虽然不会产生任何错误,但不起作用。这里是我的代码:从mySQL服务器获取数据时获取ListView的功能

http://pastebin.com/sZy5dAzS

我只是想指出的是,当我插入数据到一个TextView它出现在我的应用程序就好了。所以一定是我在做我的ListView错误。我只是不知道是什么。

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

在我的代码

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

的顶部for循环我的JSON解析:

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

       } 

我的确在这里有一个捕获,你可以看到在pastebin链接,只是没有发布它。

和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(); 

    } 

只是重申;一个TextView在抽取数据时工作正常,所以剩下的代码是正确的。

真的很感谢任何帮助。谢谢。

回答

1

我不确定你的代码中确切的问题是什么,但我有一种感觉,它与你的适配器有关。我建议一种方法来实现它,它应该可以工作,并且更好,更灵活,基本上可以创建自己的适配器。

首先使这将用作数据源

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 
    } 
} 
适配器一个小Driver类

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

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

现在在你的代码只需创建司机的数组列表,创建一个适配器出这一点,并设置你的ListView该适配器。

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

您应该能够把刚才复制所有的这些东西粘贴到自己的类,它应该工作..并与一个我在底部了.. 这应该更换你的列表视图代码,让我知道如果您有任何问题

+0

感谢您的。我是在想。我不必在onPostExecute中放入任何东西吗?否则,应用程序页面上将不显示任何内容 无论如何,我跑的代码,并得到了一个力量关闭。这里是logcat:http://pastebin.com/Z4Svaw6t –

+0

你可以粘贴你的新代码到粘贴箱,并请链接它。另外是的,这将是一个好主意,做一个postExecute而不是实际的后台任务实际视图更新/设置..你的任务应该得到的JSON和后执行应该使用JSON设置视图 – krilovich

+0

当然,这里是:http://pastebin.com/Re01zr4u –