Iam creating contacts app that contain search text and below it ListView that contain all contact

11 Views Asked by At

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) {
    }
}

}
1

There are 1 best solutions below

0
On

make contact class as serializable and make interface into listadapter like below code..ListAdapter class.

    onItemClickListner onItemClickListner;

public void setOnItemClickListner(CommentsAdapter.onItemClickListner onItemClickListner) {
    this.onItemClickListner = onItemClickListner;
}

public interface onItemClickListner {
    void onClick(Contact contact);//pass your object types.
}
@Override
public View getView(final int position, final View convertView, ViewGroup parent) {
    Contact contact=contactList.get(position);
    final LayoutInflater inflater = ((Activity) context).getLayoutInflater();
    final View outerContiner = inflater.inflate(R.layout.table_layout, parent, false);// define your layout
    TextView text = (TextView) outerContiner.findViewById(R.id.tlTvName);
    TextView barcode = (TextView) outerContiner.findViewById(R.id.tlTvBar);

    text.setText(data.get(position).itemName);
    barcode.setText(data.get(position).barcod);
    outerContiner.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onItemClickListner.onClick(contact);
        }
    });
    return outerContiner;
}

then after bind adapter into listview call below code and put intent code..

        adapter.setOnItemClickListner(new CommentsAdapter.onItemClickListner() {
        @Override
        public void onClick(Contact contact) {
            // here put intent code and get contact detail to click listview item.
            adapter.notifyDataSetChanged();
        }
    });