i have two problem filtering my custom listview:
1) The listview is filtered when I write text but if I delete the filtering text it doesn't return the original listview data
2) How should i handle the checkbox that I check when the listview is filtered
This is my CustomAdapter
public class PositionListViewAdapter extends ArrayAdapter<PositionInfo>
implements Filterable {
private Locale LANGUAGE = Locale.ITALIAN;
static class ViewHolder {
TextView tName;
TextView tAddress;
TextView tRating;
TextView tDate;
ImageView tImage;
}
ArrayList<PositionInfo> originalData;
ArrayList<PositionInfo> filteredData;
final int loader = R.drawable.loader;
ImageLoader imgLoader;
int resLayout;
Context context;
private Filter mFilter;
public PositionListViewAdapter(Context context, int textViewResourceId,
ArrayList<PositionInfo> myPosition) {
super(context, textViewResourceId, myPosition);
imgLoader = new ImageLoader(getContext());
this.filteredData = myPosition;
this.originalData = myPosition;
this.mFilter = new ItemFilter();
resLayout = textViewResourceId;
this.context = context;
}
public int getCount() {
return filteredData.size();
}
public PositionInfo getItem(int position) {
return filteredData.get(position);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater ll = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = ll.inflate(resLayout, parent, false);
holder = new ViewHolder();
holder.tName = (TextView) convertView.findViewById(R.id.pos_name);
holder.tAddress = (TextView) convertView
.findViewById(R.id.pos_address);
holder.tRating = (TextView) convertView
.findViewById(R.id.pos_rating);
holder.tDate = (TextView) convertView.findViewById(R.id.pos_date);
holder.tImage = (ImageView) convertView
.findViewById(R.id.thumbnail);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
PositionInfo item = originalData.get(position);
holder.tName.setText(item.getPositionName());
holder.tAddress.setText(item.getPositionAddress());
holder.tRating.setText("Rating :" + item.getPositionRating());
holder.tDate.setText(item.getPositionData());
if (item.isGoogle())
imgLoader.DisplayImage(item.getIcon(), loader, holder.tImage);
else
imgLoader.DisplayImage(
Constant.url_server_img + "/"+item.getIdImg()+".png", loader,
holder.tImage);
return convertView;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
Log.d("FILTRO", "DENTRO");
}
return mFilter;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
String filterString = constraint.toString().toLowerCase(LANGUAGE);
Log.d("FILTERED_STRING",filterString);
if (filterString == null || filterString.length() == 0) {
ArrayList<PositionInfo> list = new ArrayList<PositionInfo>(
originalData);
results.values = list;
results.count = list.size();
Log.d("ORIGINAL_DATA",originalData.toString());
} else {
final ArrayList<PositionInfo> list = new ArrayList<PositionInfo>(
originalData);
final ArrayList<PositionInfo> nlist = new ArrayList<PositionInfo>();
int count = list.size();
for (int i = 0; i < count; i++) {
final PositionInfo item = list.get(i);
final String value = item.getPositionName().toLowerCase(
LANGUAGE);
Log.d("FILTERING",value);
if (value.contains(filterString)) {
nlist.add(item);
}
results.values = nlist;
results.count = nlist.size();
}
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
filteredData = (ArrayList<PositionInfo>) results.values;
if (filteredData != null) {
Log.d("PUBLIS_LIST_RES", filteredData.size() + "");
notifyDataSetChanged();
clear();
int count = filteredData.size();
for (int i = 0; i < count; i++) {
add(filteredData.get(i));
notifyDataSetInvalidated();
}
}
}
}
}
This is the class where i use the ListView
public class ListaInserzioni extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
private String[] mOptions;
private NavigationDrawerSetup navigation;
private SessionManager sessionManager;
private PositionListViewAdapter myAdapter;
private ListView places;
private android.widget.EditText inputSearch;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_position);
setTitle("Lista");
sessionManager = new SessionManager(this);
menuSettings(savedInstanceState);
DataWrapper dw = (DataWrapper) getIntent().getSerializableExtra(
"LISTA_INSERZIONI");
ArrayList<PositionInfo> myPosition = dw.getList();
if (myPosition == null) {
//dobbiamo fare qualcosa?
}
CheckNetwork.check(this);
places = (ListView) findViewById(R.id.myListView);
myAdapter = new PositionListViewAdapter(this,
R.layout.drawer_list_position, myPosition);
places.setAdapter(myAdapter);
places.setItemsCanFocus(false);
places.setTextFilterEnabled(true);
inputSearch = (android.widget.EditText) findViewById(R.id.inputSearch);
inputSearch.addTextChangedListener(new android.text.TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2,
int arg3) {
myAdapter.getFilter().filter(cs);
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(android.text.Editable s) {
// TODO Auto-generated method stub
}
});
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.confirm_choice, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (navigation.onOptionsItemSelected(item)) {
return true;
} else {
switch (item.getItemId()) {
case R.id.confirm_position_choices:
confirm();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
public void confirm() {
Log.d("TAG", "Sono qui dentro");
ArrayList<PositionInfo> selectedPositions = new ArrayList<PositionInfo>();
final SparseBooleanArray checkedItems = places
.getCheckedItemPositions();
int checkedItemsCount = checkedItems.size();
for (int i = 0; i < checkedItemsCount; ++i) {
// Item position in adapter
int position = checkedItems.keyAt(i);
// Add position if item is checked == TRUE!
if (checkedItems.valueAt(i))
selectedPositions.add(myAdapter.getItem(position));
}
if (selectedPositions.size() < 2)
Toast.makeText(getBaseContext(),
"Need to select two or more locations.", Toast.LENGTH_SHORT)
.show();
else {
// Just logging the output.
for (PositionInfo t : selectedPositions)
Log.d("Location selezionata: ", t.getPositionName());
Intent i = new Intent(ListaInserzioni.this,
ItinerarioSummary.class);
i.putExtra("LISTA_INSERZIONI_SELEZIONATE", new DataWrapper(selectedPositions));
startActivity(i);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
navigation.onConfigurationChanged(newConfig);
}
private void menuSettings(Bundle savedInstanceState) {
mTitle = getTitle();
mOptions = getResources().getStringArray(R.array.options_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_position);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
if (mDrawerLayout == null)
System.out.println("DL");
if (mDrawerList == null)
System.out.println("DLI");
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
navigation = new NavigationDrawerSetup(mDrawerLayout, mDrawerList,
mDrawerToggle, mOptions, this, sessionManager, -1);
navigation.configureNavigationDrawer();
}
}
This is the class for the checkbox
public class CheckableLayout extends LinearLayout implements Checkable {
private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };
public CheckableLayout(Context context) {
super(context, null);
}
public CheckableLayout(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public CheckableLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
private boolean checked;
@Override
public boolean isChecked() {
return checked;
}
@Override
public void setChecked(boolean checked) {
if (this.checked != checked) {
this.checked = checked;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (child instanceof Checkable) {
((Checkable) child).setChecked(checked);
}
}
}
}
@Override
public void toggle() {
setChecked(!checked);
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
}
and finally this is the XML Layout
<?xml version="1.0" encoding="utf-8"?>
<it.team13.findout.custumizedListView.CheckableLayout
xmlns:android="http://ift.tt/nIICcg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/list_row_selector">
<CheckBox
android:id="@+id/myCheckBox"
android:layout_width="43dp"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"
android:layout_alignParentLeft="true"
android:background="@drawable/customcheckbox_background"/>
<RelativeLayout
xmlns:android="http://ift.tt/nIICcg"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/thumbnail"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="8dp"
/>
<TextView
android:id="@+id/pos_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/thumbnail"
android:layout_toRightOf="@id/thumbnail"
android:textSize="@dimen/title"
android:textStyle="bold" />
<TextView
android:id="@+id/pos_rating"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/pos_name"
android:layout_toRightOf="@id/thumbnail"
android:layout_marginTop="1dip"
android:textSize="@dimen/rating" />
<TextView
android:id="@+id/pos_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/pos_rating"
android:layout_toRightOf="@id/thumbnail"
android:layout_marginTop="5dip"
android:textColor="@color/genre"
android:textSize="@dimen/add" />
<TextView
android:id="@+id/pos_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:textColor="@color/year"
android:textSize="@dimen/year" />
</RelativeLayout>
</it.team13.findout.custumizedListView.CheckableLayout>
Thanks for your help !
Aucun commentaire:
Enregistrer un commentaire