简体   繁体   中英

How to display sqlite data in custom ListView

I am building a simple app where SQLite is the database source. I have mainly 2 button from which one is saving (Button Save) the data and another one is displaying (Button Display) with a toast message. but now I want to show in a custom list view with a new activity.

This is the MainActivity.java

public class MainActivity extends AppCompatActivity {

    //variable for EditText
    EditText nameET, desigET, numberET, emailET, searcgET, updateID, newNameET;
    //creating a databaseHelper object
    DatabaseHelper dbHelper;
    //display button
    Button displayListView;

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

        //mapping the XML editText with variables
        nameET = (EditText) findViewById(R.id.emp_name);
        desigET = (EditText) findViewById(R.id.designation);
        numberET = (EditText) findViewById(R.id.phone_number);
        emailET = (EditText) findViewById(R.id.email_add);
        searcgET = (EditText) findViewById(R.id.search_edt);
        updateID = (EditText) findViewById(R.id.id_for_update_edt);
        newNameET = (EditText) findViewById(R.id.change_name_edt);
        //display button mapping
        displayListView = (Button) findViewById(R.id.display_btn);
        //initialising the databaseHelper object
        dbHelper = DatabaseHelper.getInstance(getApplicationContext());

        //display button click
        displayListView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //as soon as display button clicked, start the new activity
                Intent displayActivityIntent =
                        new Intent(MainActivity.this, DisplayListView.class);
                startActivity(displayActivityIntent);
            }
        });

    }

    //defining the save button to save data into database
    public void saveData(View view){
        String name = nameET.getText().toString();
        String designation = desigET.getText().toString();
        String phone = numberET.getText().toString();
        String email = emailET.getText().toString();

        //building the employee object of type employee
        Employee employee = new Employee(name, designation, phone, email);
        //calling the insert method to insert the data
        long inserted = dbHelper.insertData(employee);

        if (inserted > + 0){
            Toast.makeText(getApplicationContext(), "Data inserted", Toast.LENGTH_LONG).show();
        }else{
            Toast.makeText(getApplicationContext(), "Data insertion failed, with fail code -1", Toast.LENGTH_LONG).show();
        }
    }

This is another Activity where I want to display all the data from the database when user press "Display" button

public class DisplayListView extends AppCompatActivity {

    ListView displayEmployee;
    DatabaseHelper dbHelper;
    //declaring adapter
    CustomAdapter adapter;
    //declaring data source


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

        displayEmployee = (ListView)findViewById(R.id.display_employee_list);
    }

    //displaying employee from database
    public void display(View view) {
        try {
            //get the employee list from the arrayList
            ArrayList<Employee> employees = dbHelper.getAllEmployees();
            //check if the arrayList is null or not
            if (employees != null && employees.size() > 0){
                //creating and passing argument to adapter object
                adapter = new CustomAdapter(DisplayListView.this, R.layout.list_item, employees);

            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}

And this is the Custom Adapter

public class CustomAdapter extends ArrayAdapter<Employee> {

    //setting up the property
    Activity cont;
    ArrayList<Employee> employeeList;

    public CustomAdapter(Context context, int resource, ArrayList<Employee> employees) {
        super(context, resource);
        //initializing the object property
        this.cont = (Activity) context;
        this.employeeList = employees;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null){
            LayoutInflater inflater = cont.getLayoutInflater();
            view = inflater.inflate(R.layout.list_item, null);
            //getting the textView to display
            TextView nameTextView = (TextView) view.findViewById(R.id.display_name);
            TextView designationTextView1 = (TextView) view.findViewById(R.id.display_designation);
            TextView phoneTextView = (TextView) view.findViewById(R.id.display_phone);

            //set the text info from the position by calling employee object
            Employee emp = employeeList.get(position);
            nameTextView.setText(emp.getName());
            designationTextView1.setText(emp.getDesignation());
            phoneTextView.setText(emp.getPhone());


        }else{
            view = convertView;
        }
        return view;
    }
}

and this is the xml for customizing the layout

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

    <TextView
        android:id="@+id/display_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="larg text"
        android:textColor="#0000ff"
        android:padding="15dp"
        android:textStyle="bold"
        android:textSize="20sp"
        android:textAlignment="center" />
    <TextView
        android:id="@+id/display_designation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="designation"
        android:textColor="#6a5acd"
        android:padding="5dp"
        android:textStyle="italic"
        android:textSize="15sp"
        android:textAlignment="center" />
    <TextView
        android:id="@+id/display_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="phone"
        android:textAlignment="center"
        android:padding="10dp"
        android:textSize="15sp"
        android:textColor="#8b008b"/>
</LinearLayout>

but when I press Display button its showing an empty activity. my xml is not mapping properly or the adapter is not being able to load the data from the display method.

you are not setting adapter to your listview after creating the adapter.

update your code to

adapter = new CustomAdapter(DisplayListView.this, R.layout.list_item, employees);
displayEmployee.setAdapter(adapter);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM