2012-09-09 60 views
-1

我想從我的MYDB表中獲取數據,我想展現給alertdialog數據,這裏是我的代碼:如何獲得字符串的CharSequence

public void gotResult(String result, String message) { 
     // TODO Auto-generated method stub 
     if (result != null) { 
      switch (Action) { 
      case VIEW_BIKE_TYPE: 
       try { 
        JSONObject jsonObject = null; 
        jsonObject = new JSONObject(result); 
        int response = jsonObject.getInt("result"); 
        if (response != 0) { 
         JSONArray jsonArray = new JSONArray(
           jsonObject.getString("data")); 


         CharSequence[] items = new CharSequence[jsonArray.length()]; 

         for (int i = 0; i < jsonArray.length(); i++) { 
          JSONObject biketype = new JSONObject(jsonArray 
            .get(i).toString()); 


          items[i] = biketype.getString("type_name"); 
         } 
         bike_type_Result.gotResult(items, null, Action); 
        } else { 
         String error_message = jsonObject 
           .getString("message"); 
         bike_type_Result.gotResult(null, error_message, 
           Action); 
        } 
       } catch (JSONException e) { 
        // TODO: handle exception 
        bike_type_Result.gotResult(null, 
          "Failed parsing data from database. Please try again.. " 
            + e.getMessage(), Action); 
        Log.e("CON ERROR", e.getMessage()); 
       } 
      } 
     } else { 
      bike_type_Result.gotResult(null, message, Action); 
      Log.e("CON ERROR", message); 
     } 
    } 
}; 

和我有一個這樣的代碼:

protected void onListItemClick(ListView l, View v, int position, long id) { 

    final Entity_Brand brand = adapterBrand.getItem(position); 

    CharSequence[] items = { ...... }; 

    AlertDialog.Builder builder = new AlertDialog.Builder(
      Tab_Brand_ListView_Activity.this); 
    builder.setIcon(R.drawable.alert_brand_logo); 
    builder.setTitle(brand.getBrand_name()); 

這是我的代碼,有人請給我看一下代碼CharSequence[] items = { ...... };,所以我可以從myDB獲取數據? 請幫助我。謝謝

回答

0

只需將您代碼中出現的所有CharSequence替換爲String即可。

+0

我想使用charsequence將我的數據庫中已存在的數據顯示爲警報,但我不知道如何將數據調用到警報中。你會幫我嗎? –

0

將字符串數組轉換爲像這樣的charsequnce並得到想要的字符串 List listItems = new ArrayList();

listItems.add("Take Photo"); 
    listItems.add("Choose From Gallery"); 
    if (flagImage == 1) { 
     listItems.add("Remove Photo"); 

    } 
    listItems.add("Cancel"); 

    final CharSequence[] items = listItems.toArray(new CharSequence[listItems.size()]);