我试图将MySQL数据显示在表中,而不是列表视图中,但是无法并且我不知道该怎么做。
我的代码工作得很好,它返回一个包含数据的listeview,现在我只希望这些数据在一个表中,而不是列表视图中
是否有方法在表中显示列表视图?或者将列表视图转换成表?
我的mainActivity.java

public class MainActivity extends AppCompatActivity {

    ProgressDialog pDialog;
    JSONParser jParser = new JSONParser();
    ListView lv;
    ArrayList<HashMap<String, String>> productsList;

    private static String url_all_products "http://192.168.1.13/member/all.php";
    JSONArray products = null;
    int error=0;
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "temperaturedata";
    private static final String TAG_PID = "id";
    private static final String TAG_DATEANDTIME = "dateandtime";
    private static final String TAG_CIN = "cin";
    private static final String TAG_TEMPERATURE = "temperature";
    private static final String TAG_HUMIDITY = "humidity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        productsList = new ArrayList<HashMap<String, String>>();
        // Get listview
        lv = (ListView)findViewById(R.id.listView);
        new LoadAllProducts().execute();
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

            }
        });

    }

    class LoadAllProducts extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {
            HashMap<String, String> params = new HashMap<String, String>();

            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

            Log.d("All Products: ", json.toString());
            int success = 0;
            try {
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    products = json.getJSONArray(TAG_PRODUCTS);
                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

                        String id = c.getString(TAG_PID);
                        String dateandtime = c.getString(TAG_DATEANDTIME);
                        String cin = c.getString(TAG_CIN);
                        String temperature=c.getString(TAG_TEMPERATURE);
                        String humidity = c.getString(TAG_HUMIDITY);

                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put(TAG_PID, id);
                        map.put(TAG_DATEANDTIME, dateandtime);
                        map.put(TAG_CIN,cin);
                        map.put(TAG_TEMPERATURE,temperature);
                        map.put(TAG_HUMIDITY,humidity);

                        productsList.add(map);
                    }
                } else {
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }



            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();

            ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, productsList,
                    R.layout.item, new String[] { TAG_PID,
                    TAG_DATEANDTIME,TAG_CIN,TAG_TEMPERATURE,TAG_HUMIDITY},
                    new int[] { R.id.pid, R.id.dateandtime,R.id.cin,R.id.temperature,R.id.humidity});
            lv.setAdapter(adapter);

        }

    }
}


我的活动主要.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

和my item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:weightSum="4">

    <TextView
        android:id="@+id/pid"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:visibility="gone" />


    <TextView
        android:id="@+id/dateandtime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/cin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/temperature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/humidity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

最佳答案

嗨,我向你推荐这个图书馆
TableView是一个强大的Android库,用于显示复杂的数据结构和呈现由行、列和单元格组成的表格数据。TableView依赖于一个单独的模型对象来保存和表示它显示的数据。此存储库还包含一个示例应用程序,该应用程序旨在向您展示如何在应用程序中创建自己的TableView。
这是链接
https://github.com/evrencoskun/TableView

关于android - 如何使用Android在表中显示MySQL数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56187250/

10-11 00:02
查看更多