【发布时间】:2019-01-24 07:48:06
【问题描述】:
我正在构建一个简单的应用程序,其中 SQLite 是数据库源。我主要有 2 个按钮,其中一个正在保存(按钮保存)数据,另一个正在显示(按钮显示)并带有 toast 消息。但现在我想在自定义列表视图中显示一个新活动。
这是 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();
}
}
这是另一个 Activity,我想在用户按下 “显示” 按钮时显示数据库中的所有数据
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();
}
}
}
这是自定义适配器
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;
}
}
这是自定义布局的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 没有正确映射或适配器无法从显示方法加载数据。
【问题讨论】:
-
使用CursorAdapter 而不是
ArrayAdapter。使用Sqlite会更容易 -
在适配器下方设置 displayEmployee.setAdapter(adapter) = new CustomAdapter(DisplayListView.this, R.layout.list_item, employees);
-
也试过了,但还是没有。 @NguyễnTrungHiếu
-
员工人数?
-
什么意思? @NguyễnTrungHiếu
标签: java android android-layout android-listview android-sqlite