Search code examples
androidlistviewandroid-listviewandroid-arrayadapter

Android: ListView shows only the first element


I have a problem with my custom ListView. I created my custom adapter but in the list only the first element is showed. I can't figure out why. Here there is the code of the fragment where the list is locaded

 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedBundle) {

        View firstAccessView;
        if(savedBundle==null) {
            firstAccessView = inflater.inflate(R.layout.search_fragment_layout, null);

            //Contact friendContact = ContactListFragment.findContactById(String.valueOf(this.friendId));

            ((ActionBarActivity)getActivity()).getSupportActionBar().setTitle("Search Friend");

            for(int i=0; i<8; i++){
                Contact c = new Contact(idContact[i], nameSurname[i], facebookId[i], timeStamp[i]);
                this.rows.add(c);

            }

            adapter = new SearchListAdapter(getActivity(), this.rows);
            list = (ListView) firstAccessView.findViewById(R.id.listSearch);
            list.setAdapter(adapter);

            adapter.notifyDataSetChanged();


            list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    Toast.makeText(getActivity().getApplicationContext(), "item selected", Toast.LENGTH_SHORT).show();

                }
            });

        }else{
            firstAccessView = getView();
        }
        return firstAccessView;
    }

here there is the code of my layout

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    >

        <ListView

            android:id="@+id/listSearch"
            android:scrollbars="none"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:footerDividersEnabled="false"
            android:headerDividersEnabled="false"
            android:paddingStart="15dp"
            android:paddingEnd="15dp"
            />

</LinearLayout>

here the code of my adapter

   public class SearchListAdapter extends ArrayAdapter<Contact>{

    private View view;
    private final Activity context;
    private List<Contact> rows;
    private int count = 1;


    public SearchListAdapter(Activity context, List<Contact> rows){

        super(context, R.layout.list_contacts, rows);
        this.context = context;
        this.rows = rows;

    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            view = inflater.inflate(R.layout.list_contacts, null);
            view.setPadding(0,10,0,10);

            holder = new ViewHolder();
            holder.imageContact = (ImageView)view.findViewById(R.id.imageContact);
            holder.nameSurnameContact = (TextView) view.findViewById(R.id.nameSurnameContact);
            holder.idContact = (TextView) view.findViewById(R.id.idContact);

            view.setTag(holder);
        }else{
            view=convertView;
            holder = (ViewHolder) convertView.getTag();

            holder.nameSurnameContact.setTextColor(Color.BLACK);
            holder.nameSurnameContact.setText(this.rows.get(position).getName());
            holder.idContact.setText(this.rows.get(position).getFacebook_id());
            Picasso.with(context).load("https://graph.facebook.com/" + this.rows.get(position).getFacebook_id() + "/picture?height=115&width=115").placeholder(R.mipmap.iconuseranonymous).transform(new CircleTransform()).fit().centerCrop().into(holder.imageContact);

        }

        return view;
    }


    @Override
    public Contact getItem(int position){
        return this.rows.get(position);

    }

    @Override
    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    static class ViewHolder {

        ImageView imageContact;
        TextView nameSurnameContact;
        TextView idContact;
        int position;
    }
}

Solution

  • Use this code, you need to set convertview variables outside the if sentence:

          public class SearchListAdapter extends ArrayAdapter<Contact>{
    
    //private View view;
    private final Activity context;
    private List<Contact> rows;
    private int count;
    
    
    public SearchListAdapter(Activity context, List<Contact> rows){
    
        super(context, R.layout.list_contacts, rows);
        this.context = context;
        this.rows = rows;
    
    }
    
    public View getView(int position, View convertView, ViewGroup parent) {
    
        ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = context.getLayoutInflater();
            convertView = inflater.inflate(R.layout.list_contacts, null);
            convertView.setPadding(0,10,0,10);
    
            holder = new ViewHolder();
            holder.imageContact = (ImageView)convertView.findViewById(R.id.imageContact);
            holder.nameSurnameContact = (TextView) convertView.findViewById(R.id.nameSurnameContact);
            holder.idContact = (TextView) convertView.findViewById(R.id.idContact);
    
            convertView.setTag(holder);
        }else{
            //view=convertView;
            holder = (ViewHolder) convertView.getTag();
    
    
    
        }
    
        holder.nameSurnameContact.setTextColor(Color.BLACK);
            holder.nameSurnameContact.setText(this.rows.get(position).getName());
            holder.idContact.setText(this.rows.get(position).getFacebook_id());
            Picasso.with(context).load("https://graph.facebook.com/" + this.rows.get(position).getFacebook_id() + "/picture?height=115&width=115").placeholder(R.mipmap.iconuseranonymous).transform(new CircleTransform()).fit().centerCrop().into(holder.imageContact);
    
        return convertView;
    }
    
    
    @Override
    public Contact getItem(int position){
        return this.rows.get(position);
    
    }
    
    @Override
    public int getCount() {
        return this.rows.size();
    }
    
    public void setCount(int count) {
        this.count = count;
    }
    
    
    
    class ViewHolder {
    
        ImageView imageContact;
        TextView nameSurnameContact;
        TextView idContact;
        int position;
    }
    }