在最后一个数组中选择数据(android)

Komang Putra:

我需要帮助来选择最后一个数组中的数据,然后在编辑文本中显示它,这是我的代码:

izinnonfullday.java(主要)

package com.example.eis2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
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 com.example.eis2.Item.postnonfullmodel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;

import static com.example.eis2.Item.LoginItem.KEY_NIK;

public class izinnonfullday extends AppCompatActivity {
    Spinner spinner;
    Button list_data;
    private List<postnonfullmodel> no_pengajuan;
    EditText nopengajuan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_izinnonfullday);
        final String nik_baru = getIntent().getStringExtra(KEY_NIK);
        nopengajuan = (EditText) findViewById(R.id.nopengajuan);
        nopengajuan.setFocusable(false);
        no_pengajuan = new ArrayList<>();


        list_data = (Button) findViewById(R.id.list_data);
        list_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(izinnonfullday.this, tabel_izin_non_full_day.class);
                i.putExtra(KEY_NIK, nik_baru);
                startActivity(i);
            }
        });

        spinner = findViewById(R.id.jenisizin);
        String[] jenisizin = {"Datang telat", "Pulang cepat", "Keluar Kantor"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, jenisizin);
        spinner.setAdapter(adapter);

        final Calendar myCalendar = Calendar.getInstance();
        final EditText edittext = (EditText) findViewById(R.id.tanggalizinnonfull);
        final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                updateLabel();
            }

            private void updateLabel() {
                String myFormat = "MM/dd/yy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                edittext.setText(sdf.format(myCalendar.getTime()));
            }
        };
        edittext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(izinnonfullday.this, date, myCalendar
                        .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                        myCalendar.get(Calendar.DAY_OF_MONTH)).show();
            }
        });
        getLastArray();
    }
        private void getLastArray() {
            StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://hrd.tvip.co.id/rest_server/pengajuan/izin_non_full_day/index",
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONObject obj = new JSONObject(response);
                                JSONArray movieArray = obj.getJSONArray("data");

                                ///get last data only///
                                for (int i = 0; i < movieArray.length(); i++) {
                                        JSONObject movieObject = movieArray.getJSONObject(-1);

                                        postnonfullmodel nomor = new postnonfullmodel(
                                                movieObject.getInt("no_pengajuan_non_full"));

                                        no_pengajuan.add(nomor);
                                        nopengajuan.setText(nomor.getNo_pengajuan_non_full());


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

            RequestQueue requestQueue = Volley.newRequestQueue(this);
            requestQueue.add(stringRequest);

        }
    }

postnonfullmodel.java(获取数据)


public class postnonfullmodel {
    Integer no_pengajuan_non_full;

    public postnonfullmodel(Integer no_pengajuan_non_full){
        this.no_pengajuan_non_full = no_pengajuan_non_full;
    }
    public Integer getNo_pengajuan_non_full() {return no_pengajuan_non_full; }
}

