Android recyleview列表使用与数据库无法正常工作

时间:2017-01-12 05:10:00

标签: java android android-recyclerview android-database

我正在开发一个用于在数据库中列出详细信息的应用。列表不起作用。但它在日志中显示为罚款。为什么我的recyclerview listling不起作用。当我使用bean1 = new Bean(cn.getName(),cn.getNumber(),cn.getSpeeddial())时,它显示错误。

我的代码如下所示。

SpeedDialViewActivity.java

public class SpeedDialViewActivity extends AppCompatActivity {

    private List<Bean> beanList = new ArrayList<>();
    private RecyclerView recyclerView;
    private ViewAdapter mAdapter;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_speed_dial_view);
        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        mAdapter = new ViewAdapter(beanList);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);

        prepareData();


    }

    private void prepareData() {

        DatabaseHandler handler = new DatabaseHandler(getApplicationContext());
        Log.d("Reading: ", "Reading all contacts..");
        List<Bean> beanList = handler.getAllContacts();
        //Cursor cursor = (Cursor) handler.getAllContacts();

        //Bean bean1;

       for (Bean cn : beanList) {
            String log = "Id: " + cn.getId() + " ,Name: " + cn.getName() + " ,Phone: " + cn.getNumber();
            // Writing Contacts to log
            Log.d("Name: ", log);



          //  bean1 = new Bean(cn.getName(), cn.getNumber(), cn.getSpeeddial()); // Error occurring
           // beanList.add(bean1);
            }

    }
    }

    DatabaseHandler.java

    public class DatabaseHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "speeddial";
    private static final String TABLE_CONTACTS = "contacts";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";
    private static final String KEY_SPEED_DIAL = "speed_dial_number";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
                + KEY_PH_NO + " TEXT" + KEY_SPEED_DIAL + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }

    void addContact(Bean bean) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_NAME, bean.getName()); // Contact Name
        values.put(KEY_PH_NO, bean.getNumber()); // Contact Phone
        values.put(KEY_PH_NO, bean.getSpeeddial()); // Contact Phone

        // Inserting Row
        db.insert(TABLE_CONTACTS, null, values);
        db.close(); // Closing database connection
    }

    Bean getBean(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
                        KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Bean bean = new Bean(Integer.parseInt(cursor.getString(0)),cursor.getString(1), cursor.getString(2), cursor.getString(3));
        // return contact
        return bean;
    }

    public List<Bean> getAllContacts() {
        List<Bean> contactList = new ArrayList<Bean>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Bean contact = new Bean();
                contact.setId(Integer.parseInt(cursor.getString(0)));
                contact.setName(cursor.getString(1));
                contact.setNumber(cursor.getString(2));
                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }
    }

ViewAdapter.java

public class ViewAdapter extends RecyclerView.Adapter<ViewAdapter.MyViewHolder> {

private List<Bean> beanList;

public class MyViewHolder extends RecyclerView.ViewHolder {
    public TextView name, number, speeddial_number;

    public MyViewHolder(View view) {
        super(view);
        name = (TextView) view.findViewById(R.id.name);
        number = (TextView) view.findViewById(R.id.number);
        speeddial_number = (TextView) view.findViewById(R.id.speeddialNumber);
    }
}

public ViewAdapter(List<Bean> beanList){
    this.beanList = beanList;
}

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

@Override
public void onBindViewHolder(ViewAdapter.MyViewHolder holder, int position) {
    Bean bean = beanList.get(position);
    holder.name.setText(bean.getName());
    holder.number.setText(bean.getNumber());
    holder.speeddial_number.setText(bean.getSpeeddial());
}

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

Bean.java

public class Bean{
private int id;
private String name;
private String number;
private String speeddial;


public Bean(int id, String name, String number, String speeddial) {
    this.id=id;
    this.name=name;
    this.number=number;
    this.speeddial=speeddial;
}

public Bean(String name, String number, String speeddial) {
    this.name=name;
    this.number=number;
    this.speeddial=speeddial;
}

public Bean() {

}


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getNumber() {
    return number;
}

public void setNumber(String number) {
    this.number = number;
}

public String getSpeeddial() {
    return speeddial;
}

public void setSpeeddial(String speeddial) {
    this.speeddial = speeddial;
}
}

错误是java.lang.RuntimeException:无法启动活动ComponentInfo {com.app.appname / com.app.appname}:java.util.ConcurrentModificationException

请帮帮我

3 个答案:

答案 0 :(得分:1)

您没有在列表中添加要提取的联系人,也没有更改通知数据集。按如下所示更改onPrepare()方法

   private void prepareData() {

    DatabaseHandler handler = new DatabaseHandler(getApplicationContext());
    Log.d("Reading: ", "Reading all contacts..");
    List<Bean> beanList = handler.getAllContacts();
    this.beanList.addAll(beanList);
    mAdapter.notifyDatasetChanged();

}

答案 1 :(得分:0)

prepareData(beanList)中传递相同的列表,并在此列表中添加项目。在成功添加后,将方法更改为prepareData(List<Bean> beanList>).,只需调用mAdapter.notifyDataSetChanged()方法,以便适配器知道某些项目已添加到列表中。

答案 2 :(得分:0)

您没有在清单文件的应用程序标记中提及android:name。 因此,您只需使用上下文对象或使用 SpeedDialViewActivity.this

DatabaseHandler handler = new DatabaseHandler(mContext);

                         or

DatabaseHandler handler = new DatabaseHandler(SpeedDialViewActivity.this);

private void prepareData() {

    DatabaseHandler handler = new DatabaseHandler(SpeedDialViewActivity.this);
    Log.d("Reading: ", "Reading all contacts..");
    List<Bean> beanList = handler.getAllContacts();
    //Cursor cursor = (Cursor) handler.getAllContacts();

    //Bean bean1;

   for (Bean cn : beanList) {
        String log = "Id: " + cn.getId() + " ,Name: " + cn.getName() + " ,Phone: " + cn.getNumber();
        // Writing Contacts to log
        Log.d("Name: ", log);



      //  bean1 = new Bean(cn.getName(), cn.getNumber(), cn.getSpeeddial()); // Error occurring
       // beanList.add(bean1);
        }

}