検索機能を追加したいです。チュートリアルを見ました。コードを追加しようとしましたが、まだエラーがあります。おそらく、アダプターを理解していません。public class orderActivity は ListActivity を拡張します{
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> ordersList;
EditText inputsearch;
// List view
private ListView bb;
// Listview Adapter
ArrayAdapter<String> adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_order);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
username = i.getStringExtra(KEY_NAME);
// Hashmap for ListView
ordersList = new ArrayList<HashMap<String, String>>();
// Loading orders in Background Thread
new LoadAllOrders().execute();
// Get listview
ListView lv = getListView();
// on seleting single product
// launching Edit Product Screen
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String oid = ((TextView) view.findViewById(R.id.name)).getText()
.toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(),
orderdetailActivity.class);
// sending pid to next activity
in.putExtra(KEY_ID, oid);
// starting new activity and expecting some response back
startActivityForResult(in, 100);
}
});
//the search function part!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
bb = (ListView) findViewById(android.R.id.list);
inputsearch = (EditText) findViewById(R.id.inputsearch);
bb.setAdapter(adapter);
/**
* Enabling Search Filter
* */
inputsearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
orderActivity.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
}
});
}
/**
* getting All orders from url
* */
protected String doInBackground(String... args) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_orders, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Orders: ", json.toString());
// Checking for SUCCESS TAG
int success = json.getInt(KEY_SUCCESS);
if (success == 1) {
// orders found
// Getting Array of orders
orders = json.getJSONArray(KEY_ORDERS);
// looping through All Orders
for (int i = 0; i < orders.length(); i++) {
JSONObject c = orders.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(KEY_ID);
String name = c.getString(KEY_USERNAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(KEY_ID, id);
map.put(KEY_USERNAME, name);
// adding HashList to ArrayList
ordersList.add(map);
}
} else {
// no orders found
// Launch Add New product Activity
Intent i = new Intent(getApplicationContext(),
orderActivity.class); // no ensure to need change
// Closing all previous activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all orders
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
orderActivity.this, ordersList,
R.layout.all_list, new String[] { KEY_USERNAME,
KEY_ID},
new int[] { R.id.oid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
レイアウトがあります。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="@+id/oid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
</LinearLayout>
別のレイアウトがあります
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dip"
android:text="Please enter the order number:"
android:textSize="20dp" />
<EditText
android:id="@+id/inputsearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:hint="Search orders.."
android:inputType="textVisiblePassword"
android:ems="10" />
<!-- Always give id value as list(@android:id/list) -->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
それは私のログです
03-12 09:52:00.252: W/dalvikvm(295): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
03-12 09:52:00.261: E/AndroidRuntime(295): FATAL EXCEPTION: main
03-12 09:52:00.261: E/AndroidRuntime(295): java.lang.NullPointerException
03-12 09:52:00.261: E/AndroidRuntime(295): at com.example.androidhive.orderActivity$2.onTextChanged(orderActivity.java:125)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.widget.TextView.sendOnTextChanged(TextView.java:6131)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.widget.TextView.handleTextChanged(TextView.java:6172)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:6316)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.text.SpannableStringBuilder.sendTextChange(SpannableStringBuilder.java:889)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:352)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.text.SpannableStringBuilder.change(SpannableStringBuilder.java:269)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:432)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:409)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:28)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:583)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:174)
03-12 09:52:00.261: E/AndroidRuntime(295): at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:120)
03-12 09:52:00.261: E/AndroidRuntime(295): at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:247)
03-12 09:52:00.261: E/AndroidRuntime(295): at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:73)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.os.Looper.loop(Looper.java:123)
03-12 09:52:00.261: E/AndroidRuntime(295): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-12 09:52:00.261: E/AndroidRuntime(295): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 09:52:00.261: E/AndroidRuntime(295): at java.lang.reflect.Method.invoke(Method.java:521)
03-12 09:52:00.261: E/AndroidRuntime(295): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-12 09:52:00.261: E/AndroidRuntime(295): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-12 09:52:00.261: E/AndroidRuntime(295): at dalvik.system.NativeStart.main(Native Method)
03-12 09:52:02.294: I/Process(295): Sending signal. PID: 295 SIG: 9