目前,我正在尝试在Android Studio中创建一个移动应用程序,允许用户输入ISBN号,当点击搜索按钮时,它将搜索原始文件夹中的CSV并返回该ISBN查询的结果。此CSV文件包含与以逗号分隔的该号码相关联的所有其他信息。目前,它将CSV文件的所有结果返回到用户界面上的编辑文本字段中。如何更改我的代码只返回我想要的特定结果?
这是readFile类:
public class readFile {
InputStream inputStream;
public readFile(InputStream inputStream){
this.inputStream = inputStream;
}
public List<String[]> read(){
List<String[]> resultList = new ArrayList<String[]>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String csvLine;
while ((csvLine = reader.readLine()) != null) {
String[] row = csvLine.split(",");
resultList.add(row);
}
}
catch (IOException ex) {
throw new RuntimeException("Error in reading CSV file: "+ex);
}
finally {
try {
inputStream.close();
}
catch (IOException e) {
throw new RuntimeException("Error while closing input stream: "+e);
}
}
return resultList;
}
}
itemArrayAdapter类:
public class ItemArrayAdapter extends ArrayAdapter<String[]> {
private List<String[]> scoreList = new ArrayList<String[]>();
static class ItemViewHolder {
EditText title, publicationPlace,publicationDate,edition,author;
}
public ItemArrayAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
@Override
public void add(String[] object) {
scoreList.add(object);
super.add(object);
}
@Override
public int getCount() {
return this.scoreList.size();
}
@Override
public String[] getItem(int index) {
return this.scoreList.get(index);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ItemViewHolder viewHolder;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.item_layout, parent, false);
viewHolder = new ItemViewHolder();
viewHolder.title = (EditText) row.findViewById(R.id.title);
viewHolder.publicationDate = (EditText) row.findViewById(R.id.publicationDate);
viewHolder.publicationPlace = (EditText) row.findViewById(R.id.publicationPlace);
viewHolder.edition = (EditText) row.findViewById(R.id.edition);
viewHolder.author = (EditText) row.findViewById(R.id.authors);
row.setTag(viewHolder);
} else {
viewHolder = (ItemViewHolder)row.getTag();
}
String[] isbn = getItem(position);
viewHolder.title.setText(isbn[1]);
viewHolder.publicationDate.setText(isbn[2]);
viewHolder.publicationPlace.setText(isbn[3]);
viewHolder.edition.setText(isbn[4]);
viewHolder.author.setText(isbn[5]);
return row;
}
}
答案 0 :(得分:0)
[...代码...]
String[] row = csvLine.split(",");
// Add the check as shown here:
for ( int col = 0; col < row.length; col++ ) { // this could be avoided
if ( row[col].compareTo(ISBN_string) == 0 )
resultList.add(row);
}
// Maby if you know the column no. you can get away without the iteration
// resultList.add(row); <-- this is now obsolete
[...更多代码...]