我有一个自定义arraylist的活动,可以容纳5600个项目。 arraylist引用输入的.csv文件,并链接到列表视图以供显示。我遇到的问题是为该列表视图添加搜索功能,以便用户可以在需要更具体的内容时缩小搜索范围。我已经尝试了很多其他教程,但似乎没有任何东西适合我的确切场景。我很感激帮助,希望我能正确地发布这个问题。
这是listview的java类
package com.example.android.practiceapp;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class games extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_games);
InputStream inputStream = getResources().openRawResource(R.raw.slots);
CSVFile csvFile = new CSVFile(inputStream);
List<String[]> slots = csvFile.read();
MyListAdapter adapter=new MyListAdapter(this, R.layout.list_item,R.id.game_title, slots);
ListView listView=(ListView)findViewById(R.id.list);
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);
}
}
catch (IOException e) {
Log.e("Main",e.getMessage());
}
finally {
try {
inputStream.close();
}
catch (IOException e) {
Log.e("Main",e.getMessage());
}
}
return resultList;
}
}
}
如果需要,ListViewAapter
package com.example.android.practiceapp;
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.util.List;
/**
* Created by swvki on 7/15/2017.
*/
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, items);
this.context=context;
groupid=vg;
this.items=items;
}
static class ViewHolder {
public TextView textid;
public TextView textname;
public TextView denom;
public TextView tech;
}
public View getView(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.game_title);
viewHolder.textname = (TextView) rowView.findViewById(R.id.location);
viewHolder.denom = (TextView) rowView.findViewById(R.id.denomination);
viewHolder.tech = (TextView) rowView.findViewById(R.id.manufacterer);
rowView.setTag(viewHolder);
}
// Fill data
ViewHolder holder = (ViewHolder) rowView.getTag();
String[] row=items.get(position);
holder.textid.setText(row[0]);
holder.textname.setText(row[1]);
holder.denom.setText(row[2]);
holder.tech.setText(row[3]);
return rowView;
}
}
这是XML
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list"
android:background="@drawable/background"
android:orientation="vertical"
android:padding="16dp"
tools:context="com.example.android.practiceapp.games">
</ListView>