使用基本适配器的ListView中没有显示数据

时间:2018-09-27 15:04:17

标签: java android sqlite cursor baseadapter

我正在使用BaseAdapter以便在ListView中显示用户配置文件的数据。 这些数据是用SQLite存储的,我通过在UserDAO类中使用Cursor来获取它。该代码可以很好地编译,但是当我对其进行仿真时,会出现白屏,并且我不明白为什么。

我的光标使用户进入DAO

public Cursor getEntrepreneur_2 (){

    SQLiteDatabase database = dbHelper.getReadableDatabase();

    String[] columns = {DataBaseHelper.USER_ID, DataBaseHelper.USER_MAIL, DataBaseHelper.USER_PASSWORD, DataBaseHelper.USER_NAME,
            DataBaseHelper.USER_FIRSTNAME, DataBaseHelper.USER_ROLE, DataBaseHelper.USER_LANGUAGES,
            DataBaseHelper.USER_COUNTRY_FROM, DataBaseHelper.USER_COMMUNITY, DataBaseHelper.USER_PHONE_NUMBER};

    String selection = DataBaseHelper.USER_ID + "= ?";

    return database.query(USER_TABLE, columns, selection, null, null, null, null, null);}

我的UserProfilAdapter

@SuppressLint("InflateParams")

 public class UserProfilAdapter extends BaseAdapter {

private Context mContext;
private ArrayList<String> id;
private ArrayList<String> email;
private ArrayList<String> firstName;
private ArrayList<String> lastName;
private ArrayList<String> password;
private ArrayList<String> phoneNumber;
private ArrayList<String> adressNumber;
private ArrayList<String> streetAdress;
private ArrayList<String> postalCode;

public UserProfilAdapter(Context c, ArrayList<String> id, ArrayList<String> fname, ArrayList<String> lname,
                         ArrayList<String> email, ArrayList<String> pwd,
                         ArrayList<String> phnmbr, ArrayList<String> anmbr,
                         ArrayList<String> stadress,
                         ArrayList<String> pcde) {
    this.mContext = c;

    this.id = id;
    this.email = email;
    this.firstName = fname;
    this.lastName = lname;
    this.password = pwd;
    this.phoneNumber = phnmbr;
    this.adressNumber = anmbr;
    this.streetAdress = stadress;
    this.postalCode = pcde;
}

public int getCount() {
    return id.size();
}

public Object getItem(int position) {
    return id.get(position);
}

public long getItemId(int position) {
    return position;
}

@Override
public View getView(int pos, View child, ViewGroup parent) {
    if (child == null) {
        // Create inflater
        LayoutInflater getLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // Get view from inflater
        assert getLayoutInflater != null;
        child = getLayoutInflater.inflate(R.layout.row_entrepreneur_profil, null);
        TextView txt_mail = (TextView) child.findViewById(R.id.mail_row_e);
        TextView txt_fName = (TextView) child.findViewById(R.id.firstname_row_e);
        TextView txt_lName = (TextView) child.findViewById(R.id.name_row_e);
        TextView txt_pwd = (TextView) child.findViewById(R.id.password_row_e);
        TextView txt_phnmbr = (TextView) child.findViewById(R.id.phone_row_e);
        TextView txt_anmbr = (TextView) child.findViewById(R.id.numero_adress_row_e);
        TextView txt_snmbr = (TextView) child.findViewById(R.id.street_adress_row_e);
        TextView txt_pcde = (TextView) child.findViewById(R.id.postal_code_row_e);

        txt_mail.setText(email.get(pos));
        txt_fName.setText(firstName.get(pos));
        txt_lName.setText(lastName.get(pos));
        txt_pwd.setText(password.get(pos));
        txt_phnmbr.setText(phoneNumber.get(pos));
        txt_anmbr.setText(adressNumber.get(pos));
        txt_snmbr.setText(streetAdress.get(pos));
        txt_pcde.setText(postalCode.get(pos));
    }
    return child;
}

}

主要活动

public class EntrepreneurProfilList extends Activity {

UserDAO entrepreneurDataBase;
UserProfilAdapter profilEntrAdptr;

private ArrayList<String> id;
private ArrayList<String> firstName;
private ArrayList<String> lastName;
private ArrayList<String> email;
private ArrayList<String> password;
private ArrayList<String> phonenumber;
private ArrayList<String> adressnumber;
private ArrayList<String> streetadress;
private ArrayList<String> postalcode;

private ListView userList;

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

    // Lists of texts
    id = new ArrayList<>();
    firstName = new ArrayList<>();
    lastName = new ArrayList<>();
    email = new ArrayList<>();
    password = new ArrayList<>();
    phonenumber = new ArrayList<>();
    adressnumber = new ArrayList<>();
    streetadress = new ArrayList<>();
    postalcode = new ArrayList<>();

    userList = findViewById(R.id.list_data);

    entrepreneurDataBase = new UserDAO(this);
}

@Override
protected void onResume (){
    setContentView(R.layout.list_view_entrepreneur_profil);
    super.onResume();
    displayData();
}

