1) How to change the number of pictures shown dynamically according to the number of pics from the database?
就这部分而言,根据数据库中可用图片的数量使用 for 循环,您唯一需要的是知道数据库中存在的元素数量,然后像我在numberOfElements
这里使用的那样使用它
for(int i = 1; i <= numberOfElements ; i++) {
LinearLayout lHor = new LinearLayout(this);
lHor.setOrientation(LinearLayout.HORIZONTAL);
//lHor.setBackgroundColor(Color.rgb(238, 233, 191));
// Text View
TextView tv = new TextView(this);
tv.setText("FAULT "+i);
tv.setTextSize(20);
// tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.rgb(255,255,255));
tv.setPadding(12, 12, 12, 12);
tv.setId(i);
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
lHor.addView(tv); // Adding the TextView to the LinearLayout
//CheckBox
CheckBox cb = new CheckBox(this);
cb.setId(i);
cb.setTag("CheckBox");
cb.setClickable(false);
// cb.setChecked(true);
cb.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
cb.setGravity(Gravity.CENTER);
checkBoxes.add(cb);
lHor.addView(cb);
l.addView(lHor);
}
您可能已经知道如何获取数据库中的元素数量,如果没有,我使用了这个
// Method 3: Getting total number of entries present in the database
public int getTotalNumberOfEntries() {
String[] columns = new String[]{ KEY_ROWID, KEY_TYPE, KEY_DATE};
Cursor c = myDataBase.query(DATABASE_TABLE, columns, null, null, null, null, null);
int count = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
count++;
}
c.close();
return count;
}
========== 编辑 =================
您可以在活动的 onCreate() 方法中调用此方法
private void setDynamicContentViewOfThisPage() {
// Defining the Scroll View and the LinearLayout
ScrollView sv = new ScrollView(this);
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.VERTICAL);
sv.addView(l);
// You will need to collect data from the previous Intent :-)
TextView introduction = new TextView(this);
introduction.setText("Set Text Here");
introduction.setGravity(Gravity.LEFT);
introduction.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(introduction);
Button instructionsButton = new Button(this);
instructionsButton.setTag("Some Button");
instructionsButton.setId(987654321); // Random ID set to avoid conflicts :-D
instructionsButton.setText("Click to read all the instructions");
instructionsButton.setGravity(Gravity.CENTER);
instructionsButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(instructionsButton);
instructionsButton.setOnClickListener(this);
// Creates a line
TableLayout tl1 = new TableLayout(this);
View v1 = new View(this);
v1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v1.setBackgroundColor(Color.rgb(51, 51, 51));
tl1.addView(v1);
l.addView(tl1);
// Version 2 (Creating Different Layouts)
for(int i = 1; i <= 3 ; i++) {
// Creates a line
TableLayout tl2 = new TableLayout(this);
View v2 = new View(this);
v2.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v2.setBackgroundColor(Color.rgb(51, 51, 51));
tl2.addView(v2);
l.addView(tl2);
LinearLayout lHor = new LinearLayout(this);
lHor.setOrientation(LinearLayout.HORIZONTAL);
//lHor.setBackgroundColor(Color.rgb(238, 233, 191));
LinearLayout lVer1 = new LinearLayout(this);
lVer1.setOrientation(LinearLayout.VERTICAL);
lVer1.setGravity(Gravity.RIGHT);
lVer1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
// Text View
TextView tv = new TextView(this);
tv.setText("TV "+i);
tv.setTextSize(20);
// tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.rgb(255,255,255));
tv.setPadding(12, 12, 12, 12);
tv.setId(i);
// tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
lVer1.addView(tv); // Adding the TextView to the LinearLayout
//CheckBox
CheckBox cb = new CheckBox(this);
cb.setClickable(false);
// cb.setChecked(true);
cb.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
cb.setGravity(Gravity.CENTER);
lVer1.addView(cb);
lHor.addView(lVer1);
LinearLayout lVer = new LinearLayout(this);
lVer.setOrientation(LinearLayout.VERTICAL);
lVer.setGravity(Gravity.RIGHT);
lVer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
Button showsomeOtherButton = new Button(this);
showsomeOtherButton.setTag("showSomeButton");
showsomeOtherButton.setId(i);
showsomeOtherButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
showsomeOtherButton.setText("View Image");
// showsomeOtherButton.setEnabled(false);
lVer.addView(showsomeOtherButton);
Button someOtherDataButton = new Button(this);
someOtherDataButton.setId(i);
someOtherDataButton.setTag("someOtherButton");
someOtherDataButton.setText("Do this action " + i);
// someOtherDataButton.setEnabled(false);
someOtherDataButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
lVer.addView(someOtherDataButton);
showsomeOtherButton.setOnClickListener(this);
someOtherDataButton.setOnClickListener(this);
lHor.addView(lVer);
l.addView(lHor);
// Creates a line
TableLayout tl3 = new TableLayout(this);
View v3 = new View(this);
v3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v3.setBackgroundColor(Color.rgb(51, 51, 51));
tl3.addView(v3);
l.addView(tl3);
}
// Creates a line
TableLayout tl3 = new TableLayout(this);
View v3 = new View(this);
v3.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v3.setBackgroundColor(Color.rgb(51, 51, 51));
tl3.addView(v3);
l.addView(tl3);
Button nextPageButton = new Button(this);
nextPageButton.setTag("goToNExtPageButton");
nextPageButton.setId(98765432);
nextPageButton.setText("Go To Next Page");
nextPageButton.setGravity(Gravity.CENTER);
//nextPageButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity=Gravity.CENTER;
nextPageButton.setLayoutParams(layoutParams);
l.addView(nextPageButton);
// Set the content View to this
this.setContentView(sv);
}
}