Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Adding header in table listview

Posted on November 14, 2022

Solution 1 :

add some weight for every TextView you have in a row

android:layout_weight="1"

this will make all your views equal width and all will fit inside LinearLayout. you may also add weight = 2 or more for adding more space for one of columns

or use GridView or even better TableLayout, this class is intended to build such constructions (tables)

Problem :

I need help to make header in my table listview, i tried every time, header isn’t the same as the table column, i mean not so tidy…

here is my code for 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>

list_item.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>

and here is my code for java :

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

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

}

here is the result

here is the example of apk

READ  [ANSWERED] android - Will UpdateTransition animation maintain its running velocity when if being changed to new animation?
Powered by Inline Related Posts

can you guys give me a solution or example ?
i appreciate that

Comments

Comment posted by stackoverflow.com/questions/7514844/…

stackoverflow.com/questions/7514844/…

Comment posted by Komang Putra

that’s fix, thanks. but can i ask you again ? do you know how to descending data in listview ?

Comment posted by HERE

maybe

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress