OK I have a multiple choice ListView that works fine. I check the boxes for the contacts (held in a String[]) and can return the values fine. Because some people have a bunch of contacts I wanted to create a search bar kind of like the stock one for the Android phone book. I created an EditText and aligned it above my list. I found the filtering code here on StackOverflow and it works wonderfully.
My Problem:
When you filter someones name out, and you select the name, when you either backspace from the EditText or continue typing, the correct position of the name you selected is not saved. For example, if I start typing "Adam" and get to "Ada" and select it, if I backspace to type in "Carol", whatever position "Ada" was at is selected. It gathers the place that "Adam" was at from the click (Let's say 2) and when the list is restored checks that position (2) even though Adam is not there anymore. I need a way to gather the name.. then when the list is restored or searched again, the NAME Adam is checked and not the POSITION Adam was previously at. I have absolutely no ideas other than creating tons of Arrays and could really use some help.
public class MainActivity extends Activity {
private static final String[] items={"lorem1", "ipsum1", "dolor1",
"sit1", "amet1","lorem2", "ipsum2", "dolor2",
"sit2", "amet2","lorem3", "ipsum3", "dolor3",
"sit3", "amet3","lorem4", "ipsum4", "dolor4",
"sit4", "amet4","lorem5", "ipsum5", "dolor5",
"sit5", "amet5","lorem6", "ipsum6", "dolor6",
"sit6", "amet6","pigeon","victory"};
ListView list;
Button save;
EditText inputSearch;
SearchView searchview;
ArrayAdapter<String> adapter;
int textlength=0;
// private String my_sel_items;
ArrayList<String> selectedItems1 = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = (new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items));
list = (ListView) findViewById(R.id.list);
list.setEmptyView(findViewById(R.id.empty));
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setAdapter(adapter);
list.setTextFilterEnabled(true);
inputSearch = (EditText) findViewById(R.id.inputSearch);
save = (Button) findViewById(R.id.save);
inputSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
MainActivity.this.adapter.getFilter().filter(cs);
textlength = inputSearch.getText().length();
selectedItems1.clear();
for (int i = 0; i < items.length; i++) {
if (textlength <= items[i].length()) {
if (inputSearch.getText().toString().equalsIgnoreCase(
(String)
items[i].subSequence(0,
textlength))) {
selectedItems1.add(items[i]);
}
}
}
list.setAdapter(new ArrayAdapter<String>
(MainActivity.this,
android.R.layout.simple_list_item_multiple_choice, selectedItems1));
}
@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
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SparseBooleanArray checked = list.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
int position = checked.keyAt(i);
if (checked.valueAt(i))
selectedItems.add(adapter.getItem(position));
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
if (selectedItems.size() < 1) {
Toast.makeText(getApplicationContext(), "Select atleast one ", Toast.LENGTH_LONG).show();
} else {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
Bundle b = new Bundle();
b.putStringArray("selectedItems", outputStrArr);
intent.putExtras(b);
startActivity(intent);
}
}
});
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
}
});
}
}