我正在使用listview和动态项目。它在错误的地方保存(这意味着第三项显示第一项,第五项显示第二项等)。我不知道如何解决这个问题。我添加了相关代码。请检查一下。
修改:
UpcomingGoalAdapter.java:
public class UpcomingGoalAdapter extends ArrayAdapter<UpcomingGoalItems> {
private Context context;
@SuppressWarnings("unused")
private List<UpcomingGoalItems> items;
String eventIdForVol;
private UpcomingGoalAdapter adapter;
String userIdStr, tokenStr;
public UpcomingGoalAdapter(Context context, int resource, List<UpcomingGoalItems> objects) {
super(context, resource, objects);
this.context = context;
this.items = objects;
this.adapter = this;
}
@SuppressLint("InflateParams")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
final UpcomingGoalItems rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.adapt_upcoming_goal, null);
holder = new ViewHolder();
holder.tvInterest = (TextView) convertView.findViewById(R.id.tv_interest_goal_adapt);
holder.ivEdit = (ImageView) convertView.findViewById(R.id.iv_edit_goal_adapt);
holder.tvCount = (TextView) convertView.findViewById(R.id.tv_count_goal_adapt);
holder.ivDelete = (ImageView) convertView.findViewById(R.id.iv_delete_goal_adapt);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if((rowItem.getInterest().equals(null) || rowItem.getInterest().equals(""))
&& (rowItem.getLocation().equals(null) || rowItem.getLocation().equals(""))
&& (rowItem.getFromDate().equals(null) || rowItem.getFromDate().equals(""))
&& (rowItem.getToDate().equals(null) || rowItem.getToDate().equals(""))) {
holder.tvInterest.setText("-");
} else {
holder.tvInterest.setText("Looking for "+rowItem.getInterest() + " in " + rowItem.getSplitLocation() + " On ( "
+ rowItem.getFromDate() + " - " + rowItem.getToDate()+" ) ");
}
holder.tvCount.setText(rowItem.getCount());
return convertView;
}
private class ViewHolder {
TextView tvInterest;
ImageView ivEdit;
TextView tvCount;
ImageView ivDelete;
}
UpcomingGoalActivity.java:
ArrayList<UpcomingGoalItems> itemsaArrayList;
UpcomingGoalAdapter itemsAdapter;
ListView listView;
itemsaArrayList = new ArrayList<UpcomingGoalItems>();
itemsAdapter = new UpcomingGoalAdapter(UpcomingGoalActivity.this, R.layout.adapt_upcoming_goal, itemsaArrayList);
listView = (ListView) findViewById(R.id.lv_interest_goal_search);
listView.setAdapter(itemsAdapter);
hitGoalApi();
private void hitGoalApi(){
String myGoalsUrl = PK_MY_GOALS;
Log.e("myGoalsUrl", myGoalsUrl);
StringRequest request = new StringRequest(Request.Method.GET, myGoalsUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
dialog.dismiss();
if(response != null && !response.startsWith("<HTML>")){
Log.e("MyGoalsRes", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArrData = jsonObject.getJSONArray("data");
for(int i=0; i < jsonArrData.length(); i++){
JSONObject getDataJsonObj = jsonArrData.getJSONObject(i);
LOCATION = getDataJsonObj.getString("location");
String[] placeArray = LOCATION.split("\\s*,\\s*");
Log.e("placeArray", ""+ Arrays.toString(placeArray));
String placeStr = placeArray[0];
FROM_DATE = getDataJsonObj.getString("fromdate");
TO_DATE = getDataJsonObj.getString("todate");
GOAL_ID = getDataJsonObj.getString("g_id");
getInterest = getDataJsonObj.getString("users_interest_goals");
hitCountApi(GOAL_ID, placeStr, FROM_DATE, TO_DATE, getInterest);
}
}
catch (JSONException e){
e.printStackTrace();
}
}else{
toastShort(getApplicationContext(), "Check Internet");
}
}
}
private void hitCountApi(final String goalId, final String splitLoc, final String fromDate,
final String toDate, final String getInteresn) {
String countUrl = PK_GOAL_SEARCH + goalId + ".json";
Log.e("countUrl", countUrl);
StringRequest request = new StringRequest(Request.Method.GET, countUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
dialog.dismiss();
if (response != null && !response.startsWith("<HTML>")) {
Log.e("CountRes", response);
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject metaJsonObj = jsonObject.getJSONObject("_metadata");
String totalRecStr = metaJsonObj.getString("total_records");
Log.e("totalRecStr", "" + totalRecStr);
UpcomingGoalItems items = new UpcomingGoalItems();
items.setSplitLocation(splitLoc);
items.setFromDate(fromDate);
items.setToDate(toDate);
items.setGoalId(goalId);
items.setInterest(getInterest);
items.setCount(totalRecStr); ;
itemsaArrayList.add(items);
itemsAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
} else {
toastShort(getApplicationContext(), "Check Internet");
}
}
})
根据Volley响应顺序,我必须为listview显示正确的ID。它的顺序错误。
让我知道为什么在错误的地方订购以及如何解决这个问题。谢谢。
答案 0 :(得分:0)
tldr:使用ArrayList而不是List:
private ArrayList<UpcomingGoalItems> items;
我使用的ArrayAdapters看起来像这样:
(ArrayList&lt; String&gt;名称是根据ArrayList&lt; Task&gt;的名称创建的ArrayList)
public class ArrayAdapterTask extends ArrayAdapter<String> {
private final Context context;
private int layoutId;
private ArrayList<Task> tasks;
public ArrayAdapterTask(Context context, ArrayList<String> names, ArrayList<Task> tasks, int layoutId) {
super(context, layoutId, names);
this.context = context;
this.tasks = null;
this.tasks = tasks;
this.layoutId = layoutId;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
// Do stuff with the tasks.get(position)
}
}