В моем listview с пользовательским адаптером sql данные не отображаются

#java #sqlite #adapter

#java #sqlite #адаптер

Вопрос:

Ошибки не отображаются, но данные о моей активности не отображаются.

В моем sql-коде есть 6 элементов данных: id, имя, фамилия, электронная почта, номер телефона и пароль, и я создал пользовательский адаптер для просмотра всех из них в виде списка, и я не знаю, где ошибка.

данные не появились

DatabaseHelper.java

     package edu.angelo.parentsportal;

    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;

    import java.util.ArrayList;

    public class DatabaseHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "Parents_Portal.db";
public static final String TABLE_NAME = "Parents_Table";
public static final String COL_0 = "ID";
public static final String COL_1 = "NAME";
public static final String COL_2 = "SURNAME";
public static final String COL_3 = "EMAIL_ADDRESS";
public static final String COL_4 = "PHONE_NUMBER";
public static final String COL_5 = "PASSWORD";

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "   TABLE_NAME  "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, EMAIL_ADDRESS TEXT, PHONE_NUMBER TEXT, PASSWORD TEXT)");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " TABLE_NAME);
onCreate(db);
}

public boolean insertData(String name, String surname, String email_address, String phone_number, String password){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1,name);
    contentValues.put(COL_2,surname);
    contentValues.put(COL_3,email_address);
    contentValues.put(COL_4,phone_number);
    contentValues.put(COL_5,password);
    long result = db.insert(TABLE_NAME, null , contentValues);
    if (result == -1) {
        return false;
    }
    else {
        return true;
    }
}

public Cursor getAllData(){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor result = db.rawQuery("select * from "  TABLE_NAME,null);
    return resu<
}


public ArrayList<ParentModel> getAllParentsData(){
    ArrayList<ParentModel> list = new ArrayList<>();
    String sql = "select * from "   TABLE_NAME;
    SQLiteDatabase mydb = this.getWritableDatabase();
    Cursor cursor = mydb.rawQuery(sql, null);
    if (cursor.moveToFirst()) {
        do {
            ParentModel parentModel = new ParentModel();
            parentModel.setID(cursor.getInt(0));
            parentModel.setName(cursor.getString(1));
            parentModel.setSurname(cursor.getString(2));
            parentModel.setEmail(cursor.getString(3));
            parentModel.setPhone_number(cursor.getString(4));
            parentModel.setPassword(cursor.getString(5));
            list.add(parentModel);
        }
        while (cursor.moveToNext());
    }
    return list;
}
}
  

Admin_Parents.java

     package edu.angelo.parentsportal;

    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.v7.app.AlertDialog;
    import android.view.View;
    import android.support.design.widget.NavigationView;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.ArrayAdapter;
    import android.widget.ListAdapter;
    import android.widget.ListView;
    import android.widget.Toast;

    import java.util.ArrayList;

    public class Admin_Parents extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

DatabaseHelper mydb;
ListView listView;
ArrayList<ParentModel> arrayList;
CustomAdapter customAdapter;

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

    mydb = new DatabaseHelper(this);
    listView = findViewById(R.id.admin_parent_list_view);
    arrayList = new ArrayList<>();
    arrayList = mydb.getAllParentsData();
    customAdapter = new CustomAdapter(this, arrayList);

    listView.setAdapter(customAdapter);


    layoutTools();
    }
}
  

CustomAdapter.java

 package edu.angelo.parentsportal;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.zip.Inflater;

public class CustomAdapter extends BaseAdapter {
private Activity activity;
private ArrayList<ParentModel> items;
private LayoutInflater inflater;

ParentModel parentModel;

TextView tvid;
TextView tvname;
TextView tvsurname;
TextView tvemail;
TextView tvphonenumber;
TextView tvpassword;

public CustomAdapter(Activity activity, ArrayList<ParentModel> items) {
    this.activity = activity;
    this.items = items;
}

@Override
public int getCount() {
    return 0;
}

@Override
public ParentModel getItem(int position) {
    return items.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (inflater == null){
        inflater = (LayoutInflater) 
    activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    if (convertView == null){
        convertView = inflater.inflate(R.layout.row, null);
    }

    TextView tvid = convertView.findViewById(R.id.tv_id);
    TextView tvname = convertView.findViewById(R.id.tv_name);
    TextView tvsurname = convertView.findViewById(R.id.tv_surname);
    TextView tvemail = convertView.findViewById(R.id.tv_email);
    TextView tvphonenumber = convertView.findViewById(R.id.tv_PhoneNumber);
    TextView tvpassword = convertView.findViewById(R.id.tv_password);

    ParentModel parentModel = items.get(position);
    tvid.setText(parentModel.getID());
    tvname.setText(parentModel.getName());
    tvsurname.setText(parentModel.getSurname());
    tvemail.setText(parentModel.getEmail());
    tvphonenumber.setText(parentModel.getPhone_number());
    tvpassword.setText(parentModel.getPassword());


    return convertView;
    }
}
  

ParentModel.java

     package edu.angelo.parentsportal;

public class ParentModel {
public int ID;
public String Name;
public String Surname;
public String Email;
public String Phone_number;
public String Password;

public int getID() {
    return ID;
}

public void setID(int ID) {
    this.ID = ID;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public String getSurname() {
    return Surname;
}

public void setSurname(String surname) {
    Surname = surname;
}

public String getEmail() {
    return Email;
}

public void setEmail(String email) {
    Email = email;
}

public String getPhone_number() {
    return Phone_number;
}

public void setPhone_number(String phone_number) {
    Phone_number = phone_number;
}

public String getPassword() {
    return Password;
}

public void setPassword(String password) {
    Password = password;
    }
}
  

Activity_admin__parent.xml

     <?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_admin__parents"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@ id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/admin_nav_header"
    app:menu="@menu/admin_drawer_menu" />

</android.support.v4.widget.DrawerLayout>
  

row.xml

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">

<TextView
    android:id="@ id/tv_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:text="@string/id" />

<TextView
    android:id="@ id/tv_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:text="@string/name" />

<TextView
    android:id="@ id/tv_surname"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_height="wrap_content"
    android:text="@string/surname" />

<TextView
    android:id="@ id/tv_email"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_height="wrap_content"
    android:text="@string/email_address" />

<TextView
    android:id="@ id/tv_PhoneNumber"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_height="wrap_content"
    android:text="@string/phone_number" />

<TextView
    android:id="@ id/tv_password"
    android:layout_width="match_parent"
    android:layout_marginBottom="10dp"
    android:layout_height="wrap_content"
    android:text="@string/password" />
</LinearLayout>
  

Комментарии:

1. Вы пробовали отладку?

Ответ №1:

В вашем пользовательском адаптере вы возвращаете 0 в методе getCount (). Вы должны вернуть длину ваших элементов ArrayList.