我有一个Java类,其中包含有关用户(名称,年龄,类,标记)的信息。我正在尝试创建一个列表视图来显示不同的用户(只有他们的姓名和年龄)。
由于某种原因,列表视图未显示。为什么呢?
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private ListView listView;
private UserAdapter userAdapter;
private ArrayList<User> userList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userList = new ArrayList<>();
DefaultList(); //calls default list function, with dummy values
listView = (ListView) findViewById(R.id.userListView);
//Associating the adapter with the monster list
userAdapter = new userAdapter(this, userList);
listView.setAdapter(userAdapter);
}
public boolean onCreateOptionsMenu(Menu menu){
return true;
}
//Initializes the list view with some default values, for testing purposes only
private void DefaultList(){
userList.add(new User("Nat", 17, "MBS", 4, "98"));
userList.add(new User("Dave", 27, "BIS", 1, "54"));
userList.add(new User("Gray", 21, "BIS", 2, "51"));
userList.add(new User("John", 20, "BIS", 1, "76"));
}
}
列表视图的自定义适配器:
public class UserAdapter extends BaseAdapter {
private Context currentContext;
private ArrayList<User> userList;
public static class ViewHolder {
TextView uName;
TextView uAge;
}
public UserAdapter(Context context, ArrayList<User> user){
currentContext = context;
userList = user;
}
public View getView(int i, View view, ViewGroup viewGroup) {
//If view not created, inflate the view
ViewHolder viewHolder;
if(view == null){
viewHolder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) currentContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.user_item, null);
//attach the ViewHolder to this view
viewHolder = new ViewHolder();
viewHolder.userName = (TextView) view.findViewById(R.id.uNameTextView);
viewHolder.userAge = (TextView) view.findViewById(R.id.uAgeTextView);
view.setTag(viewHolder);
}
else{
//View already created
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.uName.setText(userList.get(i).getUserName());
viewHolder.uAge.setText(userList.get(i).getUserAge());
return view;
}
main_activity的XML文件:
<ListView
android:id="@+id/userListView"
style="@style/Widget.AppCompat.ListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:visibility="visible" />`
每个用户列表项的XML:
<TextView
android:id="@+id/uNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="16dp"
android:layout_marginTop="20dp"
android:text="Name" />
<TextView
android:id="@+id/uAgeTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/uNameTextView"
android:layout_below="@+id/uNameTextView"
android:text="Age" />
我正在使用Android Studio。
答案 0 :(得分:2)
将此方法添加到您的适配器,它将正常工作。
@Override
public int getCount() {
return userList != null ? userList.size() : 0;
}
答案 1 :(得分:0)
您需要在适配器中添加 getCount()方法:
getCount()函数返回要包含的项目总数 显示在列表中。它从数组列表size()方法计算值 或数组的长度。
例如,如果我们有一个元素列表 一个arraylist,我们必须在列表视图中显示项目然后我们 可以使用size函数计算元素的总数 那么整数值由函数getCount()返回为 如下所示。
@Override
public int getCount() {
int count=arrayList.size(); //counts the total number of elements from the arrayList
return count;//returns the total count to adapter
}
检查您的适配器,在您已将textView初始化为:
viewHolder.userName = (TextView) view.findViewById(R.id.uNameTextView);
viewHolder.userAge = (TextView) view.findViewById(R.id.uAgeTextView);
和其他,
viewHolder.uName.setText(userList.get(i).getUserName());
viewHolder.uAge.setText(userList.get(i).getUserAge());
请修复此问题以获得结果。
答案 2 :(得分:0)
从if语句中取出这些行。我认为你的view
第一次不是空的,这会造成麻烦。
LayoutInflater inflater = (LayoutInflater) currentContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.user_item, null);
viewHolder.userName = (TextView) view.findViewById(R.id.uNameTextView);
viewHolder.userAge = (TextView) view.findViewById(R.id.uAgeTextView);
答案 3 :(得分:0)
覆盖getView方法
(->)
答案 4 :(得分:-1)
替换它:public class UserAdapter extends BaseAdapter {
由此:public class UserAdapter extends ArrayAdapter<User>
最好在其他类中为User创建模型;
好运