activity_izinnonfullday.xml(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=".izinnonfullday">
    <TextView
        android:id="@+id/nonfullday"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:textColor="@color/colorPrimaryDark"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="32dp"
        android:text = "Izin Non Full Day" />

    <Button
        android:id="@+id/list_data"
        android:layout_width="wrap_content"
        android:layout_height="36dp"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="35dp"
        android:layout_toRightOf="@+id/nonfullday"
        android:text="list data" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="No.pengajuan"
        android:editable="false"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="32dp"
        android:layout_below="@id/nonfullday"
        android:textColor="#000000"
        android:textSize="20dp"/>
    <EditText
        android:id="@+id/nopengajuan"
        android:layout_width="241dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/textView1"
        android:layout_marginLeft="23dp"
        android:layout_marginTop="95dp"
        android:hint="nomor pengajuan"
        android:textColor="#000000"
        android:selectAllOnFocus="true"/>

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mulai Tanggal"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="22dp"
        android:layout_below="@id/nopengajuan"
        android:textColor="#000000"
        android:textSize="20dp"/>
    <EditText
        android:id="@+id/tanggalizinnonfull"
        android:layout_width="241dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/textView2"
        android:layout_marginLeft="23dp"
        android:layout_below="@id/nopengajuan"
        android:layout_marginTop="5dp"
        android:hint="tanggal pengajuan"
        android:clickable="true"
        android:focusable="false"
        android:inputType="datetime"
        android:textColor="#000000"
        android:selectAllOnFocus="true"/>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Jenis Izin"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="22dp"
        android:layout_below="@id/textView2"
        android:textColor="#000000"
        android:textSize="20dp"/>
    <Spinner
        android:id="@+id/jenisizin"
        android:layout_width="241dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/textView3"
        android:layout_below="@id/tanggalizinnonfull"
        android:hint="jenis izin"
        android:layout_marginTop="10dp"
        />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Keterangan Tambahan"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="22dp"
        android:layout_below="@id/jenisizin"
        android:textColor="#000000"
        android:textSize="20dp"/>
    <EditText
        android:id="@+id/keterangan"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView4"
        android:ems="1000"
        android:inputType="text"
        android:hint="keterangan"
        android:text="" />

    <Button
        android:id="@+id/pengajuan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/keterangan"
        android:layout_centerHorizontal="true"
        android:text="pengajuan"
        android:layout_marginTop="15dp" />
</RelativeLayout>

我想在pengajuan的edittext中显示数据,这是最后一个数组

在此处输入图片说明

你们能解决吗?我很感激...

akhilesh0707:

修复强制关闭而不是使用(不能将整数值设置为edittext)

nopengajuan.setText(nomor.getNo_pengajuan_non_full());

nopengajuan.setText(String.valueOf(nomor.getNo_pengajuan_non_full()));

将您的尝试块更改为一个以下

try {
     JSONObject obj = new JSONObject(response);
     JSONArray movieArray = obj.getJSONArray("data");
     for (int i = 0; i < movieArray.length(); i++) {
         JSONObject movieObject = movieArray.getJSONObject(i);
         postnonfullmodel nomor = new postnonfullmodel(movieObject.getInt("no_pengajuan_non_full"));
         no_pengajuan.add(nomor);
     }
     // To get the last time from list
     postnonfullmodel item = no_pengajuan.get(no_pengajuan.size()-1)
     // Set value to edit text
     nopengajuan.setText(String.valueOf(item.getNo_pengajuan_non_full()));
} catch (JSONException e) {
   e.printStackTrace();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

根据来自另一个数组的argmax的索引从数组中选择值

如何基于JavaScript中另一个数组的索引从数组中选择元素?

从Json数组中选择最后一个值

如何平均每5行特定列并从Pandas的另一列中选择最后一个数据

从另一个数组中选择相同名称的项目

从服务器上每个数据库的一个表中选择数据

Excel:从单元格中选择最后一个或多个数字

如何从另一个数组中的一个数组中选择一个值?

ECTO-从Postgresql数据库中选择一个数组字段

从DataFrame中选择第一个和最后一个数据的这两种方法有什么区别?

使用一个数据框中的最后一个有效数据索引选择另一个数据框中的数据

电子,Javascript:如何在数据库返回的数组中选择一个数组

如何在bash脚本中选择行中的最后一个数字

在PostgreSQL中选择一个数组列的总和

从中选择一个数据库?

PHP根据从另一个数组中选择的元素消除数组元素

从数组中选择与php中另一个数组中的条件匹配的数据

php数组在多个数组中选择一个键值并更改它们

从数据库中选择只是可以获取第一个数据

如何使用键从另一个数据中选择一个数据

如何从另一个数组中的多个元素中选择数组中的元素

根据另一个参考数组从一个数组中选择接近匹配

如何从另一个数据库的select语句中选择数据?

每天选择最后一个数据

从最后一个完整行中选择数据

如何在某个值上的基本关联数组中选择一个数组

从大型数据集中的成对列中选择最后一个有效数据日期

如何在 Mule 4 的对象数组中选择最后一个重复对象

从数组中选择数据并在段落中显示来自另一个数组的数据 - javascript