android适配器+listview

简单 继承BaseAdapter:

 

 

Myadapter代码:

public class Myadapter extends BaseAdapter {
private Context context;

private List<User>list;
public Myadapter(Context context, List<User>list){
super();
this.context=context;
this.list=list;
}
@Override
//得到个数
public int getCount() {
return list.size();
}

@Override
//得到所以索引,即i值
public Object getItem(int position) {
return null;
}

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

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

User user=list.get(position);
LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup viewGroup=(ViewGroup) layoutInflater.inflate(R.layout.user_item,null);
TextView textView1= (TextView)viewGroup.findViewById(R.id.textview1);
textView1.setText(user.getName());
TextView textView2= (TextView)viewGroup.findViewById(R.id.textview2);
textView2.setText(user.getPhone());
TextView textView3= (TextView)viewGroup.findViewById(R.id.textview3);
// textView3.setText(user.getAge());
textView3.setText(String.valueOf(user.getAge()));
return viewGroup;
}
}

MianActivity:

public class MainActivity extends AppCompatActivity {
private List<User> list =new ArrayList<User>();
/* private String[] ss=new String[]{
"山西","太原","山东","贵阳","天津","北京","洪湖"
};*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.list_view);
for(int i=0;i<10;i++){
User user=new User("gao"+i,"2333"+i,i);
list.add(user);
}
Myadapter adapter=new Myadapter(this,list);
listView.setAdapter(adapter);
}


}

user类:

public class User {
    private String name;
    private String phone;
    private Integer age;
    
    public User(String name, String phone, Integer age) {
        this.name = name;
        this.phone = phone;
        this.age = age;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  >
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list_view"
        >
    </ListView>
</LinearLayout>

user_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_weight="1"
    android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textview2"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/textview3"
        android:layout_width="wrap_content"
        android:layout_weight="1"
        android:layout_height="wrap_content" />
</LinearLayout>

 

posted @ 2018-10-11 15:45  goodshred  阅读(152)  评论(0编辑  收藏  举报