我已将其设置为将csv中的项目显示在下拉列表中,但是当我点击手机上的列表时,它会显示[Ljava.lang.String;@427fa7co
或其他数字代码等值。
但是当我点击其中一个时,下拉列表的显示屏上会显示正确的名称。只是不在实际列表中。
主要活动:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Spinner;
public class create extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//opens csv
InputStream inputStream = getResources().openRawResource(R.raw.shopitems);
CSVFile csvFile = new CSVFile(inputStream);
List<String> itemList = csvFile.read();
MyListAdapter adapter=new MyListAdapter(this, R.layout.listrow,R.id.txtid, itemList);
Spinner spinner=(Spinner)findViewById(R.id.spinner);
spinner.setAdapter(adapter);
//ListView listView=(ListView)findViewById(R.id.listview);
//listView.setAdapter(adapter);
}
private class CSVFile {
InputStream inputStream;
public CSVFile(InputStream inputStream){
this.inputStream = inputStream;
}
public List<String> read(){
List<String> resultList = new ArrayList<String>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] row = line.split(",");
resultList.add(row[1]);
}
}
catch (IOException e) {
Log.e("Main",e.getMessage());
}
finally {
try {
inputStream.close();
}
catch (IOException e) {
Log.e("Main",e.getMessage());
}
}
return resultList;
}
}
}
Mylistadapter.java - 我认为问题可能就在这里
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.lang.String;
public class MyListAdapter extends ArrayAdapter<String[]> {
int groupid;
List<String> items;
Context context;
String path;
public MyListAdapter(Context context, int vg, int id, List<String> items) {
super(context, vg, id, (List<String[]>) items);
this.context = context;
groupid = vg;
this.items = items;
}
static class ViewHolder {
public TextView textid;
public TextView textname;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
{
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(groupid, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.textid = (TextView) rowView.findViewById(R.id.txtid);
viewHolder.textname = (TextView) rowView.findViewById(R.id.txtname);
rowView.setTag(viewHolder);
}
// Fill data in the drop down.
ViewHolder holder = (ViewHolder) rowView.getTag();
String row = items.get(position);
//holder.textid.setText(row[0]); prints aisle number, dont need
holder.textname.setText(row[1]);
return rowView;
}
}
}
答案 0 :(得分:1)
Spinner
使用ArrayAdapter.getDropDownView
代替ArrayAdapter.getView
iirc。签名是一样的,方法不同。改变
public View getView(int position, View convertView, ViewGroup parent) {
到
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
答案 1 :(得分:0)
问题实际上是,当您使List <>适应ListView时,List上的每个项目本身都是一个数组,因此,由于没有确切的文本要显示,因此会显示
[Ljava.lang.String;@427fa7co
证据?尝试执行:
Log.d(list[0].toString());
您应该得到完全相同的
[Ljava.lang.String;@427fa7co
解决方案:
最简单:如果您不需要数组列表(N x M),只需使用List<String>
中级: 尝试创建一个新的Object Class并创建一个String []属性,重写toString()方法以获取要显示的确切文本
专家:实际上并不比以前更难,但应该是最佳实践 创建一个新的Object Class并设置您实际需要的属性(例如ID,名称,地址等)。覆盖toString()方法以获取要显示的确切文本的示例
@Override
public string toString()
{
return this.name;
}