Listview多次显示一个项目

时间:2015-02-11 14:41:26

标签: android sqlite listview

我正在尝试使用SQLite DB创建一个Android列表。创建新项目时没有问题,在返回列表活动后,项目会显示不同的时间。仍然在我的数据库中只插入一个新项目(应该),当我重新启动应用程序并加载列表时,每个项目都显示一次。所以这是我的主要活动,正如我所说的DBHandler应该没问题。

public class MainActivity extends Activity {

List<FavImages> FavImages = new ArrayList<FavImages>();
ListView favImageListView;

final Context context = this;

private SharedPreferences mPrefs;
private SharedPreferences.Editor mEditor;

//label logs
private static String logtag = "CameraApp";
//use main camera
private static int TAKE_PICTURE = 1;

private Uri imageUri;
public Uri imagePath = Uri.parse("android.resource://com.adrian/drawable/no_picture.png");
DataBaseHandler dbHandler;
int longClickedItemIndex;
ArrayAdapter<FavImages> favImagesAdapter;

private static final int EDIT = 0, DELETE = 1;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    favImageListView = (ListView) findViewById(R.id.listView);
    dbHandler = new DataBaseHandler(getApplicationContext());

    mPrefs = PreferenceManager.getDefaultSharedPreferences(context);
    mEditor = mPrefs.edit();


    //enter an Item
    registerForContextMenu(favImageListView);
    //maybe without long
    favImageListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            //position of Item
            longClickedItemIndex = position;
            return false;
        }
    });

    populateList();

    //Button Action
    Button cameraButton = (Button)findViewById(R.id.button_camera);
    cameraButton.setOnClickListener(cameraListener);
}

private OnClickListener cameraListener = new OnClickListener(){
    public void onClick(View v){
        takePhoto(v);
    }
};

//launch native camera app
private void takePhoto(View v){
    final Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

    //save Image and create file

    // in Progress
    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.text_entry, null);
    final EditText input1 = (EditText) textEntryView.findViewById(R.id.pictureName);
    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    //create Dialog
    alert
            .setTitle("Bitte bennenen Sie Ihr Bild!")
            .setView(textEntryView)
            .setPositiveButton(R.string.alert_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Log.i("AlertDialog","TextEntry 1 Entered "+input1.getText().toString());
                            /* User clicked OK so do some stuff */
                            String inputText = input1.getText().toString();

                            mEditor.putString("pictureName", inputText);
                            mEditor.commit();

                            File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), replaceChars(mPrefs.getString("pictureName", "picture")) + ".jpg");
                            //access information of file
                            imageUri = Uri.fromFile(photo);
                            //save image path information
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

                            //for favImages
                            imagePath = imageUri;
                            mEditor.putString("picturePath", imagePath.toString());
                            mEditor.commit();
                            //
                            startActivityForResult(intent, TAKE_PICTURE);

                            //
                            Log.e("Dateipfad", imagePath.toString());
                            FavImages favImages = new FavImages(dbHandler.getFavCount(), mPrefs.getString("pictureName", "Bild"), imagePath);
                            dbHandler.createFav(favImages);
                            FavImages.add(favImages);
                            //favImagesAdapter.notifyDataSetChanged();
                            populateList();

                            List<FavImages> addableFavs = dbHandler.getAllFav();
                            int favCount = dbHandler.getFavCount();

                            for(int i = 0; i < favCount; i++){
                                FavImages.add(addableFavs.get(i));
                            }

                            if (!addableFavs.isEmpty())
                                populateList();
                        }
                    });
    //show Dialog
    alert.show();


}

public String replaceChars (String inputText){
    inputText = inputText.replace("ä","ae");
    inputText = inputText.replace("ö","oe");
    inputText = inputText.replace("ü","ue");
    inputText = inputText.replace("ß","ss");

    return inputText;
}

//deal with output
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    //user hits ok button (picture accepted)
    if(resultCode == Activity.RESULT_OK){
        Uri selectedImage = imageUri;
        //communication between apps
        getContentResolver().notifyChange(selectedImage, null);

        /*get Image
        ImageView imageView = (ImageView)findViewById(R.id.image_camera);
        //hold Image data
        ContentResolver cr = getContentResolver();
        Bitmap bitmap;

        //get bitmap data
        try {
            bitmap = MediaStore.Images.Media.getBitmap(cr, selectedImage);
            //set Image
            imageView.setImageBitmap(bitmap);
            //notify user of success
            Toast.makeText(MainActivity.this, selectedImage.toString(), Toast.LENGTH_LONG).show();
        }catch (Exception e){ //catch exceptions along the way
            Log.e(logtag, e.toString());
        } */
    }
}