private void displayData(){

    Cursor mCursor = entrepreneurDataBase.getEntrepreneur_2();
    clear();

    if (mCursor.moveToFirst())
    {
        do
        {
            firstName.add(mCursor.getString(mCursor.getColumnIndex(DataBaseHelper.USER_FIRSTNAME)));
            lastName.add(mCursor.getString(mCursor.getColumnIndex(DataBaseHelper.USER_NAME)));
            email.add(mCursor.getString(mCursor.getColumnIndex(DataBaseHelper.USER_MAIL)));
            password.add(mCursor.getString(mCursor.getColumnIndex(DataBaseHelper.USER_PASSWORD)));
            phonenumber.add(mCursor.getString(mCursor.getColumnIndex(DataBaseHelper.USER_PHONE_NUMBER)));
            adressnumber.add(mCursor.getString(mCursor.getColumnIndex(DataBaseHelper.USER_NUMERO_ADRESS)));
            streetadress.add(mCursor.getString(mCursor.getColumnIndex(DataBaseHelper.USER_STREET_ADRESS)));
            postalcode.add(mCursor.getString(mCursor.getColumnIndex(DataBaseHelper.USER_POSTAL_CODE_ADRESS)));

        } while (mCursor.moveToNext());
    }
    profilEntrAdptr = new UserProfilAdapter(getApplicationContext(), id, firstName, lastName, email, password, phonenumber, adressnumber, streetadress, postalcode);
    userList.setAdapter(profilEntrAdptr);
    mCursor.close();
}

public void clear() {
    firstName.clear();
    lastName.clear();
    email.clear();
    password.clear();
    phonenumber.clear();
    adressnumber.clear();
    streetadress.clear();
    postalcode.clear();
}

}

XML listView

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
    android:id="@+id/my_toolbar_entrepreneur_profil_list"
    layout="@layout/toolbar_layout"/>

<View
    android:id="@+id/a"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:paddingBottom="10dp" />

<ListView android:id="@+id/list_data"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:scrollbars="vertical"/>

XML行视图

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:focusableInTouchMode="true"
android:orientation="vertical">

<TextView
    android:id="@+id/name_row_e"
    style="@style/Theme.AppCompat.Light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sample Data"
    android:textSize="15sp"
    android:paddingBottom="5dp"/>

<TextView
    android:id="@+id/firstname_row_e"
    style="@style/Theme.AppCompat.Light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sample Data 2"
    android:textSize="15sp"
    android:paddingBottom="5dp"/>

<TextView
    android:id="@+id/mail_row_e"
    style="@style/Theme.AppCompat.Light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sample Data 3"
    android:textSize="15sp"
    android:paddingBottom="5dp"/>

<TextView
    android:id="@+id/password_row_e"
    style="@style/Theme.AppCompat.Light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sample Data 4"
    android:textSize="15sp"
    android:paddingBottom="5dp"/>

<TextView
    android:id="@+id/phone_row_e"
    style="@style/Theme.AppCompat.Light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sample Data 5"
    android:textSize="15sp"
    android:paddingBottom="5dp"/>

<TextView
    android:id="@+id/numero_adress_row_e"
    style="@style/Theme.AppCompat.Light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sample Data 6"
    android:textSize="15sp"
    android:paddingBottom="5dp"/>

<TextView
    android:id="@+id/street_adress_row_e"
    style="@style/Theme.AppCompat.Light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sample Data 7"
    android:textSize="15sp"
    android:paddingBottom="5dp"/>

<TextView
    android:id="@+id/postal_code_row_e"
    style="@style/Theme.AppCompat.Light"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Sample Data 8"
    android:textSize="15sp"
    android:paddingBottom="5dp"/>

和调试屏幕

here D/daobase: database creating
       database created
D/EGL_emulation: eglMakeCurrent: 0xe80053c0: ver 3 0 (tinfo 0xe80038b0)
I/Choreographer: Skipped 44 frames!  The application may be doing too 
much work on its main thread.
D/EGL_emulation: eglMakeCurrent: 0xe80053c0: ver 3 0 (tinfo 0xe80038b0)
I/OpenGLRenderer: Davey! duration=865ms; Flags=0, 
IntendedVsync=20220849708979, Vsync=20221583042283, 
OldestInputEvent=9223372036854775807, NewestInputEvent=0, 
HandleInputStart=20221595346365, AnimationStart=20221595887365, 
PerformTraversalsStart=20221602163365, DrawStart=20221617032365, 
SyncQueued=20221698057365, SyncStart=20221715953365, 
IssueDrawCommandsStart=20221716144365, SwapBuffers=20221721127365, 
FrameCompleted=20221732761365, DequeueBufferDuration=354000, 
QueueBufferDuration=1403000, 
I/AssistStructure: Flattened final assist data: 1968 bytes, containing 
1 windows, 5 views
I/AssistStructure: Flattened final assist data: 2008 bytes, containing 
1 windows, 5 views
W/ActivityThread: handleWindowVisibility: no activity for token 
android.os.BinderProxy@4ec517b
I/ieux.alsago_ap: Background concurrent copying GC freed 13510(803KB) 
AllocSpace objects, 6(116KB) LOS objects, 49% free, 1659KB/3MB, paused 
724us total 385.743ms
D/daobase: database creating
D/daobase: database created
W/ieux.alsago_ap: Accessing hidden method Landroid/view/View;- 

0 个答案:

没有答案