自定义适配器

package com.example.myadapter;

import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class MainActivity extends AppCompatActivity {
private ListView mListView;
private Listlist;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mListView= (ListView) findViewById(R.id.list_view);
    list=new ArrayList<>();
    for(int i=0;i<50;i++){

        String str="第(i+1)条数据";
        list.add(str);
    }
    MyAdapter adapter=new MyAdapter(list,this);
    mListView.setAdapter(adapter);


















}

}
package com.example.myadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

/**
* Created by 杜春慧 on 2016/4/7.
*/
public class MyAdapter extends BaseAdapter{
private List list;
private Context context;
public MyAdapter(List list,Context context){
this.list = list;
this.context = context;
}

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

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = LayoutInflater.from(context).inflate(R.layout.item_custom,null);
    TextView textView = (TextView)convertView.findViewById(R.id.index_text);
    textView.setText(list.get(position));
    return convertView;
}

}

你可能感兴趣的:(自定义适配器)