过滤列表视图获取Null onTextChanged

时间:2013-06-02 22:40:49

标签: android listview android-listview android-edittext

我有一个填充列表的XML解析器。这工作得很好。我正在尝试通过EditText向我的应用添加过滤器,但我在NullPointerException中获得onTextChange

public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        adapter.getFilter().filter(s);
    }

我的全班同学如下:

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

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.util.AQUtility;
import com.androidquery.util.XmlDom;

public class MainActivity extends ListActivity implements TextWatcher {
    private AQuery aq;
    ProgressDialog dialog;
    String etValue;
    EditText searchText;
    TextView result;
    String url = "http://domain.com/it.xml";
    ListView lv;
    String Name;
    String barcode;
    String productName;
    String qtySold;
    String inventory;
    ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ProgressDialog dialog = new ProgressDialog(this);
        dialog.setIndeterminate(false);
        dialog.setInverseBackgroundForced(false);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setTitle("Help on the Way...");
        dialog.setMessage("Gathering Latest Inventory Info");
        aq = new AQuery(this);
        AQUtility.setDebug(true);       
        // load the file from the server
        load_xml();
    }

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

    public void load_xml() {
        aq.progress(dialog).ajax(url, XmlDom.class, -1, this, "inventoryCb");
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public void inventoryCb(String url, XmlDom xml, AjaxStatus status) {
        // Log.i("CB", xml.toString());
        List<XmlDom> sec = xml.tags("GroupFooter");
        List<String> fv = new ArrayList<String>();

        for (XmlDom entry : sec) {
            List<XmlDom> ent = entry.tags("Field");
            for (XmlDom field : ent) {
                Name = field.attr("Name");
                // Log.v("Field Name: ", Name);
                if (Name.equals("Field19")) {
                    barcode = field.child("FormattedValue").text();
                    // Log.i("Barcode: ", barcode);

                }
                if (Name.equals("Field20")) {
                    productName = field.child("FormattedValue").text();
                    // Log.d("Product Name: ", productName);

                }
                if (Name.equals("Field21")) {
                    qtySold = field.child("FormattedValue").text();
                    // Log.e("Qty Sold: ", qtySold);
                }

            }
            inventory = barcode + " | " + productName + " | " + qtySold;
            fv.add(inventory);
            Log.i("INVENTORY: ", inventory);
        }

        searchText = (EditText) findViewById(R.id.inputSearch);
        searchText.addTextChangedListener(this);

        setListAdapter(new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, fv));

    }

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub
        adapter.getFilter().filter(s);
    }
}

为什么我在onTextChanged上获得NPE?有什么想法或想法吗?我这样做完全错了吗?


编辑:

这最终起作用:创建一个返回数组信息的方法,然后在onCreate中的适配器中调用它:

final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, xmlr());
        setListAdapter(adapter);
        EditText filterEditText = (EditText) findViewById(R.id.filterText);
        filterEditText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                    int count) {
                adapter.getFilter().filter(s.toString());
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

// LOAD XML from Local Resources Folder
    public List<String> xmlr() {

        InputStream is = getResources().openRawResource(R.raw.it);
        XmlDom xml = null;
        try {
            xml = new XmlDom(is);
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            Log.e("SAXe: ", e.getMessage());
        }
        List<XmlDom> sec = xml.tags("GroupFooter");
        fv = new ArrayList<String>();

        for (XmlDom entry : sec) {
            List<XmlDom> ent = entry.tags("Field");
            for (XmlDom field : ent) {
                Name = field.attr("Name");
                // Log.v("Field Name: ", Name);
                if (Name.equals("Field19")) {
                    barcode = field.child("FormattedValue").text();
                    // Log.i("Barcode: ", barcode);

                }
                if (Name.equals("Field20")) {
                    productName = field.child("FormattedValue").text();
                    // Log.d("Product Name: ", productName);

                }
                if (Name.equals("Field21")) {
                    qtySold = field.child("FormattedValue").text();
                    // Log.e("Qty Sold: ", qtySold);
                }

            }
            inventory = barcode + " | " + productName + " | " + qtySold;
            fv.add(inventory);
            Log.i("INVENTORY: ", inventory);
        }
        return fv;
    }

1 个答案:

答案 0 :(得分:2)

setListAdapter(new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, fv));

将其设置为OnCreate Method ..