public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private Context mContext;
private List<MyList> mItems;
private int item_layout;
public MyAdapter(Context mContext, List<MyList> items, int item_layout) {
this.mContext = mContext;
this.mItems = items;
this.item_layout = item_layout;
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View mView = inflater.inflate(R.layout.simpleitem, parent,false);
return new ViewHolder(mView);
}
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
MyList item = mItems.get(position);
holder.mTextTemp.setText(item.getTemp());
holder.mTextData.setText(item.getData());
holder.mImageView.setBackgroundResource(item.getImage());
}
@Override
public int getItemCount() {
return this.mItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextData;
public TextView mTextTemp;
public ViewHolder(View itemView) {
super(itemView);
mImageView = (ImageView) itemView.findViewById(R.id.backImage);
mTextData = (TextView) itemView.findViewById(R.id.textData);
mTextTemp = (TextView) itemView.findViewById(R.id.textTemp);
}
}
}
public class FragmentPage2 extends Fragment {
List<MyList> myList;
public DataBase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
db = new DataBase(getActivity(), "THERMO.db", null, 1);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
db.insert("11", "11", "1");
db.insert("22", "22", "2");
initData();
View layout = inflater.inflate(R.layout.fragment_page2, container, false);
RecyclerView mRecyclerView = (RecyclerView)layout.findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(layout.getContext());
mRecyclerView.setLayoutManager(mLayoutManager);
RecyclerView.Adapter mAdapter;
Log.v("11", "11");
mAdapter = new MyAdapter(layout.getContext(), myList, R.layout.simpleitem);
Log.v("22", "22");
mRecyclerView.setAdapter(mAdapter);
MainActivity.selectButton(1);
return layout;
}
private void initData()
{
myList = new ArrayList<MyList>();
myList = db.selectData(myList, "BODY_TEMP", "DATE, BODY_TEMP");
Log.v("gggg" , myList.toString());
}
}
public List selectData(List myList, String table, String field)
{
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("select " + field + " from " + table, null);
while(cursor.moveToNext())
{
myList.add(cursor.getString(0));
}
return myList;
}
String
, to aString
. That doesn't work. Casting is not a way to automatically convert objects from one type to another. – Jesper Jul 20 at 8:04