我想显示我的数组列表值是什么。因此,我想循环使用数组列表的大小作为限制。但是在我通过函数添加了一些值并想要获取它的大小之后,它始终为0。我仍然是android编程的新手。我曾在google中搜索过,但仍然感到困惑,无法理解问题所在。谢谢你们的帮助
我试图通过全局变量进行设置,但这是行不通的
这是我的活动文件
package bobby.irawan.aticket;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
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.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class DetailPromoActivity extends AppCompatActivity {
private ListView listView;
private TextView nama_detail_promo_tv;
private String nama_detail_promo;
private DetailPromoAdapter adapter;
private int counter_header=0, counter_promo = 0;
private View layout_kupon;
private int id_link_promo;
private RequestQueue requestQueue;
private String url = "192.168.11.134:8000";
private TextView header_promo, kupon_promo, notif_promo;
private ArrayList<DetailPromo> detailPromos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_promo);
View view = findViewById(R.id.layout_kupon);
header_promo = view.findViewById(R.id.header_promo_tv);
kupon_promo = view.findViewById(R.id.kode_promo_tv);
id_link_promo = (int) getIntent().getSerializableExtra("id_promo");
nama_detail_promo = (String) getIntent().getSerializableExtra("nama_promo");
String nama_ota = (String) getIntent().getSerializableExtra("nama_ota");
listView = findViewById(R.id.lv_detpro);
layout_kupon = findViewById(R.id.layout_kupon);
header_promo = layout_kupon.findViewById(R.id.header_promo_tv);
kupon_promo = layout_kupon.findViewById(R.id.kode_promo_tv);
notif_promo = layout_kupon.findViewById(R.id.notif_promo_tv);
requestQueue = Volley.newRequestQueue(this);
nama_detail_promo_tv = findViewById(R.id.nama_detail_promo);
nama_detail_promo_tv.setText(nama_detail_promo);
setDetailPromotoClass(id_link_promo);
adapter = new DetailPromoAdapter(detailPromos,this);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Promo "+nama_ota);
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
//Set kupon
System.out.println("Size detpro "+counter_header+" "+counter_promo);
if(counter_header == 0 && counter_promo == 0){
notif_promo.setVisibility(View.VISIBLE);
header_promo.setVisibility(View.GONE);
kupon_promo.setVisibility(View.GONE);
System.out.println("Masuk sini");
}
else if(counter_header == 0){
header_promo.setText("PROMO PESAWAT");
}
else{
kupon_promo.setText("Tidak perlu");
}
}
class DetailPromoAdapter extends BaseAdapter {
private List<DetailPromo> detailPromo;
private Context context;
public DetailPromoAdapter(List<DetailPromo> detailPromo, Context context) {
this.detailPromo = detailPromo;
this.context = context;
}
@Override
public int getCount() {
System.out.println("Detail promo size"+detailPromo.size());
return detailPromo.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = getLayoutInflater().inflate(R.layout.detail_promo_adapter, parent, false);
TextView nama_detail_promo = convertView.findViewById(R.id.isi_detail_promo);
if(detailPromo.get(position).getDetpro_role().equals("detail_promo")){
nama_detail_promo.setText(detailPromo.get(position).getDetpro_list());
}
return convertView;
}
}
public void setDetailPromotoClass(int id_linkpro){
detailPromos = new ArrayList<DetailPromo>();
String url_promo = "http://"+url+"/detail_promo/"+id_linkpro;
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url_promo, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("data");
for(int i=0; i<jsonArray.length(); i++){
JSONObject data_promo = jsonArray.getJSONObject(i);
int id_detpro = data_promo.getInt("id");
int id_promo = data_promo.getInt("promo_id");
int id_linkpro = data_promo.getInt("linkpro_id");
String detpro_list = data_promo.getString("detpro_list");
String detpro_role = data_promo.getString("detpro_role");
detailPromos.add(new DetailPromo(id_detpro,id_promo,id_linkpro,detpro_list,detpro_role));
System.out.println("Data promo yang diklik "+id_promo+" "+id_linkpro+" "+detpro_list+" "+detpro_role);
adapter.notifyDataSetChanged();
if(detpro_role.equals("header_promo")){
header_promo.setText(detailPromos.get(i).detpro_list);
System.out.println("Ada header");
counter_header++;
}
if(detpro_role.equals("kode_promo")){
System.out.println("Ada kupon");
kupon_promo.setText(detailPromos.get(i).detpro_list);
counter_promo++;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(request);
}
}