//
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo){
    super.onCreateContextMenu(menu, view, menuInfo);

    menu.setHeaderTitle("Favorit bearbeiten");
    menu.add(Menu.NONE, DELETE, menu.NONE, "Favorit löschen");
}

public boolean onContextItemSelected (MenuItem item){
    switch (item.getItemId()){
        case EDIT:
            //TODO: edit Favorites
            break;
        case DELETE:
            //
            dbHandler.deleteFav(FavImages.get(longClickedItemIndex));
            FavImages.remove(longClickedItemIndex);
            favImagesAdapter.notifyDataSetChanged();
            break;
    }
    return super.onContextItemSelected(item);
}

private void populateList(){
    //ArrayAdapter<FavImages> adapter = new favImagesListAdapter();
    //favImageListView.setAdapter(adapter);
    favImagesAdapter = new favImagesListAdapter();
    favImageListView.setAdapter(favImagesAdapter);
}

//Constructor for List Items
private class favImagesListAdapter extends ArrayAdapter<FavImages>{
    public favImagesListAdapter(){
        super (MainActivity.this, R.layout.listview_item, FavImages);
    }

    @Override
    public View getView (int position, View view, ViewGroup parent){
        if (view == null)
            view = getLayoutInflater().inflate(R.layout.listview_item, parent, false);

        FavImages currentFav = FavImages.get(position);

        TextView favName = (TextView) view.findViewById(R.id.favName);
        favName.setText(currentFav.getImageName());
        ImageView ivFavsImage = (ImageView) view.findViewById(R.id.favImage);
        ivFavsImage.setImageURI(currentFav.getImagePath());

        return view;
    }
}

总而言之,我做错了什么,我的物品被加载了很多次。

修改

这里要求的是我的DBHandler类:

<!-- language: lang-java -->
public class DataBaseHandler extends SQLiteOpenHelper{

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "favoritesManager",
TABLE_FAVS = "favorites",
KEY_ID = "id",
KEY_IMGName = "name",
KEY_IMGPATH = "imagePath";

//standard DB method
public DataBaseHandler(Context context){
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

//standard DB method
@Override
public void onCreate(SQLiteDatabase db){
    db.execSQL("CREATE TABLE " + TABLE_FAVS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_IMGName + " TEXT, " + KEY_IMGPATH + " TEXT)");
}

//standard DB method
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL("DROP TABL IF EXISTS" + TABLE_FAVS);

    onCreate(db);
}

public void createFav(FavImages favImages){
    SQLiteDatabase db = getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_IMGName, favImages.getImageName());
    values.put(KEY_IMGPATH, favImages.getImagePath().toString());

    db.insert(TABLE_FAVS, null, values);
    db.close();
}
public void deleteFav(FavImages favImages){
    SQLiteDatabase db = getWritableDatabase();

    db.delete(TABLE_FAVS, KEY_ID + "=?", new String[]{String.valueOf(favImages.getId())});
    db.close();
}
public int getFavCount(){
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_FAVS, null);
    int count = cursor.getCount();

    cursor.close();
    db.close();

    return count;
}

public List<FavImages> getAllFav(){
    List<FavImages> favImages = new ArrayList<FavImages>();

    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_FAVS, null);

    if (cursor.moveToFirst()){
        do {
            favImages.add(new FavImages(Integer.parseInt(cursor.getString(0)), cursor.getString(1), Uri.parse(cursor.getString(2))));
        }while (cursor.moveToNext());
    }
    cursor.close();
    db.close();
    return favImages;
}

2 个答案:

答案 0 :(得分:0)

您好需要使用视图持有者模式,请参阅下面提供的链接:

http://ricston.com/blog/optimising-listview-viewholder-pattern/

答案 1 :(得分:0)

好了看完代码后,我发现自己的错误,我将相同的项目多次添加到列表中。我知道的愚蠢的错误,但现在它运作正常。

仍然,谢谢你的帮助。