Android:无法从LinkedHashMap的ArrayList获取正确的数据

时间:2016-01-14 10:00:12

标签: android listview arraylist hashmap adapter

我正在使用LinkedHashMap的ArrayList将数据传递给适配器,但是当我在适配器中获取数据时,它会一次又一次地显示相同的数据。适配器上的数据是该ArrayList的最后一个数据。 我想要适配器中的所有数据。

TransactionalDetailsActivity:

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.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.LinkedHashMap;
import java.util.Map;

import eukti.myafterclass.R;
import eukti.myafterclass.adapter.TransactionalDetailsAdapter;
import eukti.myafterclass.controller.CallRestApi;
import eukti.myafterclass.utils.Constants;

public class TransactionalDetailsActivity extends AppCompatActivity {

    private ProgressDialog progressDialog;
    private SharedPreferences afterClassPref;
    private String student_uuid;
    private ArrayList<LinkedHashMap<String, String>> arrayList;
    private TransactionalDetailsAdapter transactionalDetailsAdapter;
    private ListView transactionListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_transactional_details);
        afterClassPref = getApplicationContext().getSharedPreferences(Constants.SHARED_PREF_NAME, Context.MODE_PRIVATE);
        student_uuid = afterClassPref.getString("studentUUID", null);
        transactionListView = (ListView) findViewById(R.id.transactionListView);
        getMyTransactionalDetails();
    }

    private void getMyTransactionalDetails() {
        TransactionalDetailsActivity.this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                progressDialog = new ProgressDialog(TransactionalDetailsActivity.this);
                progressDialog.setCanceledOnTouchOutside(false);
                progressDialog.setMessage("please wait, fetching data... ");
                progressDialog.show();
            }
        });
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("student_uuid", student_uuid);
        paramMap.put(Constants.UC_KEY, "transactionaldetails");
        paramMap.put(Constants.MODULE_KEY, "student");
        RequestParams params = new RequestParams(paramMap);
        Log.d("Transactional Details ", "params: " + params);
        CallRestApi.get(Constants.BASE_URL, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                Log.d("Transactional Details ", " Response :" + response);

                if (response != null) {
                    try {

                        if (response.getBoolean("status")) {
                            JSONArray resultArray = response.optJSONArray("result");
                            if (resultArray != null) {
                                arrayList = new ArrayList<LinkedHashMap<String, String>>();
                                LinkedHashMap<String, String> myOrderMap = new LinkedHashMap<>();

                                for (int i = 0; i < resultArray.length(); i++) {
                                    JSONObject mainJsonObj = resultArray.getJSONObject(i);
                                    Log.d("transaction_date", mainJsonObj.getString("transaction_date"));
                                    myOrderMap.put("transaction_date", mainJsonObj.getString("transaction_date"));
                                    myOrderMap.put("activity", mainJsonObj.getString("module"));
                                    myOrderMap.put("activity_details", mainJsonObj.getString("activity"));
                                    myOrderMap.put("transaction_type", mainJsonObj.getString("transaction_type"));
                                    myOrderMap.put("points", mainJsonObj.getString("count_point"));
                                    arrayList.add(myOrderMap);
                                }
                                transactionalDetailsAdapter = new TransactionalDetailsAdapter(TransactionalDetailsActivity.this, arrayList);
                                transactionListView.setAdapter(transactionalDetailsAdapter);
                                transactionListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                                transactionalDetailsAdapter.notifyDataSetChanged();
                                transactionListView.invalidateViews();
                            }
                        }
                    } 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();
                    }
                }
            }
        });
    }
}

TransactionalDetailsAdapter:

    package eukti.myafterclass.expandtest;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import eukti.myafterclass.R;


public class CustomExpandAdapter extends BaseExpandableListAdapter {

    private List<SampleTO> parentRecord;
//    private HashMap<String, List<String>> childRecord;
    private HashMap<String, List<Map<String,String>>> childRecord;
    private LayoutInflater inflater = null;
    private Activity mContext;

/*public CustomExpandAdapter(Activity context, List<SampleTO> parentList, HashMap<String, List<String>> childList) {
        this.parentRecord = parentList;
        this.childRecord = childList;
        mContext = context;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }*/


