当我尝试使用RecyclerView
中的数据填充SQLite Database
时,我遇到了问题。问题是我正确地获取了数据,但它们没有显示在RecyclerView
。
我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_times);
toolbar = (Toolbar)findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView)findViewById(R.id.recycler_list);
databaseAdapter = new DatabaseAdapter(TimesActivity.this);
checkOrientation();
new FetchDataTask().execute();
}
private class FetchDataTask extends AsyncTask<Void, Void, List<ValueList>>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(TimesActivity.this);
pDialog.setTitle("Please Wait");
pDialog.setMessage("Loading data...");
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setIndeterminate(true);
pDialog.show();
}
@Override
protected List<ValueList> doInBackground(Void... params) {
itemsList = new ArrayList<>();
try {
databaseAdapter.open();
for (int position = 0; position<= databaseAdapter.getData().size(); position++){
String name = databaseAdapter.getData().get(position).getName();
String value = databaseAdapter.getData().get(position).getValue();
String date = databaseAdapter.getData().get(position).getDate();
String day = databaseAdapter.getData().get(position).getDay();
itemsList.add(new ValueList(name, value, date, day));
}
databaseAdapter.close();
} catch (SQLException e) {
e.printStackTrace();
}
return itemsList;
}
@Override
protected void onPostExecute(List<ValueList> valueLists) {
super.onPostExecute(valueLists);
pDialog.dismiss();
listAdapter = new TimesAdapter(TimesActivity.this, valueLists);
recyclerView.setAdapter(listAdapter);
}
}
和我的数据库Adapter
类
public List<String> getName(){
db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
if (cursor != null ) {
if (cursor.moveToFirst()) {
do {
String day = cursor.getString(cursor.getColumnIndex("NAME"));
names.add(day);
}while (cursor.moveToNext());
}
cursor.close();
}
db.close();
return names;
}
timesAdapter类:
public class TimesAdapter extends RecyclerView.Adapter<TimesAdapter.TimesViewHolder>{
Context context;
List<ValueList> items;
public TimesAdapter(Context context, List<ValueList> items) {
this.context = context;
this.items = items;
}
@Override
public TimesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_item, parent, false);
TimesViewHolder holder = new TimesViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(TimesViewHolder holder, int position) {
holder.name.setText(items.get(position).getName());
holder.value.setText(items.get(position).getValue());
holder.day.setText(items.get(position).getDay());
holder.date.setText(items.get(position).getDate());
}
@Override
public int getItemCount() {
return 0;
}
public class TimesViewHolder extends RecyclerView.ViewHolder{
private TextView name, value, day, date;
public TimesViewHolder(View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.name_column);
value = (TextView)itemView.findViewById(R.id.value_column);
day = (TextView)itemView.findViewById(R.id.day_column);
date = (TextView)itemView.findViewById(R.id.date_column);
}
}
}
和ValueList类:
public class ValueList {
private String name, value, date, day;
public ValueList(String name, String value, String date, String day) {
this.name = name;
this.value = value;
this.date = date;
this.day = day;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDay() {
return day;
}
public void setDay(String day) {
this.day = day;
}
}
有什么想法吗?对话框永远不会消失,数据也不会显示。