我创建了包含搜索文本的联系人应用程序,并在其下方包含所有联系人的ListView

时间:2018-05-13 22:05:58

标签: android

Iam创建包含搜索文本的联系人应用程序及其下方ListView包含所有联系人,当点击任何列表项目显示在另一个活动中所有项目数据但错误是当我搜索名称并在我点击它时给我指定的用户给出我是另一个用户的数据。

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 个答案:

答案 0 :(得分:0)

将联系人类设为serializable,并将接口设置为listadapter,如下面的code..ListAdapter类。

    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;
}

然后将bind适配器绑定到listview之后调用代码并放入intent代码..

        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();
        }
    });