Android:ListView仅显示第一个元素

时间:2015-09-05 14:22:47

标签: android listview android-listview android-arrayadapter

我的自定义ListView有问题。我创建了自定义适配器,但在列表中只显示了第一个元素。我无法弄清楚为什么。 这里有列表所在的片段代码

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

这里有我的布局代码

  <?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>

这里是我的适配器的代码

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

3 个答案:

答案 0 :(得分:2)

使用此代码,您需要在if语句之外设置convertview变量:

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

答案 1 :(得分:1)

您的数组适配器大小设置为1,您永远不会更改它。

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

答案 2 :(得分:1)

你为什么要用这些物品的柜台?您的counter变量始终是相同的值(1),并且还可以参考您的getCount方法。 您可以相应地更改它,但在我看来,除非您有一些特定的用例,否则最好只使用getCount方法中的列表长度:

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