在android Listview中的onTextChanged之后没有搜索结果

时间:2016-04-18 10:29:46

标签: android listview

我无法解决此问题。我想通过EditText添加搜索功能。键入字母后结果应该动态显示,但现在没有结果。

感谢您的快速回复:)

ParkingActivity.java

public class ParkingActivity extends FragmentActivity {

private final String URL_TO_HIT = "http://www-users.mat.umk.pl/~discordia/dane.json";
private ListView lvParking;
private EditText inputSearch;
public ParkingAdapter adapter;
public ArrayList<ParkingModel> parkingModelList = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_activity);

    lvParking = (ListView) findViewById(R.id.lvParking);
    inputSearch = (EditText) findViewById(R.id.editText);
    new JSONTask().execute(URL_TO_HIT);

}


public class JSONTask extends AsyncTask<String,String, List<ParkingModel> > {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected List<ParkingModel> doInBackground(String... params) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();
            String line ="";
            while ((line = reader.readLine()) != null){
                buffer.append(line);
            }

            String finalJson = buffer.toString();

            JSONObject parentObject = new JSONObject(finalJson);
            JSONArray parentArray = parentObject.getJSONArray("parkingInfo");

            ArrayList<ParkingModel> parkingModelList = new ArrayList<>();

            Gson gson = new Gson();
            for(int i=0; i<parentArray.length(); i++) {
                JSONObject finalObject = parentArray.getJSONObject(i);
                /**
                 * below single line of code from Gson saves you from writing the json parsing yourself which is commented below
                 */
                ParkingModel parkingModel = gson.fromJson(finalObject.toString(), ParkingModel.class);

                parkingModelList.add(parkingModel);

            }
            return parkingModelList;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if(connection != null) {
                connection.disconnect();
            }
            try {
                if(reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return  null;
    }

    @Override
    protected void onPostExecute(final List<ParkingModel> result) {
        super.onPostExecute(result);

        if(result != null) {
            adapter = new ParkingAdapter(getApplicationContext(), R.layout.row, result);
            lvParking.setAdapter(adapter);
            inputSearch.addTextChangedListener(new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                    // When user changed the Text
                    ParkingActivity.this.adapter.getFilter().filter(cs);
                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                              int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub
                }
            });
            lvParking.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    ParkingModel parkingModel = result.get(position);
                    Intent intent = new Intent(ParkingActivity.this, DetailActivity.class);
                    intent.putExtra("parkingModel", new Gson().toJson(parkingModel));
                    startActivity(intent);
                }
            });

        }
    }

}

public class ParkingAdapter extends ArrayAdapter {

    private List<ParkingModel> parkingModelList;
    private int resource;
    private LayoutInflater inflater;
    public ParkingAdapter(Context context, int resource, List<ParkingModel> objects) {
        super(context, resource, objects);
        parkingModelList = objects;
        this.resource = resource;
        inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    }

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

        ViewHolder holder = null;

        if(convertView == null){
            holder = new ViewHolder();
            convertView = inflater.inflate(resource, null);
            holder.tvLokalizacja = (TextView)convertView.findViewById(R.id.tvLokalizacja);
            holder.tvIle_wolnych_zwyklych = (TextView)convertView.findViewById(R.id.tvIle_wolnych_zwyklych);
            holder.tvIle_wszystkich_zwyklych = (TextView)convertView.findViewById(R.id.tvIle_wszystkich_zwyklych);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvLokalizacja.setText(parkingModelList.get(position).getLokalizacja());
        holder.tvIle_wolnych_zwyklych.setText("Wolne: " + parkingModelList.get(position).getIle_wolnych_zwyklych());
        holder.tvIle_wszystkich_zwyklych.setText("Wszystkie:" + parkingModelList.get(position).getIle_wszystkich_zwyklych());


        return convertView;
    }


    class ViewHolder{

        private TextView tvLokalizacja;
        private TextView tvIle_wolnych_zwyklych;
        private TextView tvIle_wszystkich_zwyklych;

    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_parking, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    return super.onOptionsItemSelected(item);
}
}

这是我用来创建带数据的最终数组的模型。

ParkingModel.java

public class ParkingModel {

private String lokalizacja;
private int ile_wolnych_zwyklych;
private int ile_wszystkich_zwyklych;

public String getLokalizacja() {
    return lokalizacja;
}

public void setLokalizacja(String lokalizacja) {
    this.lokalizacja = lokalizacja;
}

public int getIle_wolnych_zwyklych() {
    return ile_wolnych_zwyklych;
}

public void setIle_wolnych_zwyklych(int ile_wolnych_zwyklych) {
    this.ile_wolnych_zwyklych = ile_wolnych_zwyklych;
}

public int getIle_wszystkich_zwyklych() {
    return ile_wszystkich_zwyklych;
}

public void setIle_wszystkich_zwyklych(int ile_wszystkich_zwyklych) {
    this.ile_wszystkich_zwyklych = ile_wszystkich_zwyklych;
}
}

这是我的xml文件。

list_activity.xml

<LinearLayout 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:orientation="vertical"
tools:context=".ParkingActivity">


<EditText
    android:layout_width="319dp"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:inputType="text"
    android:layout_gravity="center_horizontal" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lvParking" />
</LinearLayout>

2 个答案:

答案 0 :(得分:0)

因为在onPostExecute的{​​{1}}中您创建了JSONTask类及其下方的新对象

ParkingAdapter

这总是为空;

设置适配器只需使用

ParkingActivity.this.adapter.getFilter().filter(cs); you are using this line. 

答案 1 :(得分:0)

更改此行

ParkingAdapter adapter = new ParkingAdapter(getApplicationContext(), R.layout.row, result);

到此

adapter = new ParkingAdapter(getApplicationContext(), R.layout.row, result);