回收者视图显示空白项目

时间:2020-05-24 16:28:20

标签: java android sqlite android-recyclerview

我正在构建一个Android应用,该应用从sqlite db中搜索字符串并将结果显示在recyclerview中。 我发现构建此应用程序时遇到的问题是,当我键入一个包含字母“ a”的字符串时,它将包含该字母的行数显示为空白项。有人可以帮忙吗?
MainActivity.java

public void fetchContactOffline(String flcode){
        ArrayList<QueryResult> list = new ArrayList();
        if (flcode.length() > 0) {
            StringBuilder query = new StringBuilder("select * from ").append(TABLE_NAME).append(" where ");
            query.append(TABLE_COLUMN_FLCODE).append(" like \"%").append(flcode).append("%\"");
            SQLiteOpenHelper database = new RichDB(this);
            SQLiteDatabase db = database.getReadableDatabase();
            Cursor cursor = db.rawQuery(query.toString(), null);

            if (cursor.moveToFirst()) {
                do {
                    QueryResult queryResult = new QueryResult();
                    queryResult.answer = cursor.getString(cursor.getColumnIndex(TABLE_COLUMN_ANSWER));
                    queryResult.question = cursor.getString(cursor.getColumnIndex(TABLE_COLUMN_QUESTION));
                    list.add(queryResult);
                } while (cursor.moveToNext());
            }
            db.close();
        }
        progressBar.setVisibility(GONE);
        adapter = new Adapter(list);
        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }


Adapter.java


    private ArrayList<QueryResult> questions;

    public Adapter(ArrayList<QueryResult> questions) {
        this.questions = questions;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.items, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.reponse.setText(questions.get(position).getAnswer());
        holder.reponse.setTextColor(Color.BLACK);
        holder.question.setText(questions.get(position).getQuestion());
        holder.question.setTextColor(Color.BLUE);
    }

    @Override
    public int getItemCount() {
        return questions.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        TextView reponse,question;
        public MyViewHolder(View itemView) {
            super(itemView);
            reponse = itemView.findViewById(R.id.AnswerItem);
            question = itemView.findViewById(R.id.QuestionItem);
        }
    }


items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">

    <TextView
        android:paddingTop="10dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:textSize="20sp"
        android:textColor="#111"
        android:id="@+id/AnswerItem"
        android:text="Answer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/QuestionItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="15dp"
        android:paddingTop="5dp"
        android:paddingRight="15dp"
        android:paddingBottom="10dp"
        android:text="Question" />

    <View
        android:background="#bfbfbf"
        android:layout_width="match_parent"
        android:layout_height="0.1dp"/>

</LinearLayout>

ScreenCapture
Screenshot - 1

Screenshot - 2

2 个答案:

答案 0 :(得分:0)

我认为问题在于您不这样称呼: recyclerView.setLayoutManager(new LinearLayoutManager (this));。 而且我认为这一行-adapter.notifyDataSetChanged();是多余的,因为适配器的新实例是在adapter = new Adapter(list);

的两行上方创建的

答案 1 :(得分:0)

我已修复它,问题出在 QueryResult.java 中。感谢您的帮助 。 :)