我正在构建一个简单的应用程序,其中SQLite是数据库源。我主要有2个按钮,其中一个按钮正在保存(按钮保存)数据,另一个按钮正在显示(按钮显示)并带有祝酒消息。但现在我想在具有新活动的自定义列表视图中显示。
这是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();
}
}
这是另一个活动,我想在用户按下“显示” 按钮
时显示数据库中的所有数据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>
但是当我按下Display按钮时,它显示为空活动。我的xml映射不正确,或者适配器无法从显示方法加载数据。
答案 0 :(得分:-1)
创建适配器后,您没有将适配器设置为列表视图。
将您的代码更新为
adapter = new CustomAdapter(DisplayListView.this, R.layout.list_item, employees);
displayEmployee.setAdapter(adapter);