android - 搜索 View CSV 数组列表

标签 android android-listview android-listfragment

我刚开始为 Android 编程。 我正在为实习构建此应用程序,但我对搜索功能很熟悉。

我有一个 CVS 文件,我在 ArrayList 中设置了值,为此我构建了一个 CSV 适配器并在我的 fragment 中调用这个适配器。现在一切正常我得到了我想要的所有值的列表,问题是列表包含 1000 条记录。这就是为什么我要实现一个搜索 View ,以便用户可以搜索所需的值。

现在我希望当用户选择搜索并开始输入时,Arrylist 会被搜索并开始过滤列表中的可能选项。这样,当显示期望值时,用户可以选择这个。

我已经尝试这样做 3 天了,我知道我必须在 onQueryTextChange 和 onQueryTextsubmit 中做一些事情。但到目前为止没有运气:( 有人可以帮我解决这个问题吗?我将不胜感激。提前发送。

    public class CSVAdapter extends ArrayAdapter<airports> {
     Context ctx;

    public CSVAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);

        //Store a reference to the Context so we can use it to load a file from Assets.
        this.ctx = context;

        //Load the data.
        loadArrayFromFile();

}

    @Override
    public View getView(final int pos, View convertView, final ViewGroup parent){

        RelativeLayout row = (RelativeLayout)convertView;
        if(null == row){
            //No recycled View, we have to inflate one.
            LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = (RelativeLayout)inflater.inflate(R.layout.departure_point_fragment, null);
        }

        TextView anameTxt = (TextView)row.findViewById(R.id.airport_name);
        TextView acityTxt = (TextView)row.findViewById(R.id.airport_city);
        TextView acountryTxt = (TextView)row.findViewById(R.id.airport_country);
        TextView icaoTxt = (TextView)row.findViewById(R.id.airport_code);

        anameTxt.setText(getItem(pos).getAname());
        acityTxt.setText(getItem(pos).getAcity());
        acountryTxt.setText(getItem(pos).getAcountry());
        icaoTxt.setText(getItem(pos).getIcao());

        return row;
        }

    private void loadArrayFromFile(){
        try {
            // Get input stream and Buffered Reader for our data file.
            InputStream is = ctx.getAssets().open("airports.csv"); 
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;

            //Read each line
            while ((line = reader.readLine()) != null) {

                //Split to separate the name from the capital
                String[] RowData = line.split(",");

                //Create a State object for this row's data.
                airports cur = new airports();
                cur.setAname(RowData[0]);
                cur.setAcity(RowData[1]);
                cur.setAcountry(RowData[2]);
                cur.setIcao(RowData[3]);
                cur.setLat(RowData[4]);
                cur.setLon(RowData[5]);
                cur.setAltitude(RowData[6]);
                cur.setTimezone(RowData[7]);
                cur.setDst(RowData[8]);

                //Add the State object to the ArrayList (in this case we are the ArrayList).
                this.add(cur);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

公共(public)舱机场{

private String aname;
private String acity;
private String acountry;
private String icao;
private String lat;
private String lon;
private String altitude;
private String timezone;
private String dst;

public String getAname() {
    return aname;
}
public void setAname(String aname) {
    this.aname = aname;
}
public String getAcity() {
    return acity;
}
public void setAcity(String acity) {
    this.acity = acity;
}
public String getAcountry() {
    return acountry;
}
public void setAcountry(String acountry) {
    this.acountry = acountry;
}
public String getIcao() {
    return icao;
}
public void setIcao(String icao) {
    this.icao = icao;
}
public String getLat() {
    return lat;
}
public void setLat(String lat) {
    this.lat = lat;
}
public String getLon() {
    return lon;
}
public void setLon(String lon) {
    this.lon = lon;
}
public String getAltitude() {
    return altitude;
}
public void setAltitude(String altitude) {
    this.altitude = altitude;
}
public String getTimezone() {
    return timezone;
}
public void setTimezone(String timezone) {
    this.timezone = timezone;
}
public String getDst() {
    return dst;
}
public void setDst(String dst) {
    this.dst = dst;
}

公共(public)类 departurePointFragment 扩展 SherlockListFragment 实现 SearchView.OnQueryTextListener{ 私有(private) CSVAdapter mAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.listview, container, false);

    return view;

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);                        getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSherlockActivity().getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);  
        setHasOptionsMenu(true);


        mAdapter =new CSVAdapter(getActivity(), -1);

        setListAdapter(mAdapter);
        getListView();

        setRetainInstance(true);


    }


    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

        inflater.inflate(R.menu.searching, menu);
        MenuItem item = menu.findItem(R.id.menu_search);
        SearchView itemview = (SearchView) item.getActionView();

        // Execute this when searching
        itemview.setOnQueryTextListener(this);

        super.onCreateOptionsMenu(menu, inflater);



       Log.d("Nicola", "2");

    }





    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onQueryTextChange(String query) {

        Log.d("Nicola", "100");


        return true;
    }

}

最佳答案

花了一些时间才弄明白,但就是这样:)

将此添加到您的适配器:

ArrayList<airports> airportsArray = new ArrayList<airports>();
public ArrayList<airports> getAirportsArray()
{
    return airportsArray;
}

(您可以右键单击 ArrayList 声明,选择 Source->Generate Getters and Setters)

读取 CSV 文件后,您可以将这些对象添加到新创建的 ArrayList,更改:

this.add(cur);

this.add(cur);
airportsArray.add(cur);

然后在您的 fragment 中,在 onQueryTextChange 方法中,执行以下操作:

this.mAdapter.clear(); // This clears the existing list

// Loop through the airports
for (airports item : mAdapter.getAirportsArray())
{
  // Does the name contains what you are searching for?
  // You can add more criteria here using the || (OR) operator
  if (item.getAname().contains(query))
  {
    // If so, add it
    mAdapter.add(item);
  }
}

mAdapter.notifyDataSetChanged(); // Notify the adapter that the dataset changed
return true;

希望对您有所帮助,祝您好运!

关于android - 搜索 View CSV 数组列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16606610/

相关文章:

android - 您如何确保将支持库项目(例如 android-support-v7-appcompat)的资源(例如样式)添加到您的项目中?

位于操作栏/工具栏上方的 Android 溢出菜单?

android - 在 ListView 中存储 CheckBox 状态

android - 创建自定义简单游标适配器

java - 如何分别读取android sim联系人和手机联系人

java - Android Studio 外部 jar 错误

Android:Facebook 菜单在列表滚动中显示和 Conceal

android - Listview 未包装的弹出窗口

java - FilterListView - 从列表中删除不以提供的前缀开头的项目

android - fragment 之间的通信