在表格列表视图中添加标题

huangapple go评论67阅读模式
英文:

Adding header in table 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>

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>

这是 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;
   

<details>
<summary>英文:</summary>

I need help to make header in my table listview, i tried every time, header isn&#39;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&lt;MovieItem&gt; movieItemList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = findViewById(R.id.list);
movieItemList = new ArrayList&lt;&gt;();
loadPlayer();
}
private void loadPlayer() {
StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
new Response.Listener&lt;String&gt;() {
@Override
public void onResponse(String response) {
try {
JSONObject obj = new JSONObject(response);
JSONArray movieArray = obj.getJSONArray(&quot;data&quot;);
for (int i = 0; i &lt; movieArray.length(); i++) {
JSONObject movieObject = movieArray.getJSONObject(i);
MovieItem movieItem = new MovieItem(
movieObject.getString(&quot;F1&quot;),
movieObject.getString(&quot;depo_f1&quot;),
movieObject.getString(&quot;F4&quot;),
movieObject.getString(&quot;depo_f4&quot;),
movieObject.getString(&quot;ket_absensi&quot;));
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&lt;MovieItem&gt; movieItemList;
private Context context;
public ListViewAdapter(List&lt;MovieItem&gt; 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;
}

}

[1]: https://i.stack.imgur.com/DlznY.png
[![here is the result][1]][1]
here is the example of apk
can you guys give me a solution or example ?
i appreciate that
</details>
# 答案1
**得分**: 1
为每个在一行中的 `TextView` 添加一些权重
android:layout_weight="1"
这将使所有的视图具有相等的宽度,并且都适应于 `LinearLayout` 中。你也可以为其中一列添加权重为 2 或更多,以增加该列的空间。
或者使用 [GridView][1],甚至更好地使用 [TableLayout][2],这个类专门用于构建这种结构(表格)。
[1]: https://developer.android.com/reference/android/widget/GridView
[2]: https://developer.android.com/reference/android/widget/TableLayout
<details>
<summary>英文:</summary>
add some weight for every `TextView` you have in a row
android:layout_weight=&quot;1&quot;
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][1] or even better [TableLayout][2], this class is intended to build such constructions (tables)
[1]: https://developer.android.com/reference/android/widget/GridView
[2]: https://developer.android.com/reference/android/widget/TableLayout
</details>

huangapple
  • 本文由 发表于 2020年9月3日 12:37:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63716938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定