Android: 无法从 POJO class 访问 getter 方法
Android: Not able to access getter method from POJO class
我在 activity 中使用适配器来设置列表视图的值。为此,我将一个 POJO class 的 Arraylist 传递给适配器。但是当我试图在我的列表视图的文本视图中设置文本时,那个时候我无法访问 getName getter 方法。
购买活动:
package eukti.myafterclass.ui;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import eukti.myafterclass.R;
import eukti.myafterclass.adapter.PurchaseBaseAdapter;
import eukti.myafterclass.controller.CallRestApi;
import eukti.myafterclass.dto.PurchasePojo;
import eukti.myafterclass.utils.Constants;
public class PurchaseActivity extends AppCompatActivity {
private ListView purchaseListView;
private ArrayList<PurchasePojo> arrayList;
private ProgressDialog progressDialog;
private SharedPreferences afterClassPref;
private String university_uuid;
private PurchaseBaseAdapter purchaseBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purchase);
purchaseListView = (ListView) findViewById(R.id.purchaseListView);
arrayList = new ArrayList<PurchasePojo>();
afterClassPref = getApplicationContext().getSharedPreferences(Constants.SHARED_PREF_NAME, Context.MODE_PRIVATE);
university_uuid = afterClassPref.getString("studentUniversityUUID", null);
}
private void getNotesSubjectList(final boolean isShared){
PurchaseActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog = new ProgressDialog(PurchaseActivity.this);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setMessage("please wait, fetching data... ");
progressDialog.show();
}
});
Map<String,String> paramMap = new HashMap<>();
paramMap.put("university_uuid", university_uuid);
paramMap.put(Constants.UC_KEY, "scheme");
paramMap.put(Constants.MODULE_KEY, "student");
RequestParams params = new RequestParams(paramMap);
Log.d("Purchase Packages ", "params: " + params);
CallRestApi.get(Constants.BASE_URL, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
Log.d("Purchase Packages ", " Response :" + response);
if (response != null) {
try {
if (response.getBoolean("status")) {
JSONArray resultArray = response.optJSONArray("result");
if (resultArray != null) {
Gson gson = new Gson();
arrayList = gson.fromJson(resultArray.getJSONObject(0).toString(), new TypeToken<ArrayList<PurchasePojo>>(){}.getType());
purchaseBaseAdapter = new PurchaseBaseAdapter(PurchaseActivity.this, arrayList);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
if (progressDialog != null) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
if (progressDialog != null) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
});
}
}
购买 Pojo:
package eukti.myafterclass.dto;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class PurchasePojo {
@SerializedName("uuid")
@Expose
private String uuid;
@SerializedName("name")
@Expose
private String name;
@SerializedName("ask_question")
@Expose
private String askQuestion;
@SerializedName("view_expert_answer")
@Expose
private String viewExpertAnswer;
@SerializedName("question_bank")
@Expose
private String questionBank;
@SerializedName("price")
@Expose
private String price;
@SerializedName("is_default")
@Expose
private String isDefault;
@SerializedName("enabled")
@Expose
private String enabled;
@SerializedName("created")
@Expose
private String created;
@SerializedName("created_by")
@Expose
private String createdBy;
@SerializedName("modified")
@Expose
private String modified;
@SerializedName("modified_by")
@Expose
private String modifiedBy;
/**
*
* @return
* The uuid
*/
public String getUuid() {
return uuid;
}
/**
*
* @param uuid
* The uuid
*/
public void setUuid(String uuid) {
this.uuid = uuid;
}
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The askQuestion
*/
public String getAskQuestion() {
return askQuestion;
}
/**
*
* @param askQuestion
* The ask_question
*/
public void setAskQuestion(String askQuestion) {
this.askQuestion = askQuestion;
}
/**
*
* @return
* The viewExpertAnswer
*/
public String getViewExpertAnswer() {
return viewExpertAnswer;
}
/**
*
* @param viewExpertAnswer
* The view_expert_answer
*/
public void setViewExpertAnswer(String viewExpertAnswer) {
this.viewExpertAnswer = viewExpertAnswer;
}
/**
*
* @return
* The questionBank
*/
public String getQuestionBank() {
return questionBank;
}
/**
*
* @param questionBank
* The question_bank
*/
public void setQuestionBank(String questionBank) {
this.questionBank = questionBank;
}
/**
*
* @return
* The price
*/
public String getPrice() {
return price;
}
/**
*
* @param price
* The price
*/
public void setPrice(String price) {
this.price = price;
}
/**
*
* @return
* The isDefault
*/
public String getIsDefault() {
return isDefault;
}
/**
*
* @param isDefault
* The is_default
*/
public void setIsDefault(String isDefault) {
this.isDefault = isDefault;
}
/**
*
* @return
* The enabled
*/
public String getEnabled() {
return enabled;
}
/**
*
* @param enabled
* The enabled
*/
public void setEnabled(String enabled) {
this.enabled = enabled;
}
/**
*
* @return
* The created
*/
public String getCreated() {
return created;
}
/**
*
* @param created
* The created
*/
public void setCreated(String created) {
this.created = created;
}
/**
*
* @return
* The createdBy
*/
public String getCreatedBy() {
return createdBy;
}
/**
*
* @param createdBy
* The created_by
*/
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
/**
*
* @return
* The modified
*/
public String getModified() {
return modified;
}
/**
*
* @param modified
* The modified
*/
public void setModified(String modified) {
this.modified = modified;
}
/**
*
* @return
* The modifiedBy
*/
public String getModifiedBy() {
return modifiedBy;
}
/**
*
* @param modifiedBy
* The modified_by
*/
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
}
PurchaseBaseAdapter:
package eukti.myafterclass.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import eukti.myafterclass.R;
import eukti.myafterclass.dto.PurchasePojo;
/**
* Created by cuser on 11/1/16.
*/
public class PurchaseBaseAdapter extends BaseAdapter {
private final LayoutInflater layoutInflater;
private Context context;
private ArrayList<PurchasePojo> arrayList;
private TextView price, name, pqTextView, vaTextView;
public PurchaseBaseAdapter(Context context, ArrayList<PurchasePojo> arrayList) {
this.context = context;
this.arrayList = arrayList;
layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.arrayList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.account_list_items, null);
}
name = (TextView) convertView.findViewById(R.id.name);
price = (TextView) convertView.findViewById(R.id.price);
pqTextView = (TextView) convertView.findViewById(R.id.pqTextView);
vaTextView = (TextView) convertView.findViewById(R.id.vaTextView);
name.setText(arrayList.getName());
}
catch (Exception e) {
}
return convertView;
}
}
name.setText(arrayList.getName());由于 getName().
,适配器中的这一行显示错误
您需要先访问该列表的元素。替换
name.setText(arrayList.getName());
来自
name.setText(arrayList.get(position).getName());
将您的 getName() 替换为 get(position).getName()。
您的 getView 方法将如下所示,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.account_list_items, null);
}
name = (TextView) convertView.findViewById(R.id.name);
price = (TextView) convertView.findViewById(R.id.price);
pqTextView = (TextView) convertView.findViewById(R.id.pqTextView);
vaTextView = (TextView) convertView.findViewById(R.id.vaTextView);
name.setText(arrayList.get(position).getName());
}
catch (Exception e) {
}
return convertView;
}
我在 activity 中使用适配器来设置列表视图的值。为此,我将一个 POJO class 的 Arraylist 传递给适配器。但是当我试图在我的列表视图的文本视图中设置文本时,那个时候我无法访问 getName getter 方法。
购买活动:
package eukti.myafterclass.ui;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import eukti.myafterclass.R;
import eukti.myafterclass.adapter.PurchaseBaseAdapter;
import eukti.myafterclass.controller.CallRestApi;
import eukti.myafterclass.dto.PurchasePojo;
import eukti.myafterclass.utils.Constants;
public class PurchaseActivity extends AppCompatActivity {
private ListView purchaseListView;
private ArrayList<PurchasePojo> arrayList;
private ProgressDialog progressDialog;
private SharedPreferences afterClassPref;
private String university_uuid;
private PurchaseBaseAdapter purchaseBaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_purchase);
purchaseListView = (ListView) findViewById(R.id.purchaseListView);
arrayList = new ArrayList<PurchasePojo>();
afterClassPref = getApplicationContext().getSharedPreferences(Constants.SHARED_PREF_NAME, Context.MODE_PRIVATE);
university_uuid = afterClassPref.getString("studentUniversityUUID", null);
}
private void getNotesSubjectList(final boolean isShared){
PurchaseActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog = new ProgressDialog(PurchaseActivity.this);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setMessage("please wait, fetching data... ");
progressDialog.show();
}
});
Map<String,String> paramMap = new HashMap<>();
paramMap.put("university_uuid", university_uuid);
paramMap.put(Constants.UC_KEY, "scheme");
paramMap.put(Constants.MODULE_KEY, "student");
RequestParams params = new RequestParams(paramMap);
Log.d("Purchase Packages ", "params: " + params);
CallRestApi.get(Constants.BASE_URL, params, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
Log.d("Purchase Packages ", " Response :" + response);
if (response != null) {
try {
if (response.getBoolean("status")) {
JSONArray resultArray = response.optJSONArray("result");
if (resultArray != null) {
Gson gson = new Gson();
arrayList = gson.fromJson(resultArray.getJSONObject(0).toString(), new TypeToken<ArrayList<PurchasePojo>>(){}.getType());
purchaseBaseAdapter = new PurchaseBaseAdapter(PurchaseActivity.this, arrayList);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
if (progressDialog != null) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
}
@Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
if (progressDialog != null) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
});
}
}
购买 Pojo:
package eukti.myafterclass.dto;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class PurchasePojo {
@SerializedName("uuid")
@Expose
private String uuid;
@SerializedName("name")
@Expose
private String name;
@SerializedName("ask_question")
@Expose
private String askQuestion;
@SerializedName("view_expert_answer")
@Expose
private String viewExpertAnswer;
@SerializedName("question_bank")
@Expose
private String questionBank;
@SerializedName("price")
@Expose
private String price;
@SerializedName("is_default")
@Expose
private String isDefault;
@SerializedName("enabled")
@Expose
private String enabled;
@SerializedName("created")
@Expose
private String created;
@SerializedName("created_by")
@Expose
private String createdBy;
@SerializedName("modified")
@Expose
private String modified;
@SerializedName("modified_by")
@Expose
private String modifiedBy;
/**
*
* @return
* The uuid
*/
public String getUuid() {
return uuid;
}
/**
*
* @param uuid
* The uuid
*/
public void setUuid(String uuid) {
this.uuid = uuid;
}
/**
*
* @return
* The name
*/
public String getName() {
return name;
}
/**
*
* @param name
* The name
*/
public void setName(String name) {
this.name = name;
}
/**
*
* @return
* The askQuestion
*/
public String getAskQuestion() {
return askQuestion;
}
/**
*
* @param askQuestion
* The ask_question
*/
public void setAskQuestion(String askQuestion) {
this.askQuestion = askQuestion;
}
/**
*
* @return
* The viewExpertAnswer
*/
public String getViewExpertAnswer() {
return viewExpertAnswer;
}
/**
*
* @param viewExpertAnswer
* The view_expert_answer
*/
public void setViewExpertAnswer(String viewExpertAnswer) {
this.viewExpertAnswer = viewExpertAnswer;
}
/**
*
* @return
* The questionBank
*/
public String getQuestionBank() {
return questionBank;
}
/**
*
* @param questionBank
* The question_bank
*/
public void setQuestionBank(String questionBank) {
this.questionBank = questionBank;
}
/**
*
* @return
* The price
*/
public String getPrice() {
return price;
}
/**
*
* @param price
* The price
*/
public void setPrice(String price) {
this.price = price;
}
/**
*
* @return
* The isDefault
*/
public String getIsDefault() {
return isDefault;
}
/**
*
* @param isDefault
* The is_default
*/
public void setIsDefault(String isDefault) {
this.isDefault = isDefault;
}
/**
*
* @return
* The enabled
*/
public String getEnabled() {
return enabled;
}
/**
*
* @param enabled
* The enabled
*/
public void setEnabled(String enabled) {
this.enabled = enabled;
}
/**
*
* @return
* The created
*/
public String getCreated() {
return created;
}
/**
*
* @param created
* The created
*/
public void setCreated(String created) {
this.created = created;
}
/**
*
* @return
* The createdBy
*/
public String getCreatedBy() {
return createdBy;
}
/**
*
* @param createdBy
* The created_by
*/
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
/**
*
* @return
* The modified
*/
public String getModified() {
return modified;
}
/**
*
* @param modified
* The modified
*/
public void setModified(String modified) {
this.modified = modified;
}
/**
*
* @return
* The modifiedBy
*/
public String getModifiedBy() {
return modifiedBy;
}
/**
*
* @param modifiedBy
* The modified_by
*/
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
}
PurchaseBaseAdapter:
package eukti.myafterclass.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import eukti.myafterclass.R;
import eukti.myafterclass.dto.PurchasePojo;
/**
* Created by cuser on 11/1/16.
*/
public class PurchaseBaseAdapter extends BaseAdapter {
private final LayoutInflater layoutInflater;
private Context context;
private ArrayList<PurchasePojo> arrayList;
private TextView price, name, pqTextView, vaTextView;
public PurchaseBaseAdapter(Context context, ArrayList<PurchasePojo> arrayList) {
this.context = context;
this.arrayList = arrayList;
layoutInflater = (LayoutInflater)context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.arrayList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.account_list_items, null);
}
name = (TextView) convertView.findViewById(R.id.name);
price = (TextView) convertView.findViewById(R.id.price);
pqTextView = (TextView) convertView.findViewById(R.id.pqTextView);
vaTextView = (TextView) convertView.findViewById(R.id.vaTextView);
name.setText(arrayList.getName());
}
catch (Exception e) {
}
return convertView;
}
}
name.setText(arrayList.getName());由于 getName().
,适配器中的这一行显示错误您需要先访问该列表的元素。替换
name.setText(arrayList.getName());
来自
name.setText(arrayList.get(position).getName());
将您的 getName() 替换为 get(position).getName()。 您的 getView 方法将如下所示,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.account_list_items, null);
}
name = (TextView) convertView.findViewById(R.id.name);
price = (TextView) convertView.findViewById(R.id.price);
pqTextView = (TextView) convertView.findViewById(R.id.pqTextView);
vaTextView = (TextView) convertView.findViewById(R.id.vaTextView);
name.setText(arrayList.get(position).getName());
}
catch (Exception e) {
}
return convertView;
}