gpt4 book ai didi

android - 如何在 ListView 下方动态添加按钮

转载 作者:行者123 更新时间:2023-11-29 00:05:37 25 4
gpt4 key购买 nike

我想在 Activity A 中的 listView 下方有两个按钮。但是现在,按钮在 Activity 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>

Activity 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>

输出 enter image description here

我什至在 A 中没有 listView 也得到了这个

所以我现在有两个问题

  1. 如果 A 中没有 listView,如何隐藏两个按钮?
  2. 当我尝试将值从 B 返回到 A 时,应用程序崩溃了。它在此行显示错误 MyCustomBaseAdapter objMyCustomBaseAdapter = (MyCustomBaseAdapter) listview.getAdapter();

    LogCat 错误

Process: com.example.project.myapplication, PID: 6686 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.project.myapplication/com.example.project.myapplication.GUI.WorkDetailsTable}: java.lang.ClassCastException: android.widget.HeaderViewListAdapter cannot be cast to com.example.project.myapplication.Adapter.MyCustomBaseAdapter at 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);
}

即使列表不止一个,按钮也会出现。

最佳答案

这是我的解决方案
在你的 Activity 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();
...
}

希望对你有帮助

关于android - 如何在 ListView 下方动态添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34011732/

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