如何使用onclick按钮循环显示到特定日期的员工时间表?

时间:2018-12-09 05:39:23

标签: android android-studio android-layout

我正在android中创建概念,它将向此应用程序的用户(其中​​一名员工)显示员工的工作时间。首先,我将两个日期发送到php文件中,以检索两个日期之间的数据,即工作时间的开始和结束时间。

这个概念在php文件中运行良好,我能够检索数据。

但是,当我尝试显示时,我面临三个问题。

第一次约会-2018年9月12日
最后日期-14-12-18

  • 在特定日期(2018年12月9日-第一个日期)中,开始时间和结束时间不存在,php文件为此日期发送null,但下一个日期结束日期具有值(2018年10月12日至14-12-18(最后日期))。

使用计数概念,我正在显示员工的工作时间。我发送计数0以显示第一个日期,但它是null,因此它不会显示任何内容,因为我尝试在if else循环中使用for部分,当{ {1}}部分被执行,应用被压碎。

  • 当每个日期都具有值时,该概念将起作用,但是将为每行显示索引名称。 (我已附上输出屏幕截图)。

当单击下一个按钮时,它应删除9-12-2018的数据,并显示10-12-2018的数据,但显示在同一页面上。

我用过

else

删除以前的数据,但是当我在stk.removeView(tbrow); stk.removeView(tbrow0); 函数的开头使用此数据时,每次调用该函数时都会删除前一行,并且仅显示一个员工的工作时间。

这是我的活动文件:

init

无需单击下一步按钮即可输出:

output without clicking next button

在单击下一步按钮时:

On clicking next button

package com.example.myapplication; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Gravity; import android.view.View; import android.widget.Button; import android.widget.GridLayout; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; import android.widget.Toast; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; public class Colleagues_Schedule extends AppCompatActivity { int i; String result1; int size = 20; Context context; int Count = 0; int datecount = 1; Integer Nmaxclicks = 3; Integer Ncurrentnumber = 0; Integer Pmaxclicks = 4; Integer Pcurrentnumber = 0; Button Next, Previous; JSONArray jsonArray_New = null; String[] ScheduleDates, StartTimes, Endtimes, Names; String ScheduleDate, StartTime, Endtime, Today, Tomarrow, Name; TextView dat; private ArrayList < String > ScheduleDateArray = new ArrayList < > (); private ArrayList < String > StartTimeArray = new ArrayList < > (); private ArrayList < String > EndtimeArray = new ArrayList < > (); private ArrayList < String > NameArray = new ArrayList < > (); TableRow tbrow0, tbrow; DateFormat TW; TableLayout stk; Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD); GridLayout mainGrid2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_colleagues__schedule); Next = (Button) findViewById(R.id.next); Previous = (Button) findViewById(R.id.back); DateFormat TO = new SimpleDateFormat("yyyy-MM-dd"); Today = TO.format(Calendar.getInstance().getTime()); TW = new SimpleDateFormat("yyyy-MM-dd"); result1 = getIntent().getStringExtra("Colleagues_Schedule"); Next.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (Ncurrentnumber == Nmaxclicks) { Next.setEnabled(false); } else { stk.removeView(tbrow); stk.removeView(tbrow0); Count++; dateloop(Count); Ncurrentnumber = Ncurrentnumber + 1; } } }); Previous.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (Pcurrentnumber == Pmaxclicks) { Previous.setEnabled(false); } else { stk.removeView(tbrow); stk.removeView(tbrow0); Count--; dateloop(Count); Pcurrentnumber = Pcurrentnumber + 1; } } }); dateloop(Count); } private Date Tomarrow(int datecount) { final Calendar cal = Calendar.getInstance(); cal.add(Calendar.DATE, +datecount); return cal.getTime(); } private void dateloop(int count) { switch (count) { case 0: Set_Schedule(Today); break; case 1: datecount = 1; Tomarrow(datecount); Tomarrow = TW.format(Tomarrow(datecount)); Set_Schedule(Tomarrow); break; case 2: datecount = 2; Tomarrow(datecount); Tomarrow = TW.format(Tomarrow(datecount)); Set_Schedule(Tomarrow); break; case 3: datecount = 3; Tomarrow(datecount); Tomarrow = TW.format(Tomarrow(datecount)); Set_Schedule(Tomarrow); break; case -1: datecount = -1; Tomarrow(datecount); Tomarrow = TW.format(Tomarrow(datecount)); Set_Schedule(Tomarrow); break; default: Toast.makeText(Colleagues_Schedule.this, "UPDATE COMING SOON" + Tomarrow, Toast.LENGTH_LONG).show(); break; } } public void Set_Schedule(String tomarrow) { try { JSONObject jsonObjects = new JSONObject(result1); jsonArray_New = jsonObjects.getJSONArray("Colleagues_Schedule"); try { for (int i = 0; i < jsonArray_New.length(); i++) { JSONObject jsonObject = jsonArray_New.getJSONObject(i); Name = jsonObject.getString("Name"); NameArray.add(Name); ScheduleDate = jsonObject.getString("ScheduleDate"); ScheduleDateArray.add(ScheduleDate); StartTime = jsonObject.getString("StartTime"); StartTimeArray.add(StartTime); Endtime = jsonObject.getString("Endtime"); EndtimeArray.add(Endtime); } } catch (JSONException e) { e.printStackTrace(); } //Initilize the arraylist to array Names = new String[NameArray.size()]; ScheduleDates = new String[ScheduleDateArray.size()]; StartTimes = new String[StartTimeArray.size()]; Endtimes = new String[EndtimeArray.size()]; for (int i = 0; i < ScheduleDateArray.size(); i++) { ScheduleDates[i] = ScheduleDateArray.get(i); if (tomarrow.equals(ScheduleDates[i])) { // Toast.makeText(Colleagues_Schedule.this ,"ddda"+ ScheduleDates[i], Toast.LENGTH_LONG).show(); Names[i] = NameArray.get(i); ScheduleDates[i] = ScheduleDateArray.get(i); StartTimes[i] = StartTimeArray.get(i); Endtimes[i] = EndtimeArray.get(i); init(Names[i], ScheduleDates[i], StartTimes[i], Endtimes[i]); } } } catch (JSONException e) { e.printStackTrace(); } } @SuppressLint("ResourceType") public void init(String name, String scheduleDate, String startTime, String endtime) { /* if(stk!=null){ stk.removeView(tbrow); stk.removeView(tbrow0); }*/ tbrow0 = new TableRow(this); stk = (TableLayout) findViewById(R.id.table_main); tbrow0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0 f)); TextView tv0 = new TextView(this); tv0.setText("Name"); tv0.setTextColor(Color.WHITE); tv0.setGravity(Gravity.CENTER | Gravity.BOTTOM); tv0.setTypeface(boldTypeface); tv0.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0 f)); tbrow0.addView(tv0); TextView tv1 = new TextView(this); tv1.setText("Date"); //name tv1.setTextColor(Color.WHITE); tv1.setGravity(Gravity.CENTER | Gravity.BOTTOM); tv1.setTypeface(boldTypeface); tv1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0 f)); tbrow0.addView(tv1); TextView tv2 = new TextView(this); tv2.setText("Start Time"); tv2.setGravity(Gravity.CENTER | Gravity.BOTTOM); tv2.setTextColor(Color.WHITE); tv2.setTypeface(boldTypeface); tv2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0 f)); tbrow0.addView(tv2); TextView tv3 = new TextView(this); tv3.setText("End Time"); tv3.setGravity(Gravity.CENTER | Gravity.BOTTOM); tv3.setTextColor(Color.WHITE); tv3.setTypeface(boldTypeface); tv3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0 f)); tbrow0.addView(tv3); //added layout params and weight stk.addView(tbrow0, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); tbrow = new TableRow(this); tbrow.setBackgroundResource(R.drawable.row_border); //added layout params and weight tbrow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0 f)); TextView t1v = new TextView(this); TextView t2v = new TextView(this); TextView t3v = new TextView(this); TextView t4v = new TextView(this); tbrow.setBackgroundResource(R.drawable.row_border); t1v.setText(name); t1v.setTextColor(Color.WHITE); t1v.setGravity(Gravity.CENTER); t1v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0 f)); tbrow.addView(t1v); t2v.setText(scheduleDate); t2v.setTextColor(Color.WHITE); t2v.setGravity(Gravity.CENTER); t2v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0 f)); tbrow.addView(t2v); t3v.setText(startTime); t3v.setTextColor(Color.WHITE); t3v.setGravity(Gravity.CENTER); t3v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0 f)); tbrow.addView(t3v); t4v.setText(endtime); t4v.setTextColor(Color.WHITE); t4v.setGravity(Gravity.CENTER); t4v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0 f)); tbrow.addView(t4v); stk.addView(tbrow, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); tbrow.setClickable(true); //allows you to select a specific row tbrow.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { TableRow tablerow = (TableRow) view; TextView sample = (TextView) tablerow.getChildAt(1); String result = sample.getText().toString(); Toast toast = Toast.makeText(Colleagues_Schedule.this, result, Toast.LENGTH_LONG); toast.show(); } }); } } //end of main

Execute with  stk.removeView(tbrow);stk.removeView(tbrow0);

任何人都可以帮助我解决这两个问题。

0 个答案:

没有答案