I created a custom list view to show some data. Now when I'm entering the data the items doesn't show and my app crashes please help me I used this link for my
Show Multiple data from SQLite database inside ListView in android one by one - Android Examples[
^]
What I have tried:
1. LeaveTran Activity:
LeaveDb myDb;
Button ShowApprovedStatus;
TextInputLayout textInputGoingAbroad;
TextInputLayout textInputLayoutDate;
TextInputLayout textInputLeaveType1;
TextInputLayout textInputLeaveType2;
TextInputLayout textInputDays1;
TextInputLayout textInputExpStDt;
TextInputLayout textInputExpEndDt;
TextInputLayout textInputReqType;
TextInputLayout textInputStatus;
TextInputLayout textInputSubmit;
EditText check_date, Days1, ExpStDt, ExpEndDt;
private Spinner LeaveType1, LeaveType2;
private Spinner ReqType;
CheckBox Status, GoingAbroad;
private LinearLayout parentLinearLayout;
Button onAddField, onDelete ;
<pre> @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_leave_tran);
myDb = new LeaveDb(this);
ShowApprovedStatus = (Button) findViewById(R.id.Submit);
parentLinearLayout = (LinearLayout) findViewById(R.id.parent_linear_layout);
onAddField = (Button) findViewById(R.id.add_field_button);
onDelete = (Button) findViewById(R.id.delete_button);
textInputLeaveType1 = (TextInputLayout) findViewById(R.id.textInputLeaveType1);
textInputReqType = (TextInputLayout) findViewById(R.id.textInputReqType);
textInputLeaveType2 = (TextInputLayout) findViewById(R.id.textInputLeaveType2);
onAddField.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.field, null);
parentLinearLayout.addView(rowView, parentLinearLayout.getChildCount() - 1);
}
});
onDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
parentLinearLayout.removeView((View) v.getParent());
}
});
try {
System.out.println ("Days: " + getDaysBetweenDates("2019-05-14","2019-05-18"));
} catch (java.text.ParseException e) {
e.printStackTrace();
}
final Calendar myCalendar1 = Calendar.getInstance();
check_date = (EditText) findViewById(R.id.check_date);
textInputLayoutDate = (TextInputLayout) findViewById(R.id.textInputLayoutDate);
final DatePickerDialog.OnDateSetListener date2 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
myCalendar1.set(Calendar.YEAR, year);
myCalendar1.set(Calendar.MONTH, month);
myCalendar1.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
private void updateLabel() {
String myFormat = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
check_date.setText(sdf.format(myCalendar1.getTime()));
}
};
check_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(LeaveTran.this, date2, myCalendar1
.get(Calendar.YEAR), myCalendar1.get(Calendar.MONTH),
myCalendar1.get(Calendar.DAY_OF_MONTH)).show();
}
});
Days1 = (EditText) findViewById(R.id.Days1);
textInputDays1 = (TextInputLayout)findViewById(R.id.textInputDays1);
GoingAbroad = (CheckBox) findViewById(R.id.GoingAbroad);
textInputGoingAbroad = (TextInputLayout)findViewById(R.id.textInputGoingAbroad);
final Calendar startDate = Calendar.getInstance();
textInputExpStDt = (TextInputLayout)findViewById(R.id.textInputExpStDt);
ExpStDt = (EditText) findViewById(R.id.ExpStDt);
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
startDate.set(Calendar.YEAR, year);
startDate.set(Calendar.MONTH, month);
startDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
private void updateLabel() {
String myFormat = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
ExpStDt.setText(sdf.format(startDate.getTime()));
}
};
ExpStDt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(LeaveTran.this, date, startDate
.get(Calendar.YEAR), startDate.get(Calendar.MONTH),
startDate.get(Calendar.DAY_OF_MONTH)).show();
String outputDate = addDays(new Date(),6 );
ExpEndDt.setText(outputDate);
}
});
final Calendar endDate = Calendar.getInstance();
textInputExpEndDt = (TextInputLayout)findViewById(R.id.textInputExpEndDt);
ExpEndDt = (EditText) findViewById(R.id.ExpEndDt);
final DatePickerDialog.OnDateSetListener date1 = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
endDate.set(Calendar.YEAR, year);
endDate.set(Calendar.MONTH, month);
endDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
private void updateLabel() {
String myFormat = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
ExpEndDt.setText(sdf.format(endDate.getTime()));
}
};
ExpEndDt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(LeaveTran.this, date1, endDate
.get(Calendar.YEAR), endDate.get(Calendar.MONTH),
endDate.get(Calendar.DAY_OF_MONTH)).show();
}
});
Status = (CheckBox) findViewById(R.id.Status);
textInputStatus = (TextInputLayout)findViewById(R.id.textInputStatus);
ShowApprovedStatus();
addItemsOnSpinner2();
addItemsOnSpinner3();
}
private void addItemsOnSpinner3() {
ReqType = (Spinner)findViewById(R.id.ReqType);
List<String> list3 = new ArrayList<String>();
list3.add("Need Advance Payment");
list3.add("No Advance Payment");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list3);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ReqType.setAdapter(dataAdapter);
}
private void addItemsOnSpinner2() {
LeaveType1 = (Spinner)findViewById(R.id.LeaveType1);
List<String> list = new ArrayList<String>();
list.add("Please Select Leave Type");
list.add("Annual Leave");
list.add("Medical Leave Absence");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
LeaveType1.setAdapter(dataAdapter);
}
private String addDays(Date startDate, int Days) {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
Calendar c = Calendar.getInstance();
try {
c.setTime(dateFormat.parse(String.valueOf(startDate)));
} catch (android.net.ParseException e) {
e.printStackTrace();
} catch (java.text.ParseException e) {
e.printStackTrace();
}
c.add(Calendar.DAY_OF_WEEK,Days);
String resultDate=dateFormat.format(c.getTime());
return resultDate;
}
private long getDaysBetweenDates(String ExpStDt, String ExpEndDt) throws java.text.ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
Date startDate, endDate;
long Days = 0;
try {
startDate = dateFormat.parse(ExpStDt);
endDate = dateFormat.parse(ExpEndDt);
Days = getUnitBetweenDates(startDate, endDate, TimeUnit.DAYS);
} catch (android.net.ParseException e) {
e.printStackTrace();
}
return Days;
}
private long getUnitBetweenDates(Date startDate, Date endDate, TimeUnit Days) {
long timeDiff = startDate.getTime() - endDate.getTime();
return Days.convert(timeDiff, TimeUnit.MILLISECONDS);
}
public void ShowApprovedStatus () {
ShowApprovedStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (validate()){
String date = check_date.getText().toString();
String Leave1 = LeaveType1.getSelectedItem().toString();
String text = LeaveType2.getSelectedItem().toString();
String Day1 = Days1.getText().toString();
String Exp_St_Dt = ExpStDt.getText().toString();
String Exp_End_Dt = ExpEndDt.getText().toString();
String Req_Type = ReqType.getSelectedItem().toString();
String Status1 = Status.toString();
}
boolean isInserted = myDb.insertData(check_date.getText().toString(),ReqType.getSelectedItem().toString(),LeaveType1.getSelectedItem().toString(), LeaveType2.getSelectedItem().toString(),Days1.getText().toString(), ExpStDt.getText().toString(), ExpEndDt.getText().toString(),Status.toString(),GoingAbroad.toString());
if (isInserted == true)
{
Intent intent = new Intent(LeaveTran.this, ListViewActivity.class);
startActivity(intent);
}
else {
Snackbar.make(ShowApprovedStatus, "Data Not Inserted Successfully", Snackbar.LENGTH_LONG).show();
}
}
private boolean validate() {
boolean valid = false;
String date = check_date.getText().toString();
String Leave1 = LeaveType1.getSelectedItem().toString();
String text = LeaveType2.getSelectedItem().toString();
String Day1 = Days1.getText().toString();
String Exp_St_Dt = ExpStDt.getText().toString();
String Exp_End_Dt = ExpEndDt.getText().toString();
String Req_Type = ReqType.getSelectedItem().toString();
String Status1 = Status.toString();
if (date.isEmpty()){
textInputLayoutDate.setError("Please Enter Requested Date");
} else {
valid = true;
textInputLayoutDate.setError(null);
}
if (Leave1.isEmpty()){
valid = false;
textInputLeaveType1.setError("Please Choose your Type of Leave No 1");
} else {
valid = true;
textInputLeaveType1.setError(null);
}
if (text.isEmpty()){
valid = false;
textInputLeaveType2.setError("Please Choose your Type of Leave No 1");
} else {
valid = true;
textInputLeaveType2.setError(null);
}
if (Day1.isEmpty()) {
valid = false;
textInputDays1.setError("Please enter the No. days for Leave Type 1");
} else {
valid = true;
textInputDays1.setError(null);
}
if (Exp_St_Dt.isEmpty()) {
valid = false;
textInputExpStDt.setError("Please Enter The Expected Date Where you want to start Your leave");
} else {
valid = true;
textInputExpStDt.setError(null);
}
if (Exp_End_Dt.isEmpty()) {
valid = false;
textInputExpEndDt.setError("Please Enter the Expected Date Where you want to End Your leave");
} else {
valid = true;
textInputExpEndDt.setError(null);
}
if (Req_Type.isEmpty()) {
valid = false;
textInputReqType.setError("Please Select The Type of Request Leave You Want");
} else {
valid = true;
textInputReqType.setError(null);
}
if (Status1.isEmpty()) {
valid = false;
textInputStatus.setError("The status should not go more than 100");
} else {
valid = true;
textInputStatus.setError(null);
}
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
try {
if(sdf.parse(Exp_St_Dt).before(sdf.parse(Exp_End_Dt)))
{
valid = true;
textInputExpStDt.setError(null);
}
else if(sdf.parse(Exp_St_Dt).equals(sdf.parse(Exp_End_Dt)))
{
valid = false;
textInputExpStDt.setError("The start date should not be same with end date");
textInputExpEndDt.setError("The End Date should not be same as Start Date");
}
else if (sdf.parse(Exp_End_Dt).before(sdf.parse(Exp_St_Dt)))
{
valid = false;
textInputExpEndDt.setError("The End Date should not be less than Start Date");
}
} catch (Exception e) {
e.printStackTrace();
}
return valid;
}
});
}
}
2. ListView.xml
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ListViewActivity">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
3. ListViewActivity.java
LeaveDb myDb;
SQLiteDatabase SQLITEDATABASE;
Cursor cursor;
SQLiteListAdapter ListAdapter ;
ArrayList<string> ReqDate_ArrayList = new ArrayList<string>();
ArrayList<string> ReqType_ArrayList = new ArrayList<string>();
ArrayList<string> LeaveType1_ArrayList = new ArrayList<string>();
ArrayList<string> LeaveType2_ArrayList = new ArrayList<string>();
ArrayList<string> Days1_ArrayList = new ArrayList<string>();
ArrayList<string> ExpStDt_ArrayList = new ArrayList<string>();
ArrayList<string> ExpEndDt_ArrayList = new ArrayList<string>();
ArrayList<string> Status_ArrayList = new ArrayList<string>();
ArrayList<string> GoingAbroad_ArrayList = new ArrayList<string>();
ListView LISTVIEW;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
LISTVIEW = (ListView) findViewById(R.id.listView1);
myDb = new LeaveDb(this);
}
@Override
protected void onResume() {
ShowSQLiteDBdata() ;
super.onResume();
}
private void ShowSQLiteDBdata() {
SQLITEDATABASE = myDb.getWritableDatabase();
cursor = SQLITEDATABASE.rawQuery("SELECT * FROM LeaveTran", null);
ReqDate_ArrayList.clear(); // 1
ReqType_ArrayList.clear(); // 2
LeaveType1_ArrayList.clear(); // 3
LeaveType2_ArrayList.clear(); // 4
Days1_ArrayList.clear(); //5
ExpStDt_ArrayList.clear(); //6
ExpEndDt_ArrayList.clear(); //7
Status_ArrayList.clear(); //8
GoingAbroad_ArrayList.clear(); //9
if (cursor.moveToFirst()) {
do {
ReqDate_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_ReqDate))); // 1
ReqType_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_ReqType))); // 2
LeaveType1_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_LeaveType1))); // 3
LeaveType2_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_LeaveType2))); // 4
Days1_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_Days1))); // 5
ExpStDt_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_ExpStDt))); // 6
ExpEndDt_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_ExpEndDt))); // 7
Status_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_Status))); // 8
GoingAbroad_ArrayList.add(cursor.getString(cursor.getColumnIndex(myDb.KEY_GoingAbroad))); //9
} while (cursor.moveToNext());
}
ListAdapter = new SQLiteListAdapter(ListViewActivity.this,
ReqDate_ArrayList, //1
ReqType_ArrayList, //2
LeaveType1_ArrayList, //3
LeaveType2_ArrayList, //4
Days1_ArrayList, //5
ExpStDt_ArrayList, //6
ExpStDt_ArrayList, //7
ExpEndDt_ArrayList, // 8
Status_ArrayList, // 9
GoingAbroad_ArrayList // 10
);
LISTVIEW.setAdapter(ListAdapter);
cursor.close();
}
4. listdatalayout.xml
<relativelayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
="" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin">
<textview
android:id="@+id/textView1"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:text="ReqDate = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textViewID"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView1" android:layout_alignbottom="@+id/textView1" android:layout_torightof="@+id/textView1" android:text="ReqDate" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView2"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textView1" android:text="ReqType = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textViewNAME"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textViewID" android:layout_torightof="@+id/textView2" android:text="ReqType" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView3"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textView2" android:text="LeaveType1 = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textViewPHONE_NUMBER"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/textView3" android:layout_torightof="@+id/textView3" android:text="LeaveType1" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView4"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_below="@+id/textView3" android:text="LeaveType2 = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textViewSUBJECT"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView4" android:layout_alignbottom="@+id/textView4" android:layout_torightof="@+id/textView4" android:text="LeaveType2" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView6"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView3" android:layout_alignparentleft="true" android:layout_marginleft="18dp" android:layout_margintop="38dp" android:text="Days1 = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textViewDays1"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView6" android:layout_alignbottom="@+id/textView6" android:layout_marginleft="1dp" android:layout_marginbottom="3dp" android:layout_torightof="@+id/textView4" android:text="Days1" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView7"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView4" android:layout_alignparentleft="true" android:layout_marginleft="18dp" android:layout_margintop="38dp" android:text="ExpStDt = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textViewExpStDt"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView7" android:layout_alignbottom="@+id/textView7" android:layout_marginleft="1dp" android:layout_marginbottom="3dp" android:layout_torightof="@+id/textView4" android:text="ExpStDt" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView8"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView6" android:layout_alignparentleft="true" android:layout_marginleft="18dp" android:layout_margintop="38dp" android:text="ExpEndDt = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textViewExpEndDt"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView8" android:layout_alignbottom="@+id/textView8" android:layout_marginleft="1dp" android:layout_marginbottom="3dp" android:layout_torightof="@+id/textView4" android:text="ExpEndDt" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView9"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView7" android:layout_alignparentleft="true" android:layout_marginleft="18dp" android:layout_margintop="38dp" android:text="Status = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textViewStatus"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView9" android:layout_alignbottom="@+id/textView9" android:layout_marginleft="1dp" android:layout_marginbottom="3dp" android:layout_torightof="@+id/textView4" android:text="Status" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textView10"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView8" android:layout_alignparentleft="true" android:layout_marginleft="18dp" android:layout_margintop="38dp" android:text="Status = " android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
<textview
android:id="@+id/textViewGoingAbroad"
="" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textView10" android:layout_alignbottom="@+id/textView10" android:layout_marginleft="1dp" android:layout_marginbottom="3dp" android:layout_torightof="@+id/textView4" android:text="Status" android:textappearance="?android:attr/textAppearanceMedium" android:textcolor="#050505">
This is what the shows in my Logcat:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.huzai.listview.LeaveTran.ShowApprovedStatus(LeaveTran.java:312)
at com.example.huzai.listview.LeaveTran.onCreate(LeaveTran.java:220)