【发布时间】:2011-12-20 04:07:19
【问题描述】:
我看the ListView API 我看到了方法:
addHeaderView(View v)
我想做的是在列表上方有一个布局,这可能吗?
我尝试做类似的事情:
EditText et=new EditText(this);
et.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
et.setInputType(InputType.TYPE_CLASS_TEXT);
addHeaderView(et); //makes app crash
我也试过了
setContentView(R.layout.tryview);
但它也会导致应用崩溃。
非常感谢您的帮助!
编辑:这个类的代码是:
public class GroupsActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String your_array_contents[] = {"a","ab","c"};
setListAdapter(new ArrayAdapter<String>(this, R.layout.groups_layout, your_array_contents));
EditText et=new EditText(this);
et.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
et.setInputType(InputType.TYPE_CLASS_TEXT);
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setAdapter(new ArrayAdapter<String>(GroupsActivity.this,
android.R.layout.simple_list_item_multiple_choice, your_array_contents));
lv.addHeaderView(et); //makes app crash
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
// Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
//Toast.LENGTH_SHORT).show();
}
});
}
}
【问题讨论】:
-
看我的回答。希望它会帮助你。另请参阅我在回答中提到的链接。
-
@user370305 - 看看我对你的回答的评论
-
什么是崩溃日志?我想你会得到 nullPointerException。对吗?
-
@user370305 - 我尝试在调试模式下运行它,但在“lv.addHeaderView(header);”上找不到资源
-
我建议的链接中的代码只是为了理解目的,你不能直接使用它,因为你不知道我在其中使用了哪个布局,只需简单的 layout.xml 文件对于标题,将其充气以查看并设置为 headerView()。