在不离开活动的情况下动态更改Imagebutton的onClickListener

时间:2016-11-13 14:45:38

标签: java android

我有ImageButton onClickListener。这将我的数据库中条目的“收藏”列设置为“1”或“0”并更改按钮的图像。刷新视图或活动的最佳方法是什么,onClickListener动态更改。

如果我没有说清楚:

  • 点击1:将收藏夹列更新为1,将图片更改为ic_star_black_48dp
  • 点击2:将收藏夹列更新为0,将图片更改为ic_star_border_black_48dp
  • 点击3:将收藏夹列更新为1,将图片更改为ic_star_black_48dp
  • 点击4:将收藏夹栏更新为0,将图片更改为ic_star_border_black_48dp

所有这一切都没有离开活动。

 star.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if (cn.getFavorite() == 0) {
                star.setImageResource(R.mipmap.ic_star_black_48dp);
                db_verladestellen.updatePlace(new Location(cn.getID(), cn.getPlace_id(), cn.getName(), cn.getLongitude(), cn.getLatitude(), cn.getTor(), 1));
            }
            else{
                star.setImageResource(R.mipmap.ic_star_border_black_48dp);
                db_verladestellen.updatePlace(new Location(cn.getID(), cn.getPlace_id(), cn.getName(), cn.getLongitude(), cn.getLatitude(), cn.getTor(), 0));
            }
        }

    });

编辑:

既然你问过,这就是全班。下面是方法getAllDBPlaces()

public class UI_Verladestellen extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.verladestellen);

        //Initialisation of the database
        final DB_Verladestellen db_verladestellen = new DB_Verladestellen(this);

        //Saves all entries from the database in a List
        List<Location> placeList = db_verladestellen.getAllDBPlaces();


        //Generates a button for each entry in the list
        for (final Location cn : placeList) {

            //Linear Layout for the buttons
            final LinearLayout layout = (LinearLayout) findViewById(R.id.verladestellen_liste);
            final LinearLayout row = new LinearLayout(this);
            row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

            //Favorites-Button
            final ImageButton star = new ImageButton(this);
            star.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT));
            if (cn.getFavorite() == 0) {star.setImageResource(R.mipmap.ic_star_border_black_48dp);}
            else {star.setImageResource(R.mipmap.ic_star_black_48dp);}
            star.setId(cn.getID());
            row.addView(star);
            star.setPadding(50,50,50,50);
            star.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (cn.getFavorite() == 0) {
                        star.setImageResource(R.mipmap.ic_star_black_48dp);
                        db_verladestellen.updatePlace(new Location(cn.getID(), cn.getPlace_id(), cn.getName(), cn.getLongitude(), cn.getLatitude(), cn.getTor(), 1));
                    }
                    else{
                        star.setImageResource(R.mipmap.ic_star_border_black_48dp);
                        db_verladestellen.updatePlace(new Location(cn.getID(), cn.getPlace_id(), cn.getName(), cn.getLongitude(), cn.getLatitude(), cn.getTor(), 0));
                    }
                }
            });

            //Location-Button
            Button place = new Button(this);
            place.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            place.setText(cn.getName());
            place.setId(cn.getID());
            row.addView(place);
            row.setId(cn.getID());
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) place.getLayoutParams();
            params.weight = 1.0f;
            place.setLayoutParams(params);
            place.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openMap(cn.getID());
                }
            });

            layout.addView(row);
        }
    }

    //Methode zum öffnen der Map in MainActivity. Übermittelt die ID des Buttons, von dem die Methode aufgerufen wird.
    //Diese ID ist identisch mit der ID des Ortes in der Datenbank, den der Button repräsentiert.
    public void openMap(int view) {
        Intent intent = new Intent(UI_Verladestellen.this, UI_MainActivity.class);
        intent.putExtra("findLocation", view);
        startActivity(intent);
    }
}

getALLDBPlaces:

public List<Location> getAllDBPlaces() {
    List<Location> placeList = new ArrayList<Location>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_DB_VERLADESTELLEN_Eintrag;

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Location location = new Location();
            location.setID(Integer.parseInt(cursor.getString(0)));
            location.setPlace_id(cursor.getString(1));
            location.setName(cursor.getString(2));
            location.setLongitude(cursor.getString(3));
            location.setLatitude(cursor.getString(4));
            location.setTor(cursor.getString(5));
            location.setFavorite(cursor.getInt(6));
            // Adding contact to list
            placeList.add(location);
        } while (cursor.moveToNext());
    }
    // return contact list
    return placeList;
}

1 个答案:

答案 0 :(得分:0)

在您编辑问题之后,我的答案会有所不同 首先,您应该检查适配器的实现 处理您的场景会更容易,并且更有效 但是,如果你遇到任何问题,那么你应该尝试自己实现它,然后再来吧。