Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 单击“下一步”按钮时,如何清除版面中以前的内容?_Android_Android Studio_Android Tablelayout - Fatal编程技术网

Android 单击“下一步”按钮时,如何清除版面中以前的内容?

Android 单击“下一步”按钮时,如何清除版面中以前的内容?,android,android-studio,android-tablelayout,Android,Android Studio,Android Tablelayout,我创建的概念是在每个布局中按日期顺序显示员工工时列表 stk.removeView(tbrow); stk.removeView(tbrow0); 我已经创建了代码,并且部分工作正常,因为我无法清除布局中的所有内容 stk.removeView(tbrow); stk.removeView(tbrow0); 我曾经 stk.removeView(tbrow); stk.removeView(tbrow0); 删除表行中的内容,但由于它仅清除部分行并显示行的其余部分,因此无法正常工作 stk

我创建的概念是在每个布局中按日期顺序显示员工工时列表

stk.removeView(tbrow);
stk.removeView(tbrow0);
我已经创建了代码,并且部分工作正常,因为我无法清除布局中的所有内容

stk.removeView(tbrow);
stk.removeView(tbrow0);
我曾经

stk.removeView(tbrow);
stk.removeView(tbrow0);
删除表行中的内容,但由于它仅清除部分行并显示行的其余部分,因此无法正常工作

stk.removeView(tbrow);
stk.removeView(tbrow0);
我无法理解为什么会发生这种情况,我试图清除所有的数组列表,但不起作用

stk.removeView(tbrow);
stk.removeView(tbrow0);
这是我的密码

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
我得到这样的输出

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

请任何人帮助我解决这个问题。

在您的情况下,我建议您使用ListView或RecyclerView而不是TableLayout

好的,兄弟,但我完全不知道RecyclerView。您能提供任何示例代码,以便我可以更改代码吗
stk.removeView(tbrow);
stk.removeView(tbrow0);