我尝试了很多不同的教程试图让它工作我想知道我的机器是否有问题。 这是我的MainActivity,我在其中创建myadapter并将其设置为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);
}
}
这是我的自定义用户类。
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;
}
}
这是我的自定义UserAdapter。我从很多不同的教程中尝试了很多这样的不同版本,无论我做什么,我都无法使用我传给它的ArrayList。
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);
}
}
}
这是我的UserRepo,我创建了ArrayList,并且有一个方法可以用来创建它并返回一个。
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;
}
}
这是我的清单文件
<?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>
答案 0 :(得分:1)
在UserRepo
课程中,您永远不会初始化ArrayList<user> list
。更改代码以初始化它。
public class UserRepo {
ArrayList<user> list = new ArrayList<>();
// The rest of your code
}
答案 1 :(得分:0)
您应该创建一个ArrayList实例
public class UserRepo {
ArrayList<user> list;
public ArrayList<user> getUserArrayList(){
list = new ArrayList<>();
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;
}
}