2016-07-15 72 views
1

所以我有一個字符串數組,我有。我正在嘗試將每個單個項目從該字符串數組傳遞給自定義適配器。我無法弄清楚如何使用我在自定義適配器中傳遞的字符串?使用帶有自定義適配器的字符串數組來使用listview?

的String []我通過

String favorites = String.valueOf(FavList.get(0).get("favorites_list")); 

       String[] separated = favorites.split(","); 

       for (String s: separated) { 
        //Do your stuff here 
        FavoritesAdapter.add(s); 
       } 

Adapter.class

public class FavoritesAdapter extends ArrayAdapter<favoriteList> { 
private final Context mContext; 
private List<favoriteList> favlist; 
private TextView favorites; 
private TextView favDetail; 

public FavoritesAdapter(Context context, ArrayList<favoriteList> objects) { 
    super(context, R.layout.favorites_listview_single, objects); 
    this.mContext = context; 
    this.favlist = objects; 
} 


public View getView(final int position, View convertView, final ViewGroup parent) { 
    if (convertView == null) { 
     LayoutInflater mLayoutInflater = LayoutInflater.from(mContext); 
     convertView = mLayoutInflater.inflate(R.layout.favorites_listview_single, null); 
    } 


    // Here is where i cant figure out how to get the string that I passed. 






    return convertView; 
} 

}

+0

爲您的數據創建一個ArrayList,然後將它傳遞給您的適配器。沒有看到你的適配器代碼,很難知道 – Eenvincible

+0

對不起,忘了添加,剛剛添加。 – AndroidDev21921

回答

1

這似乎像你只有經過字符串,所以我不知道爲什麼你使用的是ArrayAdapter<favoriteList>

如果您更改適配器類這樣的:

public class FavoritesAdapter extends ArrayAdapter<String> { 
    private final Context mContext; 
    private ArrayList<String> favlist; 
    private TextView favorites; 
    private TextView favDetail; 

    public FavoritesAdapter(Context context, ArrayList<String> objects) { 
     super(context, R.layout.favorites_listview_single, objects); 
     this.mContext = context; 
     this.favlist = objects; 
    } 

    public View getView(final int position, View convertView, final ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater mLayoutInflater = LayoutInflater.from(mContext); 
      convertView = mLayoutInflater.inflate(R.layout.favorites_listview_single, null); 
     } 

     String favoriteItem = favlist.get(position) //get the string you passed 


     return convertView; 
    } 

    //...more code 
} 

然後,當你把這個字符串,通過它就像這樣:

String favorites = String.valueOf(FavList.get(0).get("favorites_list")); 
ArrayList<String> favlist = (ArrayList<String>)Arrays.asList(favorites.split(",")); 

FavoritesAdapter adapter = new FavoritesAdapter(getApplicationContext(), favlist); 
listView.setAdapter(adapter); //where listView is the view you declared previously 
+0

你缺少像favoriteIte.findViewById(R.id.someTextView).setTextView(favoriteItem); – piotrpo

+0

op沒有指定他想要對字符串做什麼,所以我只演示瞭如何傳遞它並將其存儲在變量中 – Bill

+0

謝謝quidproquo!這工作對我來說,但我唯一需要添加的是'new'arrayList的聲明。如下所示: ArrayList favlist = new ArrayList <>(Arrays.asList(favorites.split(「,」))); – AndroidDev21921

相關問題