1

我刚开始为 Android 编程。我正在为我的实习构建这个应用程序,但我有搜索功能。

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

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

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

    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();
        }
    }

}

公共类机场{

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;
}

}

公共类departmentPointFragment 扩展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;
    }

}
4

1 回答 1

1

花了一些时间来解决这个问题,但它就在这里:)

将此添加到您的适配器:

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);

然后在您的片段中,在 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;

希望对你有帮助,祝你好运!

于 2013-05-24T09:36:32.497 回答