我需要帮助来选择最后一个数组中的数据,然后在编辑文本中显示它,这是我的代码:
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中显示数据,这是最后一个数组
你们能解决吗?我很感激...
修复强制关闭而不是使用(不能将整数值设置为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] 删除。
我来说两句