1
如何設置程序創建的按鈕的屬性?例如:在我的例子中,我想爲我的「Place」按鈕創建一個「android:layout_weight =」1「」和「on_click =」startsearch「」屬性。如何設置以編程方式創建的按鈕的屬性?
List<DB_Verlauf_Contact> placeList = db.getAllDBPlaces();
LinearLayout layout = (LinearLayout) findViewById(R.id.verlauf_liste);
for (DB_Verlauf_Contact cn : placeList) {
String log = "Id: "+cn.getID()+" ,Place ID: " + cn.getPlace_id() + " ,Name: " + cn.getName()+ " ,Longitude: " + cn.getLongitude()+ " ,Latitude: " + cn.getLatitude();
LinearLayout row = new LinearLayout(this);
row.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
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);
Button Delete = new Button(this);
Delete.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Delete.setText("Löschen");
Delete.setId(cn.getID());
row.addView(Delete);
layout.addView(row);
Log.d("Name: ", log);
}
謝謝,這工作到目前爲止,但我如何在onclicklistener內調用我的類「UI_MainActivity」的方法「public void openSettings(View view)」。對不起,我對Java很陌生。 – Glave
如果你在'Fragment'中做((UI_MainActivity)getActivity())。openSettings(view); –
我不知道片段是什麼,但這不適合我。如果這些信息以任何方式提供幫助,這些類都在同一個包中。 – Glave