gpt4 book ai didi

java - 字符串数组列表未正确传递到另一个类的数组列表以显示在下拉列表中

转载 作者:行者123 更新时间:2023-12-01 14:39:22 25 4
gpt4 key购买 nike

大家好,第一次遇到堆栈溢出问题,我遇到了一个问题。我从服务器获取字符串列表并将其传递到字符串数组列表中,一切看起来都很好,直到我将它传递给另一个类作为我的 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;
}

最佳答案

1.在此更改代码

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

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

2.另一种方式

        CategoryItem temp = new CategoryItem();
temp.SETcategorystring(line);
categories.add(temp.getcategoryString().toString());

关于java - 字符串数组列表未正确传递到另一个类的数组列表以显示在下拉列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16141836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com