数据未显示在ListView中 [英] Data does not display in ListView
问题描述
美好的一天,我需要您的帮助,以解决如何在片段内将JSON response
显示为ListView
的问题.用户将在recycler viewer
中选择一张卡,每张卡的内部都包含数据.并且该数据将是API
的必需参数,然后将返回JSON response
.我想在listview
Good day, I need your help guys in how to solve my problem displaying a JSON response
into a ListView
inside a fragment. The user will select a card into a recycler viewer
and every cards has data contains inside. and that data will be the required params for the API
and then will return a JSON response
. I would like to display the response inside a listview
这是JSON响应
{
"sl_details": {
"error": false,
"sl_row": [
{
"tr_type": "RJ",
"credit": "sample 1",
"tr_date": "2017-11-30"
},
{
"tr_type": "RJ",
"credit": "sample 2",
"tr_date": "2017-11-15"
},
{
"tr_type": "CKV",
"credit": "sample 3",
"tr_date": "2017-11-06"
]
}
}
在Recylcer查看器中按下卡以检查数据是否成功获取后的Logcat
Logcat after pressing a card in recylcer viewer to check if data successfull fetch
D/SLDTLFragment:获取SLDTL:RJ
D/SLDTLFragment: Get SLDTL: RJ
D/SLDTLFragment:获取SLDTL:示例1
D/SLDTLFragment: Get SLDTL: sample 1
D/SLDTLFragment:获取SLDTL:2017-11-30
D/SLDTLFragment: Get SLDTL: 2017-11-30
D/SLDTLFragment:获取SLDTL:RJ
D/SLDTLFragment: Get SLDTL: RJ
D/SLDTLFragment:获取SLDTL:示例2
D/SLDTLFragment: Get SLDTL: sample 2
D/SLDTLFragment:获取SLDTL:2017-11-15
D/SLDTLFragment: Get SLDTL: 2017-11-15
D/SLDTLFragment:获取SLDTL:CKV
D/SLDTLFragment: Get SLDTL: CKV
D/SLDTLFragment:获取SLDTL:示例3
D/SLDTLFragment: Get SLDTL: sample 3
D/SLDTLFragment:获取SLDTL:2017-11-06
D/SLDTLFragment: Get SLDTL: 2017-11-06
预期输出
实际输出
片段代码
public class SLDTLFragment extends Fragment {
public SLDTLFragment() {
// Required empty public constructor
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String br_code = getArguments().getString("br_code");
String client_id = getArguments().getString("client_id");
String slc = getArguments().getString("slc");
String sle = getArguments().getString("sle");
String slt = getArguments().getString("slt");
String ref_no = getArguments().getString("ref_no");
Log.d("SLDTLFragment","br code: "+br_code);
Log.d("SLDTLFragment","client id: "+client_id);
Log.d("SLDTLFragment","slc: "+slc);
Log.d("SLDTLFragment","sle: "+sle);
Log.d("SLDTLFragment","slt: "+slt);
Log.d("SLDTLFragment","ref_no: "+ref_no);
// Inflate the layout for this fragment
View listview = inflater.inflate(R.layout.fragment_sldtl, container, false);
SLDTLData sl_data= new SLDTLData();
//final String[] sldtl_data= new String[]{br_code, client_id, slc, slt, ref_no};
final String[] sldtl_data= new String[]{sl_data.getSldtl_trans_type(), sl_data.getSldtl_credit_debit(), sl_data.getSldtltrans_date()};
//Passing data to getSLDTL method..
getSLDTL(br_code, client_id, slc, slt, ref_no);
ListAdapter sldtl= new SLDTLAdapter(getActivity().getApplicationContext(), sldtl_data); // I got stack up here
ListView sl = (ListView)listview.findViewById(R.id.fragment_sldtl_mainListView);
sl.setAdapter(sldtl);
return listview;
}
private void getSLDTL(final String br_code, final String client_id,
final String slc, final String slt, final String ref_no){
final List<SLDTLData> sl= new ArrayList<>();
// Tag used to cancel the request
String tag_string_req = "req_sldtl";
StringRequest strReq= new StringRequest(Request.Method.POST,
AppConfig.URL_SL_DETAILS, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("SLDTLFragment onResponse", "SL Details Response: " + response.toString());
try{
JSONObject jObj= new JSONObject(response.toString());
// boolean error= jObj.getBoolean("error");
if(!jObj.isNull("sl_details")){
JSONObject accountsObject = (JSONObject) jObj.get("sl_details");
// access individual json object thru jsonObject.get("FIELD_NAME")
Log.d("LoginActivity", "SL Details -error attribute : " + accountsObject.get("error").toString());
JSONArray slArray = accountsObject.optJSONArray("sl_row");
// Check if its login data i.e. user present
if (slArray != null) {
JSONArray array = accountsObject.getJSONArray("sl_row");
// access individual json array thru jsonObject.getJSONArray("FIELD_NAME")
Log.d("SLDTLFragment", "SL Details -sl_row array : " + accountsObject.getJSONArray("sl_row").toString());
for (int index=0; index < array.length(); index++) {
SLDTLData sl_data= new SLDTLData();
JSONObject object = (JSONObject)array.get(index);
String tr_type= object.getString("tr_type");
// Log.d("SLDTLFragment", "SL Details -sle_code attribute : " + tr_type);
String credit_debit= object.getString("credit");
// Log.d("SLDTLFragment", "SL Details -sle_code attribute : " + credit_debit);
String tr_date= object.getString("tr_date");
// Log.d("SLDTLFragment", "SL Details -sle_code attribute : " + tr_date);
// Log.d("SLDTLFragment", "SL Details -------------------------------------------------------------------------------------");
sl_data.setSldtl_trans_type(tr_type);
sl_data.setSldtl_credit_debit(credit_debit);
sl_data.setSldtltrans_date(tr_date);
sl.add(sl_data);
sl_data.getSldtl_trans_type();
sl_data.getSldtl_credit_debit();
sl_data.getSldtltrans_date();
Log.d("SLDTLFragment", "Get SLDTL: "+ sl_data.getSldtl_trans_type());
Log.d("SLDTLFragment", "Get SLDTL: "+ sl_data.getSldtl_credit_debit());
Log.d("SLDTLFragment", "Get SLDTL: "+ sl_data.getSldtltrans_date());
}
}
}else{
// a new JSON string that doesn't have sl_summ as member variable so display it and write new handler code
Log.d("SLDTLFragment", "Unknown JSON String : " + jObj.toString());
}
}catch(JSONException e){
e.printStackTrace();
}
}//end onResponse
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Changepass Error: " + error.getMessage());
}
}){
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("br_code", br_code);
params.put("client_id", client_id);
params.put("slc_code", slc);
params.put("slt_code", slt);
params.put("ref_number", ref_no);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
}
适配器代码
public class SLDTLAdapter extends ArrayAdapter<String> {
public SLDTLAdapter(@NonNull Context context, String[] sldtl_data) {
super(context, R.layout.fragment_sldtl_row, sldtl_data);
}
@Override
public View getView(int position, View ConvertView, ViewGroup parent){
LayoutInflater li= LayoutInflater.from(getContext());
View v= li.inflate(R.layout.fragment_sldtl_row, parent, false);
String sldtlItem= getItem(position);
TextView sl_type= (TextView) v.findViewById(R.id.sl_desc);
TextView trans_date= (TextView) v.findViewById(R.id.transDate);
TextView trans_amount = (TextView) v.findViewById(R.id.trans_amount);
TextView debitCredit= (TextView) v.findViewById(R.id.debitCredit);
trans_date.setText(sldtlItem); // I want this to be the actual data in json response
debitCredit.setText("Credit"); // I want this to be the actual data in json response
return v;
}
}
推荐答案
与此变量"sldtl_data"相混淆的第一件事
First thing you confused with this variable "sldtl_data"
final String[] sldtl_data= new String[]{sl_data.getSldtl_trans_type(), sl_data.getSldtl_credit_debit(), sl_data.getSldtltrans_date()};
由于没有sl_data的设置数据,因此s1_data.getSldtl_trans_type()返回空.
Here s1_data.getSldtl_trans_type() return empty because there is no set data to sl_data.
第二件事:在这里,将数据设置为sldtl_data变量,即SLDLData类.
Second thing: Here you set data to sldtl_data varaible that is SLDLData Class.
for (int index=0; index < array.length(); index++) {
SLDTLData sldtl_data= new SLDTLData();
..
}
注意:两个对象的名称相同,但这不是问题
解决方案
1.在全球范围内贴标
1.Decalre this globally
final List<SLDTLData> sldtl= new ArrayList<SLDTLData>();
删除
final List<SLDTLData> sldtl= new ArrayList<>(); from getSLDTL method.
2.更改适配器类
public class SLDTLAdapter extends ArrayAdapter<String> {
private List<Datas> mDataset;
public SLDTLAdapter(@NonNull Context context, String[] sldtl_data) {
super(context, R.layout.fragment_sldtl_row, sldtl_data);
}
至
public class SLDTLAdapter extends ArrayAdapter<SLDTLData> {
private ArrayList<SLDTLData> dataset;
Context mContext;
public SLDTLAdapter(@NonNull Context context, ArrayList<SLDTLData> data) {
super(context, R.layout.fragment_sldtl_row, data);
this.dataSet = data;
this.mContext=context;
}
然后在getview()方法中
And in getview() method
@Override
public View getView(int position, View ConvertView, ViewGroup parent){
SLDTLData sld_data = getItem(position);
trans_date.setText(sld_data.getSldtltrans_date());
...
}
最后在SLDTLFragment类中更改此代码
Finally in SLDTLFragment class change this code
ListAdapter sldtl= new SLDTLAdapter(getActivity().getApplicationContext(), sldtl_data);
到
SLDTLAdapter sldtladapter = new SLDTLAdapter(getActivity().getApplicationContext(), sldtl); // sldtl is your arraylist.
然后删除
final String[] sldtl_data= new String[]{sl_data.getSldtl_trans_type(), sl_data.getSldtl_credit_debit(), sl_data.getSldtltrans_date()};
此代码.
更新/编辑
更改您的适配器类
public class SLDTLAdapter extends ArrayAdapter<SLDTLData> {
private ArrayList<SLDTLData> dataset;
int layoutresourceid;
Context mContext;
public SLDTLAdapter(@NonNull Context context,int LayoutResourceId, ArrayList<SLDTLData> data) {
super(context, R.layout.fragment_sldtl_row, data);
this.dataSet = data;
this.mContext=context;
this.layoutresourceid = LayoutResourceId;
}
@Override
public View getView(int position, View ConvertView, ViewGroup parent){
LayoutInflater li= LayoutInflater.from((Activity)mContext);
//LayoutInflater li= LayoutInflater.from(mContext);
View v= li.inflate(layoutresourceid, parent, false);
TextView sl_type= (TextView) v.findViewById(R.id.sl_desc);
TextView trans_date= (TextView) v.findViewById(R.id.transDate);
TextView trans_amount = (TextView) v.findViewById(R.id.trans_amount);
TextView debitCredit= (TextView) v.findViewById(R.id.debitCredit);
SLDTLData sld_data = getItem(position);
trans_date.setText(sld_data.getSldtltrans_date());
sl_type.setText(sld_data.getSldtl_trans_type());
trans_amount.setText(sld_data.getSldtl_credit_debit());
debitCredit.setText("Credit");
return v;
}
在片段类中
SLDTLAdapter sldtladapter = new SLDTLAdapter(getActivity().getApplicationContext(), R.Layout.fragment_sldtl_row, sldtl); // sldtl is your arraylist.
希望有帮助.
这篇关于数据未显示在ListView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!