gpt4 book ai didi

Android:如何结合膨胀布局和非膨胀布局?

转载 作者:搜寻专家 更新时间:2023-11-01 09:04:12 27 4
gpt4 key购买 nike

在我的 Activity 中,我想在顶部显示一些 TextView ,然后在其下方展开一个 ListView 。我尝试了各种方法但都没有成功,例如 addview() 但无法使其正常工作。我的膨胀 View 成为主要布局。

任何帮助将不胜感激。基于 Ted Hopp 解决方案的 LogCat 错误

10-20 02:38:38.322: D/AndroidRuntime(1253): 关闭虚拟机10-20 02:38:38.322: W/dalvikvm(1253): threadid=3: 线程以未捕获的异常退出 (group=0x4001b188)10-20 02:38:38.322: E/AndroidRuntime(1253): 未捕获的处理程序:由于未捕获的异常,线程主要退出10-20 02:38:38.342: E/AndroidRuntime(1253): java.lang.RuntimeException: 无法启动 Activity ComponentInfo{com.example.mycoursetimetable/com.example.mycoursetimetable.MyCourses}: java.lang.IllegalStateException: 无法将标题 View 添加到列表——setAdapter 已经被调用。10-20 02:38:38.342:E/AndroidRuntime(1253):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.app.ActivityThread.access$2200(ActivityThread.java:119)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)10-20 02:38:38.342: E/AndroidRuntime (1253): 在 android.os.Handler.dispatchMessage (Handler.java:99)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.os.Looper.loop(Looper.java:123)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.app.ActivityThread.main(ActivityThread.java:4363)10-20 02:38:38.342:E/AndroidRuntime(1253):在 java.lang.reflect.Method.invokeNative( native 方法)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 java.lang.reflect.Method.invoke(Method.java:521)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)10-20 02:38:38.342:E/AndroidRuntime(1253):在 dalvik.system.NativeStart.main( native 方法)10-20 02:38:38.342: E/AndroidRuntime(1253): Caused by: java.lang.IllegalStateException: Cannot add header view to list -- setAdapter 已经被调用。10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.widget.ListView.addHeaderView(ListView.java:256)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.widget.ListView.addHeaderView(ListView.java:279)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 com.example.mycoursetimetable.MyCourses.onCreate(MyCourses.java:30)10-20 02:38:38.342: E/AndroidRuntime (1253): 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1047)10-20 02:38:38.342: E/AndroidRuntime(1253): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)10-20 02:38:38.342: E/AndroidRuntime(1253): ... 11 更多10-20 02:38:38.382: I/dalvikvm(1253): threadid=7: 对信号 3 使用react10-20 02:38:38.382: E/dalvikvm(1253): 无法打开堆栈跟踪文件 '/data/anr/traces.txt': 权限被拒绝10-20 02:38:47.463: I/Process(1253): 发送信号。 PID:1253 SIG:9

activity_my_courses

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/main" >

<Button
android:id="@+id/labelAddCourseButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addCourseButton"
android:padding="10dp"
android:text="@string/CourseName" />

</LinearLayout>

列出类(class)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >



<TextView
android:id="@+id/labelModuleCode"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="10dp"
android:text="@string/CourseName"
android:textSize="10dp" />

<TextView
android:id="@+id/labelCourseType"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/CourseType"
android:layout_marginLeft="10dp"
android:textSize="10dp" />
</LinearLayout>

代码

public class MyCourses extends ListActivity {

static final String TEST = "com.example.mycoursetimetable.TEST";
String [] MODULE;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

LinearLayout main = (LinearLayout)this.findViewById(R.id.main);
MODULE = getResources().getStringArray(R.array.module);
setListAdapter(new ListArrayAdapter(this,MODULE));
}



public void addCourseButton (View addCourseButton)
{
Intent intent = new Intent(this,AddCourse.class);
startActivity(intent);
}

protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);


try {
Class test = Class.forName("com.example.MyCourseTimeTable.AddCourse");
Intent intent = new Intent(MyCourses.this, test);

TextView textView = (TextView) v.findViewById(R.id.labelModuleCode);
String module = textView.getText().toString();


intent.putExtra(TEST,module);
startActivity(intent);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}

}

}

class ListArrayAdapter extends ArrayAdapter<String>
{
// You only need one copy of LayoutInflater
private final LayoutInflater inflater;

//create the ArrayAdpater
public ListArrayAdapter(Context context, String[] test)
{
super(context, R.layout.activity_my_courses, test);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView == null) {


// Inflate the layout that you want for each row


convertView = inflater.inflate(R.layout.listcourses, parent, false);

holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.labelModuleCode);
holder.type = (TextView) convertView.findViewById(R.id.labelCourseType);

// Add a link for your button as well and define its OnClickListener here to, eventually...

convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();

String module = getItem(position);
//set the text to the string values based on position
holder.code.setText(module);

//return the layout
return convertView;
}

class ViewHolder {
TextView code;
TextView type;
}
}

最佳答案

与其尝试定义自己的 Activity 布局,不如使用如下内容:

public class MyCourses extends ListActivity {

static final String TEST = "com.example.mycoursetimetable.TEST";
String [] MODULE;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

MODULE = getResources().getStringArray(R.array.module);
getListView().addHeaderView(getHeader()); // must come before next line
setListAdapter(new ListArrayAdapter(this,MODULE));
}

private View getHeader() {
LayoutInflater inflater =
(LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.listcourses, null);
}
. . .
}

这将使 layout/listcourses.xml 中定义的 View 成为列表的标题。

关于Android:如何结合膨胀布局和非膨胀布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13160775/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com