2012-08-31 105 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 –