如何在 android 中的片段中刷新表格布局

How can I refresh tablelayout within fragment in android

我是 Android 开发新手。我在这里删除了不必要的代码,但是对 Rest API 的调用没有问题。我这里有一个片段,其中有一个 table 布局。在异步任务中调用 Rest API,然后应该将检索到的数据插入 table 并刷新它。问题是 table 收到数据后不显示。有人可以指出正确的方向来解决这个问题吗?谢谢!

public class TransactionsFragment extends Fragment {
    final String urlString = "https:/exampleurl.net/restapi/Handler.ashx";
    String cnoKey = "com.example.app.saveCredentials.cno";
    String aliasnameKey = "com.example.app.saveCredentials.aliasname";
    SharedPreferences prefs;
    LayoutInflater inflaterGlobal;
    ViewGroup containerGlobal;

    List<String> recids;
    List<String> folders;
    List<String> whotos;
    List<String> directions;
    List<String> documents;
    List<String> thedates;

    View rootView;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_transactions, container, false);
        inflaterGlobal = inflater;
        containerGlobal = container;

        TableLayout ll = (TableLayout) rootView.findViewById(R.id.tableLayout1);

        prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
        String aliasname = prefs.getString(aliasnameKey, null);

        new TestAsync(aliasname, "", getActivity()).execute();

        return rootView;
    }

    class TestAsync extends AsyncTask<Void, Integer, String>
    {
        private String aliasname;
        private String password;
        private String value; //success value
        private String recid; //success recid
        private String folder;
        private String whoto;
        private String direction;
        private String document;
        private String thedate;
        private ProgressDialog progressDialog;
        private Activity activity;

        public TestAsync(String aliasname, String password, Activity activity) {
            super();
            this.password = password;
            this.aliasname = aliasname;
            this.activity = activity;
        }
        protected void onPreExecute() {
            System.out.println("PreExecute");
            progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);

        }

        protected String doInBackground(Void... arg0) {

            try {

                JSONObject result = GetTranReport(aliasname, "False", "1", "03", "2015");


                recids = new ArrayList<>();
                folders = new ArrayList<>();
                whotos = new ArrayList<>();
                directions = new ArrayList<>();
                documents = new ArrayList<>();
                thedates = new ArrayList<>();


                JSONObject jsonObject;
                JSONArray jsonArray = result.getJSONArray("Value");
                for (int i = 0; i < jsonArray.length()-1; i++) {
                    System.out.println("i = " + i);
                    jsonObject = jsonArray.getJSONObject(i);
                    System.out.println("recid = " + jsonObject.getString("recid"));
                    recids.add(jsonObject.getString("recid"));
                    folders.add(jsonObject.getString("folder"));
                    whotos.add(jsonObject.getString("whoto"));
                    directions.add(jsonObject.getString("direction"));
                    documents.add(jsonObject.getString("document"));
                    thedates.add(jsonObject.getString("thedate"));
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
            return "You are at PostExecute";
        }

        protected void onProgressUpdate(Integer... a) {
            System.out.println("Progress Update" + a[0]);
        }

        protected void onPostExecute(String result) {
            //handleLogin(value);
            System.out.println("" + result);
            View rootView = inflaterGlobal.inflate(R.layout.fragment_transactions, containerGlobal, false);
            //getActivity().recreate();

            TableLayout ll = (TableLayout) rootView.findViewById(R.id.tableLayout1);

            prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
            String aliasname = prefs.getString(aliasnameKey, null);
            System.out.println("carrying out table creation");
            if (whotos.size() > 0) {
                for (int i = 0; i < whotos.size(); i++) {
                    System.out.println("" +i);
                    TableRow row = new TableRow(getActivity());
                    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
                    row.setLayoutParams(lp);
                    TextView tv = new TextView(getActivity());
                    tv.setText(whotos.get(i));
                    TextView tv2 = new TextView(getActivity());
                    tv2.setText(documents.get(i));
                    TextView tv3 = new TextView(getActivity());
                    tv3.setText(directions.get(i));
                    TextView tv4 = new TextView(getActivity());
                    tv4.setText(thedates.get(i));
                    row.addView(tv); //TP name
                    row.addView(tv2); //document type
                    row.addView(tv3); //direction
                    row.addView(tv4); //date

                    ll.addView(row, i+3);
                }
                ll.invalidate();
                ll.refreshDrawableState();
            }

            progressDialog.dismiss();
        }

    }

这是fragment_transactions.xml代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <TableRow
            android:gravity="center"
            android:id="@+id/tableRow1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_gravity="center_horizontal"
                android:id="@+id/textView1"
                android:text="Transaction Report"
                android:layout_span="4"
                android:textAppearance="?android:attr/textAppearanceLarge"
                tools:ignore="ObsoleteLayoutParam" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip"
            android:weightSum="4">

            <TextView
                android:id="@+id/tp"
                android:layout_weight="1"
                android:text="TP"
                android:textAppearance="?android:attr/textAppearanceLarge" />
            <TextView
                android:id="@+id/doc"
                android:layout_weight="1"
                android:text="Doc"
                android:textAppearance="?android:attr/textAppearanceLarge" />
            <TextView
                android:id="@+id/dir"
                android:layout_weight="1"
                android:text="Direction"
                android:textAppearance="?android:attr/textAppearanceLarge" />
            <TextView
                android:id="@+id/date"
                android:layout_weight="1"
                android:text="Date"
                android:textAppearance="?android:attr/textAppearanceLarge" />
        </TableRow>

        <!-- draw a red line -->
        <View
            android:layout_height="2dip"
            android:background="#FF0000" />

    </TableLayout>
</FrameLayout>

这样试试

public class TransactionsFragment extends Fragment {
    final String urlString = "https:/exampleurl.net/restapi/Handler.ashx";
    String cnoKey = "com.example.app.saveCredentials.cno";
    String aliasnameKey = "com.example.app.saveCredentials.aliasname";
    SharedPreferences prefs;
    LayoutInflater inflaterGlobal;
    ViewGroup containerGlobal;

    List<String> recids;
    List<String> folders;
    List<String> whotos;
    List<String> directions;
    List<String> documents;
    List<String> thedates;

    View rootView;
    TableLayout ll;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_transactions, container, false);
        inflaterGlobal = inflater;
        containerGlobal = container;

        ll = (TableLayout) rootView.findViewById(R.id.tableLayout1);

        prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
        String aliasname = prefs.getString(aliasnameKey, null);

        new TestAsync(aliasname, "", getActivity()).execute();

        return rootView;
    }

    class TestAsync extends AsyncTask<Void, Integer, String> {
        private String aliasname;
        private String password;
        private String value; //success value
        private String recid; //success recid
        private String folder;
        private String whoto;
        private String direction;
        private String document;
        private String thedate;
        private ProgressDialog progressDialog;
        private Activity activity;

        public TestAsync(String aliasname, String password, Activity activity) {
            super();
            this.password = password;
            this.aliasname = aliasname;
            this.activity = activity;
        }

        protected void onPreExecute() {
            System.out.println("PreExecute");
            progressDialog = ProgressDialog.show(activity, "Loading", "Loading", true);

        }

        protected String doInBackground(Void... arg0) {

            try {

                JSONObject result = GetTranReport(aliasname, "False", "1", "03", "2015");


                recids = new ArrayList<>();
                folders = new ArrayList<>();
                whotos = new ArrayList<>();
                directions = new ArrayList<>();
                documents = new ArrayList<>();
                thedates = new ArrayList<>();


                JSONObject jsonObject;
                JSONArray jsonArray = result.getJSONArray("Value");
                for (int i = 0; i < jsonArray.length() - 1; i++) {
                    System.out.println("i = " + i);
                    jsonObject = jsonArray.getJSONObject(i);
                    System.out.println("recid = " + jsonObject.getString("recid"));
                    recids.add(jsonObject.getString("recid"));
                    folders.add(jsonObject.getString("folder"));
                    whotos.add(jsonObject.getString("whoto"));
                    directions.add(jsonObject.getString("direction"));
                    documents.add(jsonObject.getString("document"));
                    thedates.add(jsonObject.getString("thedate"));
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
            return "You are at PostExecute";
        }

        protected void onProgressUpdate(Integer... a) {
            System.out.println("Progress Update" + a[0]);
        }

        protected void onPostExecute(String result) {
            //handleLogin(value);
            System.out.println("" + result);

            prefs = getActivity().getPreferences(Context.MODE_PRIVATE);
            String aliasname = prefs.getString(aliasnameKey, null);
            System.out.println("carrying out table creation");
            if (whotos.size() > 0) {
                for (int i = 0; i < whotos.size(); i++) {
                    System.out.println("" + i);
                    TableRow row = new TableRow(activity);
                    TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
                    row.setLayoutParams(lp);
                    TextView tv = new TextView(getActivity());
                    tv.setText(whotos.get(i));
                    TextView tv2 = new TextView(getActivity());
                    tv2.setText(documents.get(i));
                    TextView tv3 = new TextView(getActivity());
                    tv3.setText(directions.get(i));
                    TextView tv4 = new TextView(getActivity());
                    tv4.setText(thedates.get(i));
                    row.addView(tv); //TP name
                    row.addView(tv2); //document type
                    row.addView(tv3); //direction
                    row.addView(tv4); //date

                    ll.addView(row, i + 3);
                }
                ll.invalidate();
                ll.refreshDrawableState();
            }

            progressDialog.dismiss();
        }

    }
}