自定义的基本适配器的ListView [英] Custom Base Adapter to ListView
问题描述
我已经尝试了许多不同的教程设法得到它的工作我想知道如果事情是错的我的机器..
这是我的MainActivity,我创建myadapter并将其设置到ListView。
公共类MainActivity延伸活动{UserRepo userRepo;
ArrayList的<使用者>用户列表;
ListView控件列表;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); 名单=(ListView控件)findViewById(R.id.listView);
userRepo =新UserRepo();
用户列表= userRepo.getUserArrayList();
UserAdapter myadapter =新UserAdapter(这一点,用户列表);
list.setAdapter(myadapter);
}}
这是我的自定义用户类。
公共类用户{字符串名称;
字符串描述;
公共用户(字符串名称,字符串描述){
this.name =名称;
this.description =描述;
}公共字符串的getName(){
返回名称;
}公共无效setname可以(字符串名称){
this.name =名称;
}公共字符串getDescription(){
返回描述;
}公共无效setDescription(字符串描述){
this.description =描述;
}
}
这是我的自定义UserAdapter。我从这么多不同的教程尝试了许多不同的版本,并没有不管我做什么,我不能让它与我传递给它的ArrayList工作。
公共类UserAdapter延伸BaseAdapter {
ArrayList的<使用者>清单;
上下文语境;
LayoutInflater mInflater;公共UserAdapter(上下文C,ArrayList的<使用者>清单)
{
上下文= C;
this.list =清单;
mInflater = LayoutInflater.from(this.context);}@覆盖
公众诠释的getCount(){
返回则为list.size();
}@覆盖
公共对象的getItem(INT位置){
返回list.get(位置);
}@覆盖
众长getItemId(INT位置){
返回的位置;
}@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
MyViewHolder mViewHolder; 如果(convertView == NULL){
convertView = mInflater.inflate(R.layout.rowlayout,父母,假);
mViewHolder =新MyViewHolder(convertView);
convertView.setTag(mViewHolder);
}其他{
mViewHolder =(MyViewHolder)convertView.getTag();
} 用户currentListData =(用户)的getItem(位置); mViewHolder.tv_name.setText(currentListData.getName());
mViewHolder.tv_description.setText(currentListData.getDescription());
返回convertView;
}私有类MyViewHolder {
TextView的tv_name,tv_description;
公共MyViewHolder(查看项目){
tv_name =(TextView中)item.findViewById(R.id.tv_name);
tv_description =(TextView中)item.findViewById(R.id.tv_description); }
}
}
这是我的UserRepo,我创建ArrayList和有我可以用它来创建并返回一个方法。
公共类UserRepo {
ArrayList的<使用者>清单;公众的ArrayList<使用者> getUserArrayList(){
用户john =新用户(约翰福音,萨摩亚室友);
list.add(约翰);
用户最高酋长=新用户(最高酋长,毛利人的室友);
list.add(最高酋长);
用户翁=新用户(翁,亚洲Flatemate);
list.add(翁); 返回列表;
}
}
这是我的清单文件
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=com.example.petakirikiri.customlistview>
<应用
机器人:allowBackup =真
机器人:图标=@的mipmap / ic_launcher
机器人:标签=@字符串/ APP_NAME
机器人:supportsRtl =真
机器人:主题=@风格/ AppTheme>
<活动
机器人:名字=。MainActivity
机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme.NoActionBar>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/>
<类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
< /清单>
在 UserRepo
类,你从来没有初始化的ArrayList<使用者>列表
。更改code进行初始化。
公共类UserRepo {
ArrayList的<使用者>名单=新的ArrayList<>();
//你的code的其余部分
}
I have tried so many different tutorials trying to get it work I'm wondering if something is wrong with my machine.. This is my MainActivity where I create myadapter and set it to the ListView.
public class MainActivity extends Activity {
UserRepo userRepo;
ArrayList<user> userList;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
userRepo = new UserRepo();
userList = userRepo.getUserArrayList();
UserAdapter myadapter = new UserAdapter(this, userList );
list.setAdapter(myadapter);
}
}
This is my custom user class.
public class user {
String name;
String description;
public user(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
This is my custom UserAdapter. I have tried so many different versions of this from so many different tutorials and no matter what I do I can't get it to work with the ArrayList that I'm passing it.
public class UserAdapter extends BaseAdapter {
ArrayList<user> list;
Context context;
LayoutInflater mInflater;
public UserAdapter(Context c, ArrayList<user> list)
{
context = c;
this.list = list;
mInflater = LayoutInflater.from(this.context);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.rowlayout, parent, false);
mViewHolder = new MyViewHolder(convertView);
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
user currentListData = (user) getItem(position);
mViewHolder.tv_name.setText(currentListData.getName());
mViewHolder.tv_description.setText(currentListData.getDescription());
return convertView;
}
private class MyViewHolder {
TextView tv_name, tv_description;
public MyViewHolder(View item) {
tv_name = (TextView) item.findViewById(R.id.tv_name);
tv_description = (TextView) item.findViewById(R.id.tv_description);
}
}
}
This is my UserRepo where I create the ArrayList and have a method that I can use to create it and return one.
public class UserRepo {
ArrayList<user> list;
public ArrayList<user> getUserArrayList(){
user john = new user("John", "Samoan Flatmate");
list.add(john);
user ariki = new user ("Ariki", "Māori Flatmate");
list.add(ariki);
user dion = new user ("Dion", "Asian Flatemate");
list.add(dion);
return list;
}
}
This is my manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.petakirikiri.customlistview">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Inside the UserRepo
class you never initialize ArrayList<user> list
. Change the code to initialize it.
public class UserRepo {
ArrayList<user> list = new ArrayList<>();
// The rest of your code
}
这篇关于自定义的基本适配器的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!