リストを生成する 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);
}
}
で NPE が発生するのはなぜonTextChanged
ですか? 考えやアイデアはありますか?私はこれを完全に間違っていますか?
編集:
これはうまくいきました:配列情報を返すメソッドを作成し、次のアダプターで呼び出します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;
}