    public CustomExpandAdapter(Activity context, List<SampleTO> parentList, HashMap<String, List<Map<String,String>>> childList) {
        this.parentRecord = parentList;
        this.childRecord = childList;
        mContext = context;
        inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public String getChild(int groupPosition, int childPosition) {
//        return this.childRecord.get(((SampleTO) getGroup(groupPosition)).getTitle()).get(childPosition);
        /*Log.d("Inside getChild","childPosition: "+childPosition);
        Log.d("Inside getChild","groupPosition: "+groupPosition);*/
        return this.childRecord.get(((SampleTO) getGroup(groupPosition)).getSubjectUUID()).get(childPosition).get("topicName");
    }


    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
//        Log.d("Inside getChildView","childPosition: "+childPosition+" groupPosition "+groupPosition);
        String childConfig = getChild(groupPosition, childPosition);

        ViewHolder holder;
        try {
            if (convertView == null) {
                holder = new ViewHolder();

                convertView = inflater.inflate(R.layout.custom_list_view_child, null);
                holder.childTitle = (TextView) convertView.findViewById(R.id.childTitle);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.childTitle.setText(childConfig);

        } catch (Exception e) {
        }
        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
//        Log.d("Inside getGroupView"," groupPosition "+groupPosition);
        SampleTO parentSampleTo = parentRecord.get(groupPosition);

//        Log.d("Inside getGroupView"," parentSampleTo "+parentSampleTo.getSubjectUUID()+" >>> "+parentSampleTo.getTitle()+" >>> "+parentSampleTo.getCount());

        ViewHolder holder;
        try {
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.custom_list_view, null);
                holder = new ViewHolder();

                holder.parentTitle = (TextView) convertView.findViewById(R.id.parentTitle);
//                holder.parentIcon = (ImageView) convertView.findViewById(R.id.parentIcon);
                holder.count = (TextView) convertView.findViewById(R.id.count);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.parentTitle.setText(parentSampleTo.getTitle());
//            holder.parentIcon.setBackgroundResource(parentSampleTo.getIcon());
            holder.count.setText(parentSampleTo.getCount());

        } catch (Exception e) {
        }
        return convertView;
    }

    public static class ViewHolder {

        private TextView childTitle;
        // Content
        private TextView parentTitle;
//        private ImageView parentIcon;
        private TextView count;

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this.childRecord.get(((SampleTO) getGroup(groupPosition)).getSubjectUUID()).size();
    }

    @Override
    public SampleTO getGroup(int groupPosition) {
        return this.parentRecord.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.parentRecord.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

}

2 个答案:

答案 0 :(得分:1)

使用此实现

package eukti.myafterclass.expandtest;

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.List;
import java.util.Map;


public class TransactionalDetailsAdapter extends BaseAdapter {

private final List<Map<String, String>> mArrayList;
private final LayoutInflater mLayoutInflater;

public TransactionalDetailsAdapter(Context context, List<Map<String, String>> arrayList) {
    mArrayList = arrayList;
    mLayoutInflater = LayoutInflater.from(context);
}

static class ViewHolder {
    TextView dateTextView;
    TextView activityTextView;
    TextView activityDetailsTextView;
    TextView transactionTypeTextView;
    TextView pointsTextView;
}


@Override
public int getCount() {
    return mArrayList.size();
}

@Override
public Object getItem(int position) {
    return mArrayList.get(position);
}

@Override
public long getItemId(int position) {
    return mArrayList.get(position).hashCode();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder viewHolder;

    if (convertView == null) {
        convertView = mLayoutInflater.inflate(R.layout.transactional_details_items, parent, false);
        viewHolder = new ViewHolder();
        viewHolder.dateTextView = (TextView) convertView.findViewById(R.id.date);
        viewHolder.activityTextView = (TextView) convertView.findViewById(R.id.activity);
        viewHolder.activityDetailsTextView = (TextView) convertView.findViewById(R.id.activityDetails);
        viewHolder.transactionTypeTextView = (TextView) convertView.findViewById(R.id.type);
        viewHolder.pointsTextView = (TextView) convertView.findViewById(R.id.points);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    final Map<String, String> itemData = mArrayList.get(position);

    viewHolder.dateTextView.setText(itemData.get("transaction_date"));
    viewHolder.activityTextView.setText(itemData.get("activity"));
    viewHolder.activityDetailsTextView.setText(itemData.get("activity_details"));
    viewHolder.transactionTypeTextView.setText(itemData.get("transaction_type"));
    viewHolder.pointsTextView.setText(itemData.get("points"));

    return convertView;
}

}

考虑用包含所有需要数据的类替换字符串的映射

活动更改

private List<Map<String, String>> arrayList;

arrayList = new ArrayList<Map<String, String>>();
Map<String, String> myOrderMap = new LinkedHashMap<>();

答案 1 :(得分:0)

您的适配器实现不正确检查this适配器代码实现