0

大家好,第一次堆栈溢出,我遇到了问题。我从服务器获取字符串列表并将其传递给字符串数组列表,一切看起来都很好,直到我将它传递给另一个类作为我的 spinadapter 然后我得到它在数组中位置的代码版本,而不是我的细绳。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category_select);

    ArrayList < String > categories = new ArrayList < String > ();
    PopulateSpinner(categories);
    Log.d(TAG, "before spin adapter");
    adapter = new SpinAdapter(this, android.R.layout.simple_spinner_item, categories);
}


private void PopulateSpinner(ArrayList < String > categories) {
    BufferedReader in = null;
    Log.d(TAG, "bufferReader");
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        Log.d(TAG, "request");
        String Url = "http://www.youcode.ca/Lab02Servlet";
        Log.d(TAG, Url);
        Log.d(TAG, "Url");
        request.setURI(new URI(Url.toString() + "?Service=categories"));
        Log.d(TAG, "URI");
        HttpResponse response = client.execute(request);
        Log.d(TAG, "response"); in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        Log.d(TAG, "in");
        String line = "";
        while ((line = in .readLine()) != null) {
            Log.d(TAG, line);
            CategoryItem temp = new CategoryItem();
            temp.SETcategorystring(line);
            categories.add(temp.toString());
        }
        /*for (String string : categories)  test the aray to see if it has the content
        {
            Log.d(TAG, string.toString());
        }*/
        Log.d(TAG, "after the while");
    }


    private Context context;
    private ArrayList < String > jitters;
    private static final String TAG = "SpinAdapterActivity";
    public SpinAdapter(Context context, int textViewResourceId, ArrayList < String > categories) {
        super(context, textViewResourceId, categories);
        this.context = context;
        this.jitters = categories;
    }@
    Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        CategorySelect activity = (CategorySelect) context;
        LayoutInflater inflater = activity.getLayoutInflater();

        View spinnerRow = inflater.inflate(R.layout.category_textview, null);
        TextView line = (TextView) spinnerRow.findViewById(R.id.Category_textview_id);

        line.setText((String)(jitters.get(position)));

        return spinnerRow;
    }
4

1 回答 1

1

1.这里修改代码

直接将字符串值添加到列表中。

   categories.add(temp.toString()); change to categories.add(line); 

2.另一种方式

        CategoryItem temp = new CategoryItem();
        temp.SETcategorystring(line);
        categories.add(temp.getcategoryString().toString());
于 2013-04-22T08:13:49.803 回答