带有 BaseAdapter 的空白 ListView

Posted

技术标签:

【中文标题】带有 BaseAdapter 的空白 ListView【英文标题】:Blank ListView with BaseAdapter 【发布时间】:2016-01-08 10:14:20 【问题描述】:

我正在尝试使用BaseAdapter 从数据库中填充我的列表。 Logcat 不显示任何错误。这是我的代码。

在这个DatabaseHelper 班级中,我有一张桌子STUDENTS

DatabaseHelper.java

public long saveData(Student std) 
    SQLiteDatabase db = getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put("NAME",std.name);
    cv.put("CITY",std.city);
    cv.put("AGE",std.age);
    long id = db.insert("STUDENTS", null, cv);
    db.close();
    return id;


public ArrayList<Student> getAllStudent() 
    ArrayList<Student> studentArrayList = new ArrayList<Student>();
    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.query("STUDENTS",null,null,null,null,null,null);
    Log.e("COUNT",""+cursor.getCount());

    while (cursor.moveToNext()) 
        Student s = new Student();
        s.name = cursor.getString(cursor.getColumnIndex("NAME"));
        s.city = cursor.getString(cursor.getColumnIndex("CITY"));
        s.age = cursor.getString(cursor.getColumnIndex("AGE"));
        studentArrayList.add(s);
    
    db.close();
    return studentArrayList;

MyBaseAdapter.java

public class MyBaseAdapter extends BaseAdapter 

ArrayList<Student> myList = new ArrayList<Student>();
LayoutInflater inflater;
Context context;

public MyBaseAdapter(Context context,ArrayList<Student> myList) 
    this.myList = myList;
    this.context = context;
    inflater = LayoutInflater.from(this.context);


@Override
public int getCount() 
    return myList.size();


@Override
public Student getItem(int position) 
    return myList.get(position);


@Override
public long getItemId(int position) 
    return 0;


@Override
public View getView(int position, View convertView, ViewGroup parent) 
    myViewHolder viewHolder;
    if(convertView == null)
        convertView = inflater.inflate(R.layout.single_row, null);
        viewHolder = new myViewHolder();
        convertView.setTag(viewHolder);
     else 
        viewHolder = (myViewHolder) convertView.getTag();
    

    viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.nameTextView);
    viewHolder.cityTextView = (TextView) convertView.findViewById(R.id.cityTextView);
    viewHolder.ageTextView = (TextView) convertView.findViewById(R.id.ageTextView);

    viewHolder.nameTextView.setText(myList.get(position).name);
    viewHolder.cityTextView.setText(myList.get(position).city);
    viewHolder.ageTextView.setText(myList.get(position).age);
    return convertView;


public class myViewHolder 
    TextView nameTextView, cityTextView, ageTextView;


这是我的模型类

Student.java

public class Student 
 String name;
 String city;
 String age;

在我的 MainActivity 中,我正在尝试将数据填充到列表中。

MainActivity.java

public class MainActivity extends ActionBarActivity 

public EditText searchEditText;
public Button addButton;
public ListView studentListView;
public DatabaseHelper dbHelper;
public ArrayList<Student> arrayList;
public MyBaseAdapter myBaseAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    searchEditText = (EditText)findViewById(R.id.searchEditText);
    addButton = (Button)findViewById(R.id.addButton);
    studentListView = (ListView) findViewById(R.id.studentListView);

    addButton.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View v) 
            Intent i = new Intent(MainActivity.this,AddNewStudent.class);
            startActivity(i);
        
    );

    dbHelper = new DatabaseHelper(getApplicationContext());
    arrayList = dbHelper.getAllStudent();
    myBaseAdapter = new MyBaseAdapter(MainActivity.this,arrayList);
    studentListView.setAdapter(myBaseAdapter);
    myBaseAdapter.notifyDataSetChanged();


single_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:orientation="vertical"
android:layout_
android:id="@+id/container">

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/nameTextView" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/cityTextView" />

<TextView
    android:layout_
    android:layout_
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/ageTextView" />
    </LinearLayout>

我不知道我哪里出错了。

【问题讨论】:

请告诉我们R.layout.single_row文件的代码 我已经添加了 single_row.xml 文件。查看我编辑的问题 hm,您确定您的 dbHelper.getAllStudent(); 方法返回正确的数据吗? Adapter 看起来不错(不包括不正确的 View Holder 模式实现),看起来您的 Student 类的所有字段都是空的。 "I am trying to fill my list from database using BaseAdapter" 这是完全错误的方法,请改用SimpleCursorAdapter 不,dbHelper.getAllStudent();不为空。我调试 Array 返回我 size = 4。 【参考方案1】:

终于,我找到了答案。您只需在 getView() 方法中添加几行即可将值从列表分配给模型类对象。

public View getView(int position, View convertView, ViewGroup parent) 

    Student std = new Student(); //this line I added
    std = myList.get(position);  //this line I added
    myViewHolder viewHolder;
    if(convertView == null)
        convertView = inflater.inflate(R.layout.single_row, null);
        viewHolder = new myViewHolder();
        convertView.setTag(viewHolder);
     else 
        viewHolder = (myViewHolder) convertView.getTag();
    

    viewHolder.nameTextView = (TextView) convertView.findViewById(R.id.nameTextView);
    viewHolder.cityTextView = (TextView) convertView.findViewById(R.id.cityTextView);
    viewHolder.ageTextView = (TextView) convertView.findViewById(R.id.ageTextView);

viewHolder.nameTextView.setText(myList.get(position).name);
viewHolder.cityTextView.setText(myList.get(position).city);
viewHolder.ageTextView.setText(myList.get(position).age);
return convertView;

【讨论】:

以上是关于带有 BaseAdapter 的空白 ListView的主要内容,如果未能解决你的问题,请参考以下文章

带有 BaseAdapter 的 Android ListView 内的 ImageButton

ListView 不显示带有 BaseAdapter 的 ImageButton

Android - 带有自定义 BaseAdapter 的 Gridview,创建 onclicklistener [重复]

如何使用 BaseAdapter 刷新带有文本和图像的列表视图?

Android - 带有自定义 BaseAdapter 的 Gridview,在位置获得点击视图

使用带有自定义 baseadapter 的 arraylist 错误地删除列表视图项位置