Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/224.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在自定义ListView中显示sqlite数据_Java_Android_Android Layout_Android Listview_Android Sqlite - Fatal编程技术网

Java 如何在自定义ListView中显示sqlite数据

Java 如何在自定义ListView中显示sqlite数据,java,android,android-layout,android-listview,android-sqlite,Java,Android,Android Layout,Android Listview,Android Sqlite,我正在构建一个简单的应用程序,其中SQLite是数据库源。我主要有两个按钮,一个是保存(按钮保存)数据,另一个是显示(按钮显示),带有toast消息。但是现在我想在自定义列表视图中显示一个新的活动 这是主要活动。java public class MainActivity extends AppCompatActivity { //variable for EditText EditText nameET, desigET, numberET, emailET, searcgE

我正在构建一个简单的应用程序,其中SQLite是数据库源。我主要有两个按钮,一个是保存(按钮保存)数据,另一个是显示(按钮显示),带有toast消息。但是现在我想在自定义列表视图中显示一个新的活动

这是主要活动。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();
        }
    }
这是另一个活动,当用户按下“显示”按钮时,我想显示数据库中的所有数据

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();
        }
    }

}
公共类DisplayListView扩展了AppCompative活动{
ListView显示员工;
数据库助手dbHelper;
//声明适配器
自定义适配器;
//声明数据源
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u display\u list\u view);
displayEmployee=(ListView)findViewById(R.id.display\u employee\u list);
}
//显示数据库中的员工
公共空白显示(视图){
试一试{
//从arrayList获取员工列表
ArrayList employees=dbHelper.getAllEmployees();
//检查arrayList是否为空
if(employees!=null&&employees.size()>0){
//创建参数并将其传递给适配器对象
adapter=新的CustomAdapter(DisplayListView.this,R.layout.list_项,employees);
}
}捕获(例外e){
e、 printStackTrace();
}
}
}
这是自定义适配器

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;
    }
}
公共类CustomAdapter扩展了ArrayAdapter{
//设置属性
活动控制;
ArrayList员工列表;
公共CustomAdapter(上下文上下文、int资源、ArrayList员工){
超级(上下文、资源);
//初始化对象属性
this.cont=(活动)上下文;
this.employeeList=员工;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=空;
if(convertView==null){
LayoutInflater充气机=继续getLayoutInflater();
视图=充气机。充气(R.layout.list_项,空);
//获取要显示的textView
TextView name TextView=(TextView)view.findViewById(R.id.display\u name);
TextView指定textView1=(TextView)view.findViewById(R.id.display_指定);
TextView电话TextView=(TextView)view.findViewById(R.id.display\u电话);
//通过调用employee对象从职位设置文本信息
emp=employeeList.get(职位);
nameTextView.setText(emp.getName());
setText(emp.getDesignation());
phoneTextView.setText(emp.getPhone());
}否则{
视图=转换视图;
}
返回视图;
}
}
这是用于自定义布局的xml

<?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>


但当我按下显示按钮时,它显示一个空的活动。我的xml映射不正确,或者适配器无法从显示方法加载数据。

创建适配器后,您没有将适配器设置为listview

将代码更新为

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

使用而不是
ArrayAdapter
。使用
Sqlite
set displayEmployee.setAdapter(adapter)下面的adapter=new CustomAdapter(DisplayListView.this,R.layout.list_项,employees)更容易;也试过了,但还是一无所获@恩圭ễ恩图隆吉ế使用员工?你是什么意思@恩圭ễ恩图隆吉ếu
display(View-View)
是他从xml调用的onClick方法如果我不再想从xml调用该方法,那么我应该在哪里修改调用?