jeudi 20 août 2015

Use LinkedList() query result with recyclerView.setAdapter in Fragment

Hoping I can describe this properly... I'm working on a app that uses fragments under tabs based on a sample app by chris banes called "cheesesquare". Basically, I want to retrieve data from a SQLite database rather than use a random list.

This is my query and it works fine;

    public List listMSGH() {
    List rsMSGH = new LinkedList();
    String query = "SELECT * FROM MSGH ORDER BY _ID DESC";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    MSGWRXF MSGWRXF = null;
    if (cursor.moveToFirst()) {
        do {
            MSGWRXF = new MSGWRXF();
            MSGWRXF.set_ID(Integer.parseInt(cursor.getString(0)));
            MSGWRXF.setsMSGRSTAT(cursor.getString(1));
            MSGWRXF.setsMSGSRCE(cursor.getString(2));
            MSGWRXF.setsMSGSNDR(cursor.getString(3));
            MSGWRXF.setsMSGTEXT(cursor.getString(13));
            rsMSGH.add(MSGWRXF);
        } while (cursor.moveToNext());
    }
    return rsMSGH;
}

This is the format that the "set" functions seem to populate ok;

    @Override
public String toString() {
    return "MSGWRXF [_ID=" + _ID + ", MSGRSTAT=" + sMSGRSTAT + ", MSGSRCE=" + 
    sMSGSRCE + ", MSGSNDR=" + sMSGSNDR + ", " + "MSGTEXT=" + sMSGTEXT + "]";
}

Now, here is my code for the actual Fragment, which also works fine;

    import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

public class MessageListFragment extends Fragment {
List<MSGWRXF> mlist;
SQLActivity db1;
ArrayList<String> listMessages = new ArrayList<String>();

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    db1 = new SQLActivity(getActivity());
    mlist = db1.listMSGH();
    listMessages.clear();
    for (int i = 0; i < mlist.size(); i++) {
        listMessages.add(i, String.valueOf(mlist.get(i)));
        // listMessages.add(String.valueOf(mlist.get(i).get_ID()) + " " + mlist.get(i).getMSGSNDR());
        }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    RecyclerView rv = (RecyclerView) inflater.inflate(R.layout.fragment_cheese_list, container, false);
    setupRecyclerView(rv);
    return rv;
}

private void setupRecyclerView(RecyclerView recyclerView) {
    recyclerView.setLayoutManager(new LinearLayoutManager(recyclerView.getContext()));
    recyclerView.setAdapter(new SimpleStringRecyclerViewAdapter(getActivity(),listMessages));
}

public static class SimpleStringRecyclerViewAdapter
        extends RecyclerView.Adapter<SimpleStringRecyclerViewAdapter.ViewHolder> {

    private final TypedValue mTypedValue = new TypedValue();
    private int mBackground;
    private List<String> mValues;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public String mBoundString;

        public final View mView;
        public final ImageView mImageView;
        public final TextView mTextView;

        public ViewHolder(View view) {
            super(view);
            mView = view;
            mImageView = (ImageView) view.findViewById(R.id.avatar);
            mTextView = (TextView) view.findViewById(android.R.id.text1);
        }

        @Override
        public String toString() {
            return super.toString() + " '" + mTextView.getText();
        }
    }

    public String getValueAt(int position) {
        return mValues.get(position);
    }

    public SimpleStringRecyclerViewAdapter(Context context, List<String> items) {
        context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
        mBackground = mTypedValue.resourceId;
        mValues = items;
    }

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

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        holder.mBoundString = mValues.get(position);
        holder.mTextView.setText(mValues.get(position));

        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                Intent intent = new Intent(context, CheeseDetailActivity.class);
                intent.putExtra(CheeseDetailActivity.EXTRA_NAME, holder.mBoundString);

                context.startActivity(intent);
            }
        });


    }

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

}

However, as you can see I'm processing the LinkedList returned by the query into another ArrayList called "listMessages" with a String conversion. This array is then happily processed by the recyclerView.setAdapter and I get several rows of similar to the following output;

MSGWRXF [_ID=10, MSGRSTAT=X, MSGSRCE=40404040, MSGSNDR=FRED, MSGTEXT=Hello]

However, I want to use my get functions like getMSGSNDR() to access each individual field in the elements and display them in different ways. So, my question;

Why am I not able to use the LinkedList "mlist" returned by the query directly in the call to recyclerView.setAdapter instead of "listMessages" and skip the ArrayList process..?

Aucun commentaire:

Enregistrer un commentaire