Iam creating contacts app that contain search text and below it ListView that contain all contact when click on any list item display in another activity all item data but the error is when I search names and give me aspecified user when i click it give me data for another user.
public class showAllContacts extends AppCompatActivity {
String name, phone, mail;
ArrayList<listObject> arr = new ArrayList<listObject>();
ListView listView;
EditText search;
ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_all_contacts);
listView = (ListView) findViewById(R.id.list_item);
search=(EditText)findViewById(R.id.search);
//get intent and data that users enter in class entercontacts.java
Intent i = getIntent();
Bundle b = i.getExtras();
//create object of database connection class
DbConnection dp = new DbConnection(showAllContacts.this);
/*
if bundle carry data insert it to database
*/
if (b != null) {
name = b.getString("name");
phone = b.getString("phone");
mail = b.getString("mail");
dp.InsertRowUser(name, phone, mail);
}
//get data from databse and put it in array
arr = dp.getAllRecords();
adapter = new ListAdapter(showAllContacts.this, R.layout.list_design, arr);
listView.setAdapter(adapter);
/*
if user click item in contacts ListView go to userpage class with array(arr) that carry data
*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(showAllContacts.this, userPage.class);
Bundle bundle = new Bundle();
bundle.putSerializable("userData", (Serializable) arr);
bundle.putInt("postion", position);
intent.putExtra("bundle", bundle);
startActivity(intent);
}
});
/*
if user search in the search text
*/
search.addTextChangedListener(new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// showAllContacts.this.adapter.getFilter().filter(s);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
int textlength=s.length();
ArrayList<listObject>listObjects=new ArrayList<listObject>();
for (listObject c:arr){
if(textlength<=c.Name.length()){
if(c.Name.toLowerCase().contains(s.toString().toLowerCase())){
listObjects.add(c);
}
}
}
adapter = new ListAdapter(showAllContacts.this, R.layout.list_design, listObjects);
listView.setAdapter(adapter);
}
@Override
public void afterTextChanged(Editable s) {
}
});
} catch (NullPointerException E) {
} catch (Exception e) {
}
}
}
make contact class as serializable and make interface into listadapter like below code..ListAdapter class.
then after bind adapter into listview call below code and put intent code..