ViewHolder使用自定义适配器

时间:2016-04-16 10:57:13

标签: android

我是Android开发人员的新手,我试图创建一个联系人列表,一个左边是图片的列表和联系人的姓名。

所以我按照教程编写了这段代码,它没有显示任何内容,你知道它可能来自何处?

MainActivity.java:

public class MainActivity extends Activity {
ListView list;

private ArrayList<Contact> List = new ArrayList<Contact>();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Contact Nico = new Contact("nico",R.drawable.smileyhappy);
    Contact Sev = new Contact("Sev",R.drawable.smileyveryhappy);


    List.add(Nico);
    List.add(Sev);


    CustomList adapter = new CustomList(MainActivity.this,List);
    list=(ListView)findViewById(R.id.list);
    list.setAdapter(adapter);
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(MainActivity.this, "You Clicked at " + List.get(+position).toString(), Toast.LENGTH_SHORT).show();

        }
    });

}

}

CustomList.java

public class CustomList extends BaseAdapter{

LayoutInflater mInflater;
Activity Context;
ArrayList<Contact> contactList = null;

public CustomList(Activity context, ArrayList<Contact> contactList) {
    this.Context=context;
    this.contactList = contactList;
}

static class ViewHolder {
    public TextView mNom;
    public ImageView mPhoto;
}


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

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public View getView(int r, View convertView, ViewGroup parent) {
    ViewHolder holder ;
    // Si la vue n'est pas recyclée
    if(convertView == null) {
        // On récupère le layout
        LayoutInflater mInflater = Context.getLayoutInflater();
        convertView  = mInflater.inflate(R.layout.list_single, null);

        holder = new ViewHolder();
        // On place les widgets de notre layout dans le holder
        holder.mNom = (TextView) convertView.findViewById(R.id.txt);
        holder.mPhoto = (ImageView) convertView.findViewById(R.id.img);

        // puis on insère le holder en tant que tag dans le layout
        convertView.setTag(holder);
    } else {
        // Si on recycle la vue, on récupère son holder en tag
        holder = (ViewHolder)convertView.getTag();
    }

    // Dans tous les cas, on récupère le contact téléphonique concerné
    Contact c = (Contact)getItem(r);
    // Si cet élément existe vraiment…
    if(c != null) {
        System.out.println("coucou");
        // On place dans le holder les informations sur le contact
        holder.mNom.setText(c.getNom());
        holder.mPhoto.setImageResource(c.getImageId());
    }
    return convertView;
}

}

Contact.java

public class Contact {

String nom;
int ImageId;

public Contact(String nom, int imageId) {
    this.nom = nom;
    ImageId = imageId;
}


public String getNom() {
    return nom;
}

public int getImageId() {
    return ImageId;
}

public void setNom(String nom) {
    this.nom = nom;
}

public void setImageId(int imageId) {
    ImageId = imageId;
}
}

list_single.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="match_parent"
         android:layout_height="match_parent" >
<RelativeLayout>
    <ImageView
        android:id="@+id/img"
        android:layout_width="50dp"
        android:layout_height="50dp"/>

    <TextView
        android:id="@+id/txt"
        android:layout_toRightOf="@id/img"
        android:layout_width="wrap_content"
        android:layout_height="50dp" />

</RelativeLayout>
</TableLayout>

1 个答案:

答案 0 :(得分:0)

由于在适配器的getView中,您使用的是getItem函数,因此需要对其进行编码。

替换

@Override
public Object getItem(int position) {
    return null;
}

用这个:

@Override
public Object getItem(int position) {
  if (contactList == null) {
    return null;
  }
  if (contactList.size() <= position) {
    return null;
  }
  return (contactList.get(position));
}

同样完成getCount函数,如下所示:

@Override
public int getCount() {
  return (contactList == null ? 0 : contactList.size());
}

希望有所帮助!