在表listview中添加头

rkkpypqq  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(401)

我需要帮助使标题在我的表listview,我试过,每次,标题是不一样的表列,我的意思是不那么整齐。。。
以下是我的xml代码:
活动\u 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>

列表项.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:id="@+id/listItem"
    android:orientation="horizontal">
        <TextView
            android:textColor="#000000"
            android:text="Lokasi Berangkat"
            android:id="@+id/depof1"
            android:gravity="center"
            android:layout_width="150dp"
            android:layout_height="wrap_content"/>
        <TextView
            android:textColor="#000000"
            android:text="Berangkat"
            android:id="@+id/f1"
            android:gravity="center"
            android:layout_width="100dp"
            android:layout_height="wrap_content"/>
        <TextView
            android:textColor="#000000"
            android:text="Lokasi Pulang"
            android:id="@+id/depof4"
            android:gravity="center"
            android:layout_width="150dp"
            android:layout_height="wrap_content"/>
        <TextView
            android:textColor="#000000"
            android:text="Pulang"
            android:id="@+id/f4"
            android:gravity="center"
            android:layout_width="100dp"
            android:layout_height="wrap_content"/>
        <TextView
            android:textColor="#000000"
            android:text="telat"
            android:gravity="center"
            android:id="@+id/telat"
            android:layout_width="100dp"
            android:layout_height="wrap_content"/>

</LinearLayout>

下面是我的java代码:
主活动.java

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);
        }
    }

listviewadapter.java文件

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;
    }
}

电影项目.java

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;
    }

}


这里是apk的例子
你们能给我一个解决方案或例子吗?我很感激

z9smfwbn

z9smfwbn1#

每一次都要增加一些重量 TextView 你已经排成一排了

android:layout_weight="1"

这将使您的所有视图宽度相等,所有视图都将适合内部 LinearLayout . 也可以添加weight=2或更多,以便为其中一列添加更多空间
或者使用gridview或者更好的tablelayout,这个类旨在构建这样的构造(表)

相关问题