カスタム アダプターを使用している ListView に検索機能を実装しようとしています。私はこの例に従っています: http://justcallmebrian.com/?p=149。コードを編集してアダプターに入れることができました。しかし、それは結果をもたらしません。私のコードを見て、私が見逃したものを教えてもらえますか、それとも何か他にできることはありますか?
リストビューは次のとおりです。
public class DataView extends Activity implements OnItemClickListener {
private static final String rssFeed = "http://leobee.com/android/push/login/schedule.php";
private static final String AddEvent = "http://leobee.com/android/push/login/deleteUpdate.php";
private static final String ARRAY_NAME = "events";
private static final String EVENT_NAME = "event_name";
private static final String EVENT_ID = "event_id";
private static final String EVENT_DATE = "event_date";
private static final String EVENT_START = "event_start";
private static final String EVENT_END = "event_end";
private static final String EVENT_LOCATION = "event_location";
private static final String EVENT_DELETE_FLAG = "event_delete_flag";
List<Item> arrayOfList;
List<String> sortOfList;
SimpleAdapter simpleadapter;
ListView listView;
NewsRowAdapter objAdapter;
List<Integer> deleteList;
String undelete;
String eventNameDeleted;
String eventIdDeleted;
String eventDateDeleted;
String eventStartDeleted;
String eventEndDeleted;
String eventLocationDeleted;
JSONObject json;
String addBack = "0";
Button btnMessage;
Button btnLogout;
UserFunctions userFunctions;
EditText searchBox;
int textlength = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedulelistview);
userFunctions = new UserFunctions();
if (userFunctions.isUserLoggedIn(getApplicationContext())) {
listView = (ListView) findViewById(R.id.listview);
listView.isTextFilterEnabled();
listView.setOnItemClickListener(this);
deleteList = new ArrayList<Integer>();
btnLogout = (Button) findViewById(R.id.btnLogout);
btnMessage = (Button) findViewById(R.id.btnMessage);
searchBox = (EditText) findViewById(R.id.EditText01);
arrayOfList = new ArrayList<Item>();
if (Utils.isNetworkAvailable(DataView.this)) {
new MyTask().execute(rssFeed);
} else {
showToast("No Network Connection. Application is unable to load data.");
}
} else {
}
}
class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(DataView.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
return Utils.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject mainJson = new JSONObject(result);
JSONArray jsonArray = mainJson.getJSONArray(ARRAY_NAME);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject objJson = jsonArray.getJSONObject(i);
Item objItem = new Item();
// here objItem is reciveing and processing data from
// JSON string text
objItem.setId(objJson.getInt(EVENT_ID));// names from
objItem.setName(objJson.getString(EVENT_NAME));
objItem.setDate(objJson.getString(EVENT_DATE));
objItem.setDeleteFlag(objJson.getInt(EVENT_DELETE_FLAG));
arrayOfList.add(objItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < arrayOfList.size(); i++) {
Item deletflagCheck = arrayOfList.get(i);
if (deletflagCheck.getDeleteFlag() == 1) {
deleteList.add(deletflagCheck.getId());
} else {
}
}
}
setAdapterToListview();
}
}
@Override
public void onItemClick(AdapterView<?> parent, View viewDel, int position,
long id) {
Item deletflagCheck = arrayOfList.get(position);
if (deletflagCheck.getDeleteFlag() == 1) {
showDeleteDialog(position);
} else {
Intent intent = new Intent(DataView.this.getApplicationContext(),
UpdateServer.class);
for (int i = 0; i < 1; i++) {
Item item = arrayOfList.get(position);
intent.putExtra("id", item.getId());
intent.putExtra("name", item.getName());
intent.putExtra("date", item.getDate());
intent.putExtra("startTime", item.getStartTime());
intent.putExtra("endTime", item.getEndTime());
intent.putExtra("location", item.getLocation());
intent.putExtra("deleteFlag", item.getDeleteFlag());
}
startActivity(intent);
}
}
public void setAdapterToListview() {
objAdapter = new NewsRowAdapter(DataView.this,
R.layout.schedulelistrow, arrayOfList, deleteList);
objAdapter.notifyDataSetChanged();
listView.setAdapter(objAdapter);
searchBox.addTextChangedListener(new TextWatcher(){
@Override
public void afterTextChanged(Editable arg0) {
// Do nothing
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// Do nothing
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
objAdapter.getFilter().filter(s);
}
});
}
public void showToast(String msg) {
Toast.makeText(DataView.this, msg, Toast.LENGTH_LONG).show();
}
}
アイテムクラス:
public class Item {
private int id;
private String name;
private String date;
private String startTime;
private String endTime;
private String location;
private int deleteFlag;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public int getDeleteFlag() {
return deleteFlag;
}
public void setDeleteFlag(int deleteFlag) {
this.deleteFlag = deleteFlag;
}
}
アダプター:
public class NewsRowAdapter extends ArrayAdapter<Item> {
private Activity activity;
private List<Item> items;
private Item objBean;
private int row;
private List<Integer> disable;
View view ;
int disableView;
ListFilter filter ;
List<Item> filteredItems;
public NewsRowAdapter(Activity act, int resource, List<Item> arrayList, List<Integer> disableList) {
super(act, resource, arrayList);
this.activity = act;
this.row = resource;
this.items = arrayList;
filteredItems = items;
this.disable=disableList;
}
public int getCount() {
return items.size();
}
public Item getItem(int position) {
return items.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
for(int k =0;k < disable.size();k++){
if(position==disable.get(k)){
disableView=disable.get(k);
System.out.println("disagle view at" +disableView);
}
}
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
getItemViewType(position);
if(position==disableView){
view.setBackgroundColor(Color.YELLOW);
}else{
view.setBackgroundColor(Color.WHITE);
}
//ViewHolder is a custom class that gets TextViews by name: tvName, tvCity, tvBDate, tvGender, tvAge;
holder = new ViewHolder();
/* setTag Sets the tag associated with this view. A tag can be used to
* mark a view in its hierarchy and does not have to be unique
* within the hierarchy. Tags can also be used to store data within
* a view without resorting to another data structure.
*/
view.setTag(holder);
} else {
getItemViewType(position);
if(position==disableView){
view.setBackgroundColor(Color.YELLOW);
}else{
view.setBackgroundColor(Color.WHITE);
}
//the Object stored in this view as a tag
holder = (ViewHolder) view.getTag();
}
if ((items == null) || ((position + 1) > items.size()))
return view;
objBean = items.get(position);
holder.tv_event_name = (TextView) view.findViewById(R.id.tv_event_name);
holder.tv_event_date = (TextView) view.findViewById(R.id.tv_event_date);
holder.tv_event_start = (TextView) view.findViewById(R.id.tv_event_start);
holder.tv_event_end = (TextView) view.findViewById(R.id.tv_event_end);
holder.tv_event_location = (TextView) view.findViewById(R.id.tv_event_location);
if (holder.tv_event_name != null && null != objBean.getName()
&& objBean.getName().trim().length() > 0) {
holder.tv_event_name.setText(Html.fromHtml(objBean.getName()));
}
if (holder.tv_event_date != null && null != objBean.getDate()
&& objBean.getDate().trim().length() > 0) {
holder.tv_event_date.setText(Html.fromHtml(objBean.getDate()));
}
if (holder.tv_event_start != null && null != objBean.getStartTime()
&& objBean.getStartTime().trim().length() > 0) {
holder.tv_event_start.setText(Html.fromHtml(objBean.getStartTime()));
}
if (holder.tv_event_end != null && null != objBean.getEndTime()
&& objBean.getEndTime().trim().length() > 0) {
holder.tv_event_end.setText(Html.fromHtml(objBean.getEndTime()));
}
if (holder.tv_event_location != null && null != objBean.getLocation ()
&& objBean.getLocation ().trim().length() > 0) {
holder.tv_event_location.setText(Html.fromHtml(objBean.getLocation ()));
}
return view;
}
public class ViewHolder {
public TextView
tv_event_name,
tv_event_date,
tv_event_start,
tv_event_end,
tv_event_location
/*tv_event_delete_flag*/;
}
private class ListFilter extends Filter{
List<Item> filt;
List<Item> tmpItems;
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults retval = new FilterResults();
List<Item> filt;
List<Item> tmpItems;
retval.values = items;
retval.count = items.size();
if(constraint != null && constraint.toString().length() > 0) {
constraint = constraint.toString().toUpperCase();
filt = new ArrayList<Item>();
tmpItems = new ArrayList<Item>();
tmpItems.addAll(items);
for(int i = 0; i > tmpItems.size(); i++) {
Item sf = tmpItems.get(i);
if(sf.getName().toUpperCase().contains(constraint)) {
filt.add(sf);
}
}
retval.count = filt.size();
retval.values = filt;
}
return retval;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredItems = (ArrayList)results.values;
notifyDataSetChanged();
clear();
for (int i = 0; i> filteredItems.size(); i++){
add(filteredItems.get(i));
}
notifyDataSetInvalidated();
}
}
@Override
public Filter getFilter() {
if (filter == null){
filter = new ListFilter();
}
return filter;
}
}
XML レイアウト:
<?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="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/layoutButtons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp" >
<Button
android:id="@+id/btnLogout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".30"
android:text="Logout" />
<Button
android:id="@+id/btnMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".70"
android:text="Send Message To All" />
</LinearLayout>
<EditText
android:id="@+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Search" >
</EditText>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>