Java 在表listview中添加标题

Java 在表listview中添加标题,java,android,xml,listview,header,Java,Android,Xml,Listview,Header,我需要帮助在我的表listview中创建标题,我每次都尝试了,标题和表列不一样,我的意思是不太整洁 以下是我的xml代码: activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=&qu

我需要帮助在我的表listview中创建标题,我每次都尝试了,标题和表列不一样,我的意思是不太整洁

以下是我的xml代码:

activity_main.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"
    tools:context=".MainActivity">
    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp">
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />
    </HorizontalScrollView>
    </RelativeLayout>

下面是apk的示例

你们能给我一个解决方案或例子吗?
我很感激为你一行中的每一个
文本视图添加一些权重

android:layout_weight="1"
这将使所有视图的宽度相等,并且所有视图都适合内部
LinearLayout
。您还可以添加权重=2或更多,以便为其中一列添加更多空间


或者使用,或者更好,这个类旨在构建这样的结构(表)

我想这页已经有你的答案了,谢谢。我能再问你一次吗?您知道如何在listview中降序数据吗?也许
setStackFromBottom
方法适合您的需要,请登录。或者在适配器中颠倒数据顺序
package com.example.movie;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String JSON_URL = "http://hrd.tvip.co.id/rest_server/api/absensi/index?shift_day=2020-08-24&shift_day_2=2020-08-31&badgenumber=" + "0100018600";

    ListView list;
    private List<MovieItem> movieItemList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = findViewById(R.id.list);
        movieItemList = new ArrayList<>();

        loadPlayer();
    }

        private void loadPlayer() {
            StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                            try {
                                JSONObject obj = new JSONObject(response);
                                JSONArray movieArray = obj.getJSONArray("data");

                                for (int i = 0; i < movieArray.length(); i++) {

                                    JSONObject movieObject = movieArray.getJSONObject(i);

                                    MovieItem movieItem = new MovieItem(
                                            movieObject.getString("F1"),
                                            movieObject.getString("depo_f1"),
                                            movieObject.getString("F4"),
                                            movieObject.getString("depo_f4"),
                                            movieObject.getString("ket_absensi"));

                                    movieItemList.add(movieItem);
                                }

                                ListViewAdapter adapter = new ListViewAdapter(movieItemList, getApplicationContext());

                                list.setAdapter(adapter);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    });

            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);
        }
    }
package com.example.movie;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;

public class ListViewAdapter extends ArrayAdapter<MovieItem> {

    private List<MovieItem> movieItemList;

    private Context context;

    public ListViewAdapter(List<MovieItem> movieItemList, Context context) {
        super(context, R.layout.list_item, movieItemList);
        this.movieItemList = movieItemList;
        this.context = context;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(context);

        View listViewItem = inflater.inflate(R.layout.list_item, null, true);

        TextView depof1 = listViewItem.findViewById(R.id.depof1);
        TextView f1 = listViewItem.findViewById(R.id.f1);
        TextView depof4 = listViewItem.findViewById(R.id.depof4);
        TextView f4 = listViewItem.findViewById(R.id.f4);
        TextView telat = listViewItem.findViewById(R.id.telat);

        MovieItem movieItem = movieItemList.get(position);

        depof1.setText(movieItem.getF1());
        f1.setText(movieItem.getDepof1());
        depof4.setText(movieItem.getF4());
        f4.setText(movieItem.getDepof4());
        telat.setText(movieItem.getKeterangan());

        return listViewItem;
    }
}
package com.example.movie;

import java.io.Serializable;

public class MovieItem implements Serializable {
    String depof1, F1, depof4, F4, keterangan;

    public MovieItem(String F1, String depof1, String F4, String depof4, String keterangan) {
        this.F1 = F1;
        this.F4 = F4;
        this.depof1 = depof1;
        this.depof4 = depof4;
        this.keterangan = keterangan;
    }

    public String getF1() {
        return F1;
    }

    public String getDepof1() {
        return depof1;
    }

    public String getF4() { return F4; }

    public String getDepof4() {
        return depof4;
    }

    public String getKeterangan() {
        return keterangan;
    }

}
android:layout_weight="1"