Android:适配器无法使用Hashmap的Arraylist

时间:2016-03-18 06:45:49

标签: android sqlite listview adapter sqlhelper

我试图从SQLite数据库中脱机显示一些数据。数据很好但我无法在listview上显示适配器。

libraryList.clear();
libraryList = dBhelper.localQuestionBank(student_uuid, questionBankId);
if (libraryList.size() > 0) {
    offSetFlag = libraryList.size();
    libraryListView.setAdapter(questionBankAdapter);
    questionBankAdapter.notifyDataSetChanged();
    libraryListView.removeFooterView(loadMoreView);
    loadingMore = false;
}

localQuestionBank方法:

public ArrayList<Map<String, String>> localQuestionBank(String student_uuid, String question_bank_id) {
        ArrayList<Map<String, String>> localQuestionBankList = new ArrayList<>();
        ArrayList<Map<String, String>> questionBankList  = new ArrayList<>();
        String[] columns = new String[]{QUESTION_BANK_LABEL, AIP_UUID, QUESTION_SEQUENCE_NAME, QUESTION_MARKS, DESCRIPTION};
        SQLiteDatabase db = mDbHelper.getWritableDatabase();
        try {
            Cursor cursor = db.query(LOCAL_QUESTION_BANK, columns, STUDENT_UUID+"=?" +" and "+ QUESTION_BANK_ID+"=?", new String[] { student_uuid, question_bank_id }, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    Map<String, String> subMap = new HashMap<String, String>();
                    subMap.put("aep_uuid", cursor.getString(cursor.getColumnIndex(AIP_UUID)));
                    subMap.put("question_marks", cursor.getString(cursor.getColumnIndex(QUESTION_MARKS)));
                    subMap.put("question_sequence_name", cursor.getString(cursor.getColumnIndex(QUESTION_SEQUENCE_NAME)));
                    subMap.put("description", cursor.getString(cursor.getColumnIndex(DESCRIPTION)));
                    subMap.put("question_bank_label", cursor.getString(cursor.getColumnIndex(QUESTION_BANK_LABEL)));
                    questionBankList.add(subMap);
                } while (cursor.moveToNext());
                localQuestionBankList.addAll(questionBankList);
                questionBankList.clear();
            }
            cursor.close();
            db.close();
        } catch (SQLException sqlEx) {
            sqlEx.printStackTrace();
        }
        return localQuestionBankList;
    }

Listview和适配器:

questionBankAdapter = new QuestionBankAdapter(getActivity(),libraryList);
libraryListView.setAdapter(questionBankAdapter);

questionBankAdapter:

package eukti.myafterclass.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Map;

import eukti.myafterclass.R;
import eukti.myafterclass.ui.QuestionBankDetailsView;
import eukti.myafterclass.utils.InternetConnectionDetector;

public class QuestionBankAdapter extends BaseAdapter {

    private final Activity context;
    private ArrayList<Map<String, String>> dashboardList;
    private static LayoutInflater inflater=null;


    public QuestionBankAdapter(Activity context,
                               ArrayList<Map<String, String>> dashList) {
        this.context = context;
        this.dashboardList = dashList;
        if (context!=null) {
            inflater = (LayoutInflater) context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
    }

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

    @Override
    public Object getItem(int position) {
        return dashboardList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public class Holder
    {
        TextView sequence;
        TextView marks;
        WebView description;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder holder=new Holder();
        View rowView;
        Log.d("LocalQBadapter:", dashboardList.get(position).get("question_sequence_name"));
        rowView = inflater.inflate(R.layout.questionbank_items, null);
        holder.sequence=(TextView) rowView.findViewById(R.id.questionSequenceName);
        holder.marks=(TextView) rowView.findViewById(R.id.marks);
        holder.description=(WebView) rowView.findViewById(R.id.description);
        holder.description.getSettings().setJavaScriptEnabled(true);

        String heading = dashboardList.get(position).get("question_sequence_name");
        holder.sequence.setText(Html.fromHtml(heading));
        holder.marks.setText("Marks : "+dashboardList.get(position).get("question_marks"));

        String description = dashboardList.get(position).get("description");

        String allAns = "<html><body>"+description.replace("\r\n", "<br/>")+"</body></html>";
        holder.description.loadDataWithBaseURL(null, allAns, "text/html", "utf-8", null);

        holder.description.setOnLongClickListener(new View.OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        holder.description.setLongClickable(false);

        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!InternetConnectionDetector.isNetworkAvailable(context)) {
                    Toast.makeText(context, "Check your connection and try again.", Toast.LENGTH_LONG).show();
                } else {
                    Intent redirect = new Intent(context, QuestionBankDetailsView.class);
                    redirect.putExtra("aep_uuid", dashboardList.get(position).get("aep_uuid"));
                    redirect.putExtra("question_sequence_name", dashboardList.get(position).get("question_sequence_name"));
                    redirect.putExtra("question_marks", dashboardList.get(position).get("question_marks"));
                    redirect.putExtra("description", dashboardList.get(position).get("description"));
                    context.startActivity(redirect);
                }
            }
        });

        return rowView;
    }
}

对我来说似乎没问题但是Listview中没有显示数据。

2 个答案:

答案 0 :(得分:1)

您键入的内容

@Override
public Object getItem(int position) {
    return dashboardList.size();
}

我认为它应该是什么

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

尝试并让我知道

答案 1 :(得分:0)

getItem()方法中,您将返回size。您需要从数据集dashboardList中提供实际对象。

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

同样在getView代码中,您可以调用getItem方法,而不是直接访问数据集。

Map<String, String> item = (Map<String, String>) getitem(position);

从此item对象绑定您的UI。另请尝试在Adapter中使用Viewholder pattern