Parse.com:如何在Fragment内部为Parse ListView添加搜索过滤器

时间:2017-01-31 12:14:48

标签: android android-fragments parse-platform android-search android-filter

我正在尝试为标签片段内的ListView添加搜索过滤器。从解析服务器使用和适配器调用数据。

我的片段java文件位于

之下

SportsCar.java

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import java.util.ArrayList;
import java.util.List;


public class SportsCar extends android.support.v4.app.ListFragment
        implements FindCallback<ParseObject> {
    private List<ParseObject> mCar = new ArrayList<ParseObject>();

    ArrayAdapter<String> adapter;
    EditText inputSearch;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_main, container, false);
    }
    @Override
    public void onViewCreated(View view, Bundle b) {
        super.onViewCreated(view, b);
        CarAdapter adaptor = new CarAdapter(getActivity(), mCar);
        setListAdapter(adaptor);
        ParseQuery.getQuery("oldcar").findInBackground(this);
    }
    @Override
    public void done (List < ParseObject > scoreList, ParseException e){
        if (e == null) {
            Log.d("score", "Retrieved " + scoreList.size() + " oldcar");
            mCar.clear();
            mCar.addAll(scoreList);
            ((CarAdapter) getListAdapter()).notifyDataSetChanged();
        } else {
            Log.d("score", "Error: " + e.getMessage());
        }

        inputSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                // When user changed the Text
                SportsCar.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
            }
        });
    }
}

这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingRight="0dp">


    <TextView android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="335dp"
        android:text="No data"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="56dp"
        android:layout_toLeftOf="@+id/android:list"
        android:layout_toStartOf="@+id/android:list" />


    <ListView android:id="@id/android:list"
        android:layout_width="263dp"
        android:layout_height="241dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_below="@+id/searchUserTextField" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentTop="true"
        android:layout_toEndOf="@+id/android:empty"
        android:id="@+id/searchUserTextField"
        android:background="@android:color/holo_red_dark"
        android:textColor="@android:color/white"
        android:layout_toRightOf="@+id/android:empty"
        android:layout_marginTop="105dp"
        android:textAllCaps="true"
        android:textAlignment="center"
        android:paddingTop="5dp"
        android:textSize="14sp"
        tools:text="Search by Name or Company" />


</RelativeLayout>

0 个答案:

没有答案