android适配器+listview

Posted goodshred

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>

 

以上是关于android适配器+listview的主要内容,如果未能解决你的问题,请参考以下文章

Android - 片段中的 setAdapter [重复]

如何使用 viewpager 和片段为我的 ListView 设置适配器

Android - 为列表视图填充适配器的异步任务

带有复选框和自定义适配器的 ListView,片段无法正常工作

片段活动中的 ListView 适配器上下文错误,我该怎么办?

ListView 未显示在片段中