我想在活动A中的listView
下面有两个按钮。但是现在,在活动A中按钮甚至没有显示listView
。
under_list_view_button.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="181dp"
android:layout_height="wrap_content"
android:id="@+id/addClaims"
android:layout_marginLeft="15px"
android:drawableRight="@mipmap/claims"
android:text="Add Claims"/>
<Button
android:layout_width="130dp"
android:layout_height="wrap_content"
android:id="@+id/btnSave"
android:drawableRight="@mipmap/submit"
android:layout_marginLeft="450px"
android:text="Submit" />
</FrameLayout>
活动A
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.work_details);
listview = (ListView) findViewById(R.id.listView);
btnAddClaims=(Button)findViewById(R.id.addClaims);
btnSubmit=(Button)findViewById(R.id.btnSave);
FrameLayout footerLayout = (FrameLayout)getLayoutInflater().inflate(R.layout.under_list_view_button, null);
btnSubmit = (Button) footerLayout.findViewById(R.id.btnSave);
btnAddClaims=(Button)footerLayout.findViewById(R.id.addClaims);
listview.addFooterView(footerLayout);
objMyCustomBaseAdapter=new MyCustomBaseAdapter(getApplicationContext(),results);
listview.setAdapter(objMyCustomBaseAdapter);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.addDetails:
mClickedPosition=-1;
Intent intent = new Intent(getApplication(), B.class); // go to B class
startActivityForResult(intent, PROJECT_REQUEST_CODE);
return true;
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { // receive from Activity B and populate ListView A
ReceiveProject = data.getStringExtra("Project");
ReceiveDescription = data.getStringExtra("Description");
if(mClickedPosition==-1) { // add returned value to new list
MyCustomBaseAdapter objMyCustomBaseAdapter = (MyCustomBaseAdapter) listview.getAdapter();
objMyCustomBaseAdapter.addNewItem(ReceiveProject, ReceiveDescription);
}
else
{
objMyCustomBaseAdapter= // update list (MyCustomBaseAdapter)listview.getAdapter();
objMyCustomBaseAdapter.changeItem(mClickedPosition,ReceiveProject,ReceiveDescription);
}
}
MyCustomBaseAdapter
public class MyCustomBaseAdapter extends BaseAdapter{ // for ListView
private static ArrayList<SearchResults> searchArrayList;
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public void addNewItem(String P,String D)
{
SearchResults obj=new SearchResults();
obj.setProject(" Project/Service/Training : "+P);
obj.setDescription(" Work Description : " + D);
searchArrayList.add(obj);
this. notifyDataSetChanged();
}
public void changeItem(int m,String P)
{
SearchResults obj=new SearchResults();
obj.setProject(" Project/Service/Training : "+P);
obj.setDescription(" Work Description : " + D);
searchArrayList.set(m,obj);
this. notifyDataSetChanged();
}
activity_a
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/background_work_details">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fillViewport="true"
android:orientation="vertical" >
<AbsoluteLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:layout_width="wrap_content"
android:layout_height="397dp"
android:id="@+id/listView"
android:layout_x="3dp"
android:layout_y="102dp" />
</AbsoluteLayout>
</ScrollView>
</LinearLayout>
我在A
中甚至没有listView所以我现在有两个问题
当我尝试将值从B返回到A时,应用程序崩溃。它在此行显示错误MyCustomBaseAdapter objMyCustomBaseAdapter = (MyCustomBaseAdapter) listview.getAdapter();
LogCat错误
进程:com.example.project.myapplication,PID:6686 java.lang.RuntimeException:传递结果失败ResultInfo {who = null,request = 1,result = -1,data = Intent {(has extras) 活动 {com.example.project.myapplication / com.example.project.myapplication.GUI.WorkDetailsTable}: java.lang.ClassCastException:android.widget.HeaderViewListAdapter 无法施展 com.example.project.myapplication.Adapter.MyCustomBaseAdapter 在android.app.ActivityThread.deliverResults(ActivityThread.java:3681)
已编辑MyCustomBaseAdapter
public class MyCustomBaseAdapter extends BaseAdapter{ // for ListView
private static ArrayList<SearchResults> searchArrayList;
FrameLayout footerLayout;
private LayoutInflater mInflater;
ListView listview;
// AbsoluteLayout footer;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results,ListView listview,FrameLayout footerLayout) {
searchArrayList = results;
this.listview=listview;
this.footerLayout=footerLayout;
mInflater = LayoutInflater.from(context);
//this.footer=footer;
addOrRemoveFooter();
}
public void addOrRemoveFooter(){
if(searchArrayList.size()==0 && listview.getFooterViewsCount()==0){
listview.removeFooterView(footerLayout);
}
else
{
listview.addFooterView(footerLayout);
}
}
虽然searchArrayList.size > 0
仍未显示该按钮。如果我将代码更改为
if(searchArrayList.size()==0 && listview.getFooterViewsCount()==0){
listview.addFooterView(footerLayout);
}
即使是多个列表,也会出现按钮。
答案 0 :(得分:1)
我认为您需要包含
under_list_view_button
到
&#34; activity_a&#34;
在activity_a
中的ListView下添加:<include layout="@layout/under_list_view_button"/>
答案 1 :(得分:1)
这是我的解决方案
在您的活动A
public class A extends Activity{
FrameLayout footerLayout;
public void onCreate(Bundle savedInstanceState) {
...
footerLayout = (FrameLayout)getLayoutInflater().inflate(R.layout.under_list_view_button, null);
btnSubmit = (Button) footerLayout.findViewById(R.id.btnSave);
btnAddClaims=(Button)footerLayout.findViewById(R.id.addClaims);
objMyCustomBaseAdapter = new MyCustomBaseAdapter(getApplicationContext(),results,listview, footerLayout);
listview.setAdapter(objMyCustomBaseAdapter);
}
}
在适配器中
public class MyCustomBaseAdapter extends BaseAdapter{ // for ListView
private static ArrayList<SearchResults> searchArrayList;
ListView listview;
FrameLayout footerLayout
private LayoutInflater mInflater;
public MyCustomBaseAdapter(Context context, ArrayList<SearchResults> results, ListView listview, FrameLayout footerLayout) {
searchArrayList = results;
this.listview = listview;
this.footerLayout = footerLayout;
addOrRemoveFooter();
}
public void addOrRemoveFooter(){
if(searchArrayList.size() == 0 && listView.getFooterViewsCount() > 0){
listview.removeFooterView(footerLayout);
}else if(listView.getFooterViewsCount() == 0 && searchArrayList.size() > 0){
listview.addFooterView(footerLayout);
}
}
public void addNewItem(String P,String D){
addOrRemoveFooter();
...
}
public void changeItem(int m,String P){
addOrRemoveFooter();
...
}
public void removeItem(int position){
addOrRemoveFooter();
...
}
希望这个帮助