我正在创建一种概念,以便在每个布局中按日期顺序显示员工小时列表。
我已经创建了代码,但由于无法清除布局中的所有内容,因此它可以部分工作。
我用过
stk.removeView(tbrow);
stk.removeView(tbrow0);
要删除表格行中的内容,但由于它仅清除了部分行并显示了其余行,因此无法正常工作。
我不明白为什么会这样,我也尝试清除所有数组列表,但不起作用。
这是我的代码
package com.example.myapplication;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.InflateException;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.LinearLayout;
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.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
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;
LinearLayout linearLayout;
int datecount=1;
Integer Nmaxclicks = 3;
Integer Ncurrentnumber = 0;
Integer Pmaxclicks = 4;
Integer Pcurrentnumber = 0;
TextView t1v,t2v,t3v,t4v;
Button Next,Previous;
JSONArray jsonArray_New = null;
String[] ScheduleDates, StartTimes, Endtimes,Names;
String ScheduleDate,StartTime,Endtime,Today,Tomarrow,Name,Password,Date1;
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);
linearLayout=(LinearLayout) findViewById(R.id.schedule);
DateFormat TO = new SimpleDateFormat("yyyy-MM-dd");
Today = TO.format(Calendar.getInstance().getTime());
TW = new SimpleDateFormat("yyyy-MM-dd");
call();
Next.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(stk!=null)
{ ScheduleDate=null;
StartTime=null;
Endtime=null;
Today=null;
Tomarrow=null;
Name=null;
Password=null;
Date1=null;
NameArray.clear();
ScheduleDateArray.clear();
StartTimeArray.clear();
EndtimeArray.clear();
stk.removeView(tbrow);
stk.removeView(tbrow0);
}
if (Ncurrentnumber == Nmaxclicks)
{
Next.setEnabled(false);
} else
{
Count++;
//Toast.makeText(context , "result" + datecount, Toast.LENGTH_LONG).show();
Ncurrentnumber = Ncurrentnumber + 1;
Tomarrow(datecount);
Tomarrow = TW.format(Tomarrow(datecount));
Date1=Tomarrow;
Emp_Hour b = new Emp_Hour();
b.execute(Name, Password, Date1);
datecount++;
}
} });
Previous.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
} });
}
private Date Tomarrow(int datecount)
{
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, +datecount);
return cal.getTime();
}
private void call()
{
context = getApplicationContext();
SharedPreferences mPrefs = context.getSharedPreferences("identifier", Context.MODE_PRIVATE);
Name = mPrefs.getString("Name", null);
Password = mPrefs.getString("Password", null);
Emp_Hour b = new Emp_Hour();
Date1=Today;
//Toast.makeText(context , "result" + Name + Password+ Today, Toast.LENGTH_LONG).show();
b.execute(Name, Password, Date1);
}
class Emp_Hour extends AsyncTask<String, String, String>
{
@Override
protected String doInBackground(String... params)
{
String name = params[0];
String password = params[1];
String today = params[2];
String data="";
int tmp;
try
{
URL url = new URL(Config_URLS.TAG_COLLEAGUES_SCHEDULE);
String urlParams = "&Name="+name+"&Password="+password +"&Today="+today;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1)
{
data+= (char)tmp;
}
is.close();
httpURLConnection.disconnect();
return data;
} catch (MalformedURLException e)
{
e.printStackTrace();
return "Exception: "+e.getMessage();
} catch (IOException e)
{
e.printStackTrace();
return "Exception: "+e.getMessage();
}
}
@Override
protected void onPostExecute(String result)
{
if(result!=null)
{
Set_index();
Set_Schedule(result);
}else
{
Toast.makeText(context , "result null", Toast.LENGTH_LONG).show();
}
}
}
private void Set_Schedule(String result)
{
try
{
JSONObject jsonObjects = new JSONObject(result);
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++)
{
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();
}
}
private void Set_index()
{
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.0f));
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.0f));
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.0f));
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.0f));
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.0f));
tbrow0.addView(tv3);
stk.addView(tbrow0, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
}
@SuppressLint("ResourceType")
public void init(String name, String scheduleDate, String startTime, String endtime)
{
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.0f));
t1v = new TextView(this);
t2v = new TextView(this);
t3v = new TextView(this);
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.0f));
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.0f));
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.0f));
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.0f));
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
我正在得到这样的输出
Before clicking next button employee hours showed for 18-12-18
clicking next button for 1st time it must show only employee hours for 19-12-18
clicking next button for 2nd time it must show only employee hours for 20-12-18
请任何人帮助我解决此问题。
答案 0 :(得分:1)
在您的情况下,建议您使用ListView或RecyclerView代替TableLayout