GetView位置始终为0.遵循http://www.codelearn.org/android-tutorial/android-listview中显示的基本概念,我的代码如下:
public void DisplayUserList()
{ // Get data list from SQL Database
UserList = mySQLiteHelper.getUserList(ACTIVE);
// Get ListView
user_listview_control = (ListView) findViewById(R.id.user_listview);
// Get Adapter
userAdapter = new UserAdapter();
// Bind it all together
user_listview_control.setAdapter(userAdapter);
} // end DisplayUserList()
// User List Base Adapter
public class UserAdapter extends BaseAdapter {
@Override
public int getCount() {
return UserList.size();
}
@Override
public Structures.user_struct getItem(int arg0){
return UserList.get(arg0);
}
@Override
public long getItemId(int arg0){return arg0;}
@Override
public View getView(int position, View arg1, ViewGroup arg2){
if (arg1 == null){
LayoutInflater inflater = (LayoutInflater) Settings.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
try {
arg1 = inflater.inflate(R.layout.userlist_item, arg2, false);
}
catch (Exception ex){
String errorMessage = ex.getMessage();
}
}
TextView username = (TextView)arg1.findViewById(R.id.username);
TextView firstname = (TextView)arg1.findViewById(R.id.userfirstname);
TextView lastname = (TextView)arg1.findViewById(R.id.userlastname);
TextView lastlogin = (TextView)arg1.findViewById(R.id.userlastlogin);
UserData = UserList.get(position); //(always zero)
username.setText(user.username);
firstname.setText(user.user_firstname);
lastname.setText(user.user_lastname);
if (user.user_lastlogin_date.length() == 0) {
lastlogin.setText("--");
} else {
lastlogin.setText(user.user_lastlogin_date);
}
// OnClickListener for this row.
arg1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(Settings.this, "User Clicked", Toast.LENGTH_SHORT).show();
//Integer position = (Integer) view.getTag();
}
});
return arg1;
}
} // end UserAdapter()
sqlite数据表有三个在UserList中返回的项目,定义为:
public List<Structures.user_struct> UserList;
当调用user_listview_control.setAdapter时,在调用getView()之前重复调用getCount()(返回值3)。在getView()中,位置始终为0.生成的ListView显示仅与数据库的第一项相对应。 [注意:如果我强制位置= 1或2,代码将返回该列表中的下一个用户]。我不知道我在哪里。可能是限制显示的xml布局吗?当我运行上面引用的示例代码时,它确实可以作为一个简单的xml布局。所以???
答案 0 :(得分:0)
GetUserList:
public List<Structures.user_struct> getUserList(int isActive) {
// Get List of Users in database
// Filter isActive 0 = Not, 1 = Is, 2 = ALL
// NOte: Is userList database does not exist, create it.
Structures.user_struct UserData = new Structures.user_struct();
List<Structures.user_struct> UserList = new ArrayList<Structures.user_struct>();
SQLiteDatabase db = this.getReadableDatabase();
String strSQL = "select * from " + Structures.USER_TABLE_NAME ;
if (isActive < 2){ // filter is or is not active Only
strSQL += " where " + Structures.USER_COL_IS_ACTIVE + "=" + isActive;
}
Cursor cursor = db.rawQuery(strSQL, null);
cursor.moveToFirst();
while (cursor.isAfterLast() == false) {
UserData.user_id = cursor.getInt(cursor.getColumnIndex(Structures.USER_COL_ID));
UserData.user_is_active = cursor.getInt(cursor.getColumnIndex(Structures.USER_COL_IS_ACTIVE));
UserData.username = cursor.getString(cursor.getColumnIndex(Structures.USER_COL_USERNAME));
UserData.user_password = cursor.getString(cursor.getColumnIndex(Structures.USER_COL_PASSWORD));
UserData.user_firstname = cursor.getString(cursor.getColumnIndex(Structures.USER_COL_FIRSTNAME));
UserData.user_lastname = cursor.getString(cursor.getColumnIndex(Structures.USER_COL_LASTNAME));
UserData.user_date_created = cursor.getString(cursor.getColumnIndex(Structures.USER_COL_DATECREATED));
UserData.user_date_updated = cursor.getString(cursor.getColumnIndex(Structures.USER_COL_DATEUPDATED));
UserData.user_lastlogin_date = cursor.getString(cursor.getColumnIndex(Structures.USER_COL_DATELASTLOGIN));
// Copy UserData to ArrayList of structures
UserList.add(UserData);
cursor.moveToNext();
UserData = new Structures.user_struct();
}
return UserList;
} // end getUserList()
UserData
// User Data Structure
public static class user_struct {
public int user_id;
public int user_is_active; // 0=not, 1=is
public String username;
public String user_password;
public String user_firstname;
public String user_lastname;
public int user_permission_level_id;
public String user_date_created;
public String user_date_updated;
public String user_lastlogin_date;
} // end user_structure()
userlist_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:id="@+id/username"
android:layout_width="@dimen/user_list_width"
android:layout_height="wrap_content"
style="@style/User_List"
android:text="username"/>
<Space
android:layout_width="5dp"
android:layout_height="wrap_content" />
<TextView android:id="@+id/userfirstname"
android:layout_width="@dimen/user_list_width"
android:layout_height="wrap_content"
style="@style/User_List"
android:text="first name"/>
<Space
android:layout_width="5dp"
android:layout_height="wrap_content" />
<TextView android:id="@+id/userlastname"
android:layout_width="@dimen/user_list_width"
android:layout_height="wrap_content"
style="@style/User_List"
android:text="last name"/>
<Space
android:layout_width="5dp"
android:layout_height="wrap_content" />
<TextView android:id="@+id/userlastlogin"
android:layout_width="@dimen/user_list_width"
android:layout_height="wrap_content"
style="@style/User_List"
android:text="last login"/>
</LinearLayout>