我试图从URL检索数据并将其存储在本地数据库中,然后将该数据附加到listview。 我想尝试新的,所以请不要介意代码有大错误或错误的程序...建议我怎么做。
我的数据库类是
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "placesmanager";
// Contacts table name
private static final String TABLE_PLACES = "places";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists TABLE_PLACES(name varchar(50),rating varchar(50),icon varchar(50))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLACES);
// Create tables again
onCreate(db);
}
//adding contact
public void addContact(PlacesData data) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", data.getName());
values.put("rating", data.getRating());
values.put("icon", data.getIcon());
// Inserting Row
db.insert(TABLE_PLACES, null, values);
db.close(); // Closing database connection
}
// Getting All Contacts
public ArrayList<PlacesData> getAllContacts() {
ArrayList<PlacesData> contactList = new ArrayList<>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_PLACES;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
PlacesData contact = new PlacesData();
contact.setName(cursor.getString(0));
contact.setRating(cursor.getString(1));
contact.setIcon(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
}
以及我想附加到数据库并尝试检索它的recyclelerview方法
public void onBindViewHolder(MyRecycler holder, final int position) {
DatabaseHandler handler = new DatabaseHandler(activity);
handler.addContact(new PlacesData(data.get(position).getName(),data.get(position).getRating(),data.get(position).getIcon()));
// Log.i("Database",handler+"");
ArrayList<PlacesData> contacts = handler.getAllContacts();
for (PlacesData cn : contacts) {
String log = "Name: "+cn.getName()+" ,Rating: " + cn.getRating() + " ,Icon: " + cn.getIcon();
Log.i("Name: ", log);
holder.tvName.setText(cn.getName());
holder.tvRating.setText(cn.getRating());
Picasso.with(activity).load(cn.getIcon()).into(holder.imgIcon);
}
}
});
将数据库数据附加到recyclerview已完成,但字段在视图中重复的方式不正确。在Android监视器中显示以下错误。
我这样做是通过跟踪一些谷歌示例建议我可能的更改。
答案 0 :(得分:3)
使你的适配器类像这样
public class PlacesDataAdapter extends RecyclerView.Adapter<PlacesDataAdapter.Viewholder>
{
ArrayList<PlacesData> PlacesDatalist;
Context context;
LayoutInflater inflater;
public PlacesDataAdapter(Context context,ArrayList<PlacesData> PlacesDatalist){
this.context=context;
this.surveylist=PlacesDatalist;
inflater= LayoutInflater.from(context);
}
@Override
public Viewholder onCreateViewHolder(ViewGroup parent, int viewType) {
final View view=inflater.inflate(R.layout.single_row,parent,false);
return new Viewholder(view);
}
@Override
public void onBindViewHolder(Viewholder holder, int position)
{
final PlacesData placedata=surveylist.get(position);
holder.tvName.setText(placedata.getName());
holder.tvRating.setText(placedata.getRating());
}
@Override
public int getItemCount() {
return PlacesDatalist.size();
}
}
从您的活动中的本地数据库中获取数据(每次清除arraylist以使值不会在列表中重复表示执行&#34; contacts.clear&#34;)
ArrayList<PlacesData> contacts = handler.getAllContacts();
之后只需创建适配器类的对象并在其中传递arraylist并执行notifydatasetchange。
PlacesDataAdapter adapter=new SurveyAdapter(this,contacts);
adapter.notifyDataSetChanged();
答案 1 :(得分:1)
在数据库的onCreate中声明一个ID。我认为这就是问题所在。每个表必须包含一个ID(主键),每个行都是唯一的。