是否可以在不使用XML代码的情况下将ListView
置于LinearLayout
或任何其他布局中?
这是我的原始代码:
public class PickEF extends Activity {
/*TextView bookingPeriod = new TextView(this);
bookingPeriod.setText("Your booking period are: "+this.getIntent().getStringExtra("checked"));
TextView available = new TextView(this);
available.setText("These are the available items during your booking period: ");
TableLayout tl = new TableLayout(this);
TableRow row1 = new TableRow(this);
TableRow row2 = new TableRow(this);
RadioGroup rg = new RadioGroup(this);
RadioButton r1 = new RadioButton(this);
r1.setText("Radio Button1");
RadioButton r2 = new RadioButton(this);
r2.setText("Radio Button2");
r1
rg.addView(r1);
rg.addView(r2);
tl.addView(rg);
setContentView(tl);*/
// Array of strings storing country names
String[] room = new String[] {
"N001",
"N003",
"N004",
"N007"
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] flags = new int[]{
R.drawable.tutorialroom,
R.drawable.tutorialroom,
R.drawable.tutorialroom,
R.drawable.tutorialroom
};
// Array of strings to store currencies
String[] capacity = new String[]{
"40",
"40",
"40",
"40",
};
// Array of strings storing country names
String[] lecture = new String[] {
"LDK1",
"LDK2",
"LDK4",
"LDK5"
};
// Array of integers points to images stored in /res/drawable-ldpi/
int[] icon = new int[]{
R.drawable.lecturehall,
R.drawable.lecturehall,
R.drawable.lecturehall,
R.drawable.lecturehall
};
// Array of strings to store currencies
String[] capacity2 = new String[]{
"140",
"140",
"140",
"140",
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pick_ef);
TextView bookingPeriod = new TextView(this);
bookingPeriod.setText("Your booking period: "+this.getIntent().getStringExtra("checked"));
TextView type = new TextView(this);
type.setText(this.getIntent().getStringExtra("type"));
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
if(type.equals("lhall"))
{
for(int i=0;i<4;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Room Number : " + lecture[i]);
hm.put("cur","Capacity : " + capacity2[i]);
hm.put("flag", Integer.toString(icon[i]) );
aList.add(hm);
}
}
else{
for(int i=0;i<4;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("txt", "Room Number : " + room[i]);
hm.put("cur","Capacity : " + capacity[i]);
hm.put("flag", Integer.toString(flags[i]) );
aList.add(hm);
}
}
// Each row in the list stores country name, currency and flag
// Keys used in Hashmap
String[] from = { "flag","txt","cur" };
// Ids of views in listview_layout
int[] to = { R.id.flag,R.id.txt,R.id.cur};
// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.listview_layout, from, to);
// Getting a reference to listview of main.xml layout file
ListView listView = ( ListView ) findViewById(R.id.listview);
// Setting the adapter to the listView
listView.setAdapter(adapter);
// Item Click Listener for the listview
OnItemClickListener itemClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View container, int position, long id) {
// Getting the Container Layout of the ListView
LinearLayout linearLayoutParent = (LinearLayout) container;
// Getting the inner Linear Layout
LinearLayout linearLayoutChild = (LinearLayout ) linearLayoutParent.getChildAt(1);
// Getting the Country TextView
TextView tvCountry = (TextView) linearLayoutChild.getChildAt(0);
Toast.makeText(getBaseContext(), tvCountry.getText().toString(), Toast.LENGTH_SHORT).show();
}
};
// Setting the item click listener for the listview
listView.setOnItemClickListener(itemClickListener);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(listView, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
setContentView(ll);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.pick_e, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是logcat:
08-22 19:08:06.529:E / AndroidRuntime(2961):致命异常:主要 08-22 19:08:06.529:E / AndroidRuntime(2961):处理:my.edu.utar.sofebos,PID:2961 08-22 19:08:06.529:E / AndroidRuntime(2961):java.lang.RuntimeException:无法启动活动ComponentInfo {my.edu.utar.sofebos / my.edu.utar.sofebos.PickEF}:java.lang .IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母上调用removeView()。 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.os.Handler.dispatchMessage(Handler.java:102) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.os.Looper.loop(Looper.java:136) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.app.ActivityThread.main(ActivityThread.java:5017) 08-22 19:08:06.529:E / AndroidRuntime(2961):at java.lang.reflect.Method.invokeNative(Native Method) 08-22 19:08:06.529:E / AndroidRuntime(2961):at java.lang.reflect.Method.invoke(Method.java:515) 08-22 19:08:06.529:E / AndroidRuntime(2961):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 08-22 19:08:06.529:E / AndroidRuntime(2961):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 08-22 19:08:06.529:E / AndroidRuntime(2961):at dalvik.system.NativeStart.main(Native Method) 08-22 19:08:06.529:E / AndroidRuntime(2961):引起:java.lang.IllegalStateException:指定的子节点已经有父节点。您必须首先在孩子的父母上调用removeView()。 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.view.ViewGroup.addViewInner(ViewGroup.java:3562) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.view.ViewGroup.addView(ViewGroup.java:3415) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.view.ViewGroup.addView(ViewGroup.java:3360) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.view.ViewGroup.addView(ViewGroup.java:3336) 08-22 19:08:06.529:E / AndroidRuntime(2961):at my.edu.utar.sofebos.PickEF.onCreate(PickEF.java:180) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.app.Activity.performCreate(Activity.java:5231) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 08-22 19:08:06.529:E / AndroidRuntime(2961):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 08-22 19:08:06.529:E / AndroidRuntime(2961):... 11更多
答案 0 :(得分:0)
您必须为列表视图提供某种布局限制或定义。
变化
ll.addView(lv);
到
ll.addView(lv, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
我认为它会正常工作。让我知道你的问题是否已解决。
答案 1 :(得分:0)
在ll.addView()
添加之前:
((ViewGroup)listview.getParent()).removeView(listview);