0

所以我有一个 ListView 和一个自定义适配器,我正在向数据库添加值,然后重新填充列表,所以我以以下方式添加项目:q、w、e、r、t、y、u、i、o、 p 列表视图填充如下:q,w,e,r,t,y,q,q,q

这是自定义适配器

public class TestListAdapter extends BaseAdapter {
    private static ArrayList<FlashCard> flashCardItems;

    private LayoutInflater mInflater;
    private Context context; 
    public TestListAdapter(Context context, ArrayList<FlashCard> results) {
        this.context = context;
        flashCardItems = results;
        mInflater = LayoutInflater.from(context);

    }

    public int getCount() {
        return flashCardItems.size();
    }

    public Object getItem(int position) {
        return flashCardItems.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.flashcard, null);
            holder = new ViewHolder();
            holder.flipper = (ViewFlipper) convertView.findViewById(R.id.flashcard);
            convertView.setTag(holder);
        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.flipper.addView(FlashCard.createQuestionCard(context,flashCardItems.get(position).getQuestion()));


        return convertView;
    }

    static class ViewHolder {
        ViewFlipper flipper;
    }

}

这是数据库处理程序

public class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "mySchoolDB";

    // Flashcards table name
    private static final String TABLE_FLASHCARDS = "flashcards";

    // Flashcards Table Columns names
    private static final String KEY_ID = "id";
    private static final String QUESTION = "question";
    private static final String ANSWER = "answer";

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

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_FLASHCARDS_TABLE = "CREATE TABLE " + TABLE_FLASHCARDS + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + QUESTION + " TEXT,"
                + ANSWER + " TEXT" + ")";
        db.execSQL(CREATE_FLASHCARDS_TABLE);
    }

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

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new Flashcard
    public void addFlashcard(FlashCard flashcard) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(QUESTION, flashcard.getQuestion()); // flashcard Name
        values.put(ANSWER, flashcard.getAnswer()); // flashcard Phone

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

    // Getting single FlashCard
    public FlashCard getFlashcard(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

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

        FlashCard flashcard= new FlashCard(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2));
        cursor.close();
        db.close();
        // return FlashCard
        return flashcard;
    }

    // Getting All Flashcards
    public ArrayList<FlashCard> getAllFlashcards() {
        ArrayList<FlashCard> FlashcardList = new ArrayList<FlashCard>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_FLASHCARDS;

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

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                FlashCard flashcard = new FlashCard(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2));

                // Adding Flashcard to list
                FlashcardList.add(flashcard);
            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();
        // return Flashcard list
        return FlashcardList;
    }

    // Updating single Flashcard
    public int updateFlashcard(FlashCard flashcard) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(QUESTION, flashcard.getQuestion());
        values.put(ANSWER, flashcard.getAnswer());
        db.close();
        // updating row
        return db.update(TABLE_FLASHCARDS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(flashcard.getID()) });

    }

    // Deleting single Flashcard
    public void deleteFlashcard(FlashCard flashcard) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_FLASHCARDS, KEY_ID + " = ?",
                new String[] { String.valueOf(flashcard.getID()) });
        db.close();
    }


    // Getting Flashcards Count
    public int getFlashcardsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_FLASHCARDS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }


    public void deleteAllFlashCards(){
        SQLiteDatabase sdb= this.getWritableDatabase();
        sdb.delete(TABLE_FLASHCARDS, null, null);
        sdb.close();
    }



}

这是列表视图

public class TestList extends ListView
{
    private Context context;
    private ArrayList<FlashCard> stringArray = new ArrayList<FlashCard>();

    public TestList(Context context) {
        super(context);
        this.context = context;
        setResults();
    }

    public void setResults() {
        DatabaseHandler db = new DatabaseHandler(context);
        stringArray = db.getAllFlashcards();

        setAdapter(new TestListAdapter(context, stringArray));
    }

    public TestList(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void addFlashCardDialog() {
        final AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Add flashcard");
        final EditText question = new EditText(context);
        question.setHint("Question");
        final EditText answer = new EditText(context);
        answer.setHint("Answer");
        LinearLayout qa = new LinearLayout(context);
        qa.setOrientation(LinearLayout.VERTICAL);
        qa.addView(question);
        qa.addView(answer);
        builder.setView(qa);

        builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                FlashCard f = new FlashCard(question.getText().toString(), answer.getText().toString());
                DatabaseHandler db = new DatabaseHandler(context);
                db.addFlashcard(f);
                setResults();

            }
        });

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.show(); 
    }
}
4

2 回答 2

0

addView()不会替换当前存在的视图。当convertView返回被回收时,'q' 已经存在于 ' 中ViewFlipper,你希望 q 消失并被 'u' 替换

holder.flipper.addView(FlashCard.createQuestionCard(context,flashCardItems.get(position).getQuestion()));

尝试将视图翻转器内的视图设置为新值,而不是使用 addView()。

例子:

    TextView text = (TextView) ViewFlipper.findViewById(R.id.textView);
    text.setText(flashCardItems.get(position).getQuestion());
于 2013-08-08T16:28:15.300 回答
0

问题来自ViewFlipper您在适配器中使用的问题。

android中的列表视图使用视图回收(这是getView中convertView的用途)但是由于每次视图被回收时您如何处理它们,您将另一个视图添加到鳍状肢,您需要做的是添加视图当 convertView 为空,然后修改翻转器中的视图以表示您的数据时,不确定问题卡视图是如何工作的,所以我只是在那里猜测。

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.flashcard, null);
        holder = new ViewHolder();
        holder.flipper = (ViewFlipper) convertView.findViewById(R.id.flashcard);
        holder.card = FlashCard.createQuestionCard(context,flashCardItems.get(position).getQuestion());
        holder.filpper.addView(holder.card);
        convertView.setTag(holder);
    }
    else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.card.setQuestion(flashCardItems.get(position).getQuestion());


    return convertView;
}

显然你必须调整你的视图持有者并添加一个具有正确类型的卡片字段,并向 questionCard 视图添加一个 setQuestion 方法,但你明白了。

于 2013-08-08T16:31:57.277 回答