FF91
FF91

Reputation: 29

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

Upvotes: 0

Views: 1923

Answers (3)

Santiago
Santiago

Reputation: 1745

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

Upvotes: 2

Salem
Salem

Reputation: 12986

Why are you using a counter to the items? Your counter variable is always the same value (1), and by reference your getCount method also. You can change it accordingly but it seems to me that, unless you have some specific use case, it would be better to just use the list length in the getCount method:

@Override
public int getCount() {
    return this.rows.size();
}

Upvotes: 1

Breavyn
Breavyn

Reputation: 2242

Your array adapter size is set to 1 and you never change it.

 public SearchListAdapter(Activity context, List<Contact> rows){
     count = rows.size();

Upvotes: 1

Related Questions