1.ui界面:如图,
代码如下:
<androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:textColor="@color/black" android:gravity="center_vertical" android:textSize="@dimen/fs14" android:text="@string/towerinfo_dnms_fwfb" android:padding="@dimen/dp5"/> <androidx.appcompat.widget.LinearLayoutCompat android:id="@+id/ll_fwfb_add" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="2" android:gravity="center_vertical" android:minHeight="@dimen/dp32" android:orientation="vertical"> <com.gy.nwe_fieldcollection.utils.AddListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_fw" /> <ImageView android:id="@+id/et_info_fwfb_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/ic_add"/> </androidx.appcompat.widget.LinearLayoutCompat>
很普通的线性布局嵌套,其中有个AddListView,是我本人自定义的Listview,自定义的原因是滑动条ScrollView与ListView的底层代码有冲突,可以百度查询冲突点,
AddListView代码如下:
package com.gy.nwe_fieldcollection.utils; import android.content.Context; import android.util.AttributeSet; import android.widget.ListView; public class AddListView extends ListView { public AddListView(Context context) { super(context); } public AddListView(Context context, AttributeSet attrs) { super(context, attrs); } public AddListView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public AddListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
下面进行代码逻辑分析:1.实现动态添加Listview功能(就是主界面可以随着副界面操作而加信息条)
(1).首先添加一个要动态添加的界面
这是我创建的一个textview
代码如下:
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_weight="3" android:layout_height="wrap_content" android:id="@+id/tv_name" android:textColor="@color/black" android:textSize="@dimen/fs16" android:padding="@dimen/dp5" />
这个是单独的一个界面,就是单独的一个xml文件。
(2).动态添加就需要一个适配器(适配器本人也不大懂,本人理解就是指挥作用哈哈)
代码如下:(这是本人师傅写的,本人技术就像我的名字一样,小白,且菜)
package com.gy.nwe_fieldcollection.adpter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.gy.nwe_fieldcollection.R; import com.gy.nwe_fieldcollection.activities.EditInfo_DianqiActivity; import com.gy.nwe_fieldcollection.activities.MainActivity; import com.gy.nwe_fieldcollection.beans.Tower_structure; import java.util.ArrayList; public class List_FwAdapter extends BaseAdapter { private ArrayList<String> list; private Context context; public List_FwAdapter(ArrayList<String> list, Context context) { this.list = list; this.context = context; } @Override public boolean hasStableIds() { return true; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } public void refresh(ArrayList<String> list){ this.list = list; notifyDataSetChanged(); } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder viewHolder; if(view == null){ view = LayoutInflater.from(context).inflate(R.layout.item_list_fw, null); viewHolder = new ViewHolder(); viewHolder.tv = view.findViewById(R.id.tv_name); view.setTag(viewHolder); }else { viewHolder = (ViewHolder) view.getTag(); } if(list.get(i) != null){ viewHolder.tv.setText(list.get(i)); } viewHolder.tv.setOnClickListener(v2 ->{ if(context instanceof EditInfo_DianqiActivity){ ((EditInfo_DianqiActivity) context).viewFw(i); } }); return view; } static class ViewHolder{ TextView tv; } }
其中有些代码我还没有参悟!
(3)封装一个实体类,用来接收数据(因为要在副页面添加信息,并且返回主界面带主要信息)
代码如下:
package com.gy.nwe_fieldcollection.beans;
import java.io.Serializable;
import java.util.List;
public class FangwuBean implements Serializable {
private long id;
private String bh;
private String qd;
private String dz;
private String hz;
private String jg;
private int zflc;
private int fflc;
private double zfmj;
private double ffmj;
private String zmj;
private String zxpj;
private String clyj;
private String bz;
private List<String> photoes;
public long getId() {
return id;
}
public void setId(long id) {
this.id=id;
}
public String getBh() {
return bh;
}
public void setBh(String bh) {
this.bh = bh;
}
public String getQd() {
return qd;
}
public void setQd(String qd) {
this.qd = qd;
}
public String getDz() {
return dz;
}
public void setDz(String dz) {
this.dz = dz;
}
public String getHz() {
return hz;
}
public void setHz(String hz) {
this.hz = hz;
}
public String getJg() {
return jg;
}
public void setJg(String jg) {
this.jg = jg;
}
public int getZflc() {
return zflc;
}
public void setZflc(int zflc) {
this.zflc = zflc;
}
public int getFflc(String s) {
return fflc;
}
public void setFflc(int fflc) {
this.fflc = fflc;
}
public double getZfmj() {
return zfmj;
}
public void setZfmj(double zfmj) {
this.zfmj = zfmj;
}
public double getFfmj() {
return ffmj;
}
public void setFfmj(double ffmj) {
this.ffmj = ffmj;
}
public String getZmj() {
return zmj;
}
public void setZmj(String zmj) {
this.zmj = zmj;
}
public String getZxpj() {
return zxpj;
}
public void setZxpj(String zxpj) {
this.zxpj = zxpj;
}
public String getClyj() {
return clyj;
}
public void setClyj(String clyj) {
this.clyj = clyj;
}
public String getBz() {
return bz;
}
public void setBz(String bz) {
this.bz = bz;
}
public List<String> getPhotoes() {
return photoes;
}
public void setPhotoes(List<String> photoes) {
this.photoes = photoes;
}
public FangwuBean(){
}
public FangwuBean(long id, String bh, String qd, String dz, String hz, String jg,
int zflc, int fflc, double zfmj, double ffmj, String zmj,
String zxpj, String clyj, String bz, List<String> photoes) {
this.id = id;
this.bh = bh;
this.qd = qd;
this.dz = dz;
this.hz = hz;
this.jg = jg;
this.zflc = zflc;
this.fflc = fflc;
this.zfmj = zfmj;
this.ffmj = ffmj;
this.zmj = zmj;
this.zxpj = zxpj;
this.clyj = clyj;
this.bz = bz;
this.photoes = photoes;
}
@Override
public String toString() {
return "FangwuBean{" +
"id=" + id +
", bh='" + bh + '\'' +
", qd='" + qd + '\'' +
", dz='" + dz + '\'' +
", hz='" + hz + '\'' +
", jg='" + jg + '\'' +
", zflc=" + zflc +
", fflc=" + fflc +
", zfmj=" + zfmj +
", ffmj=" + ffmj +
", zmj='" + zmj + '\'' +
", zxpj='" + zxpj + '\'' +
", clyj='" + clyj + '\'' +
", bz='" + bz + '\'' +
", photoes=" + photoes +
'}';
}
}
(4).主界面的相关代码:(跳转)
ImageView et_info_fwfb_add;
private AddListView lv_fw;
private List_FwAdapter mList_fwAdapter;//声明添加图片,自定义的Addview,以及相对的适配器
onCreate方法里:
fwbhs = new ArrayList<>();//实例化编号的集合
mFangwuBeans = new ArrayList<>();//实例化一个房屋类
mList_fwAdapter = new List_FwAdapter(fwbhs, this);//实例化适配器,用编号填充
lv_fw = findViewById(R.id.lv_fw);//实例化自定义布局
lv_fw.setAdapter(mList_fwAdapter);//对布局使用适配器
case R.id.et_info_fwfb_add:
startActivityForResult(new Intent(EditInfo_DianqiActivity.this, AddFangwuActivity.class), REQUEST_FW_CODE);
break;//添加图片点击跳转到副界面
(5).副界面保存按钮代码也就是按了之后主界面自动添加信息条并附主信息
btn_save.setOnClickListener(v -> {
FangwuBean fangwuBean = new FangwuBean();//实例化房屋类,用于数据传输
if(!TextUtils.isEmpty(et_info_dianqi_fwbh.getText())){//判断相应的输入框有没有数据
fangwuBean.setBh(et_info_dianqi_fwbh.getText().toString());//有数据,就通过房屋类的setBh方法保存字符串
}
if (!TextUtils.isEmpty(et_info_dianqi_qd.getText())){
fangwuBean.setQd(et_info_dianqi_qd.getText().toString());
}
Intent intent = new Intent(AddFangwuActivity.this,AddFangwuActivity.class);
intent.putExtra("fw",fangwuBean);//把要传输的数据保存intent 第一个值是固定的,像钥匙一样,第二个就是咱的数据
setResult(RESULT_OK,intent);//个人理解就是像密码,暗号一样
finish();//必须调用
});
(6).主界面接收信息 回调
private static final int REQUEST_FW_CODE = 40;//搞个暗号
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {//对暗号
if (requestCode == REQUEST_FW_CODE) {//通道一样
if(data != null){
FangwuBean fangwuBean = (FangwuBean) data.getSerializableExtra("fw");//得到副界面依附于房屋类的数据
String bh = fangwuBean.getBh();//拿出数据
fwbhs.add(bh);//把数据存进集合
mFangwuBeans.add(fangwuBean);//添加数据
mList_fwAdapter = new List_FwAdapter(fwbhs, EditInfo_DianqiActivity.this);适配器
lv_fw.setAdapter(mList_fwAdapter);适配器操作
}
} else {
}
}
}
}
}
以上就实现主界面点击跳转副界面,副页面添加表格信息,保存后主界面显示信息条主信息,但是主信息条还不能点击查看详情
(7).给主信息条添加点击事件
1)设置点击跳转的页面,这里简称3页面(就是负责展示的页面)
代码就不附了。
直接核心代码:
首先从副界面保存数据(前面传输数据已将写过)
btn_save.setOnClickListener(v -> {
FangwuBean fangwuBean = new FangwuBean();
if(!TextUtils.isEmpty(et_info_dianqi_fwbh.getText())){
fangwuBean.setBh(et_info_dianqi_fwbh.getText().toString());
}
if (!TextUtils.isEmpty(et_info_dianqi_qd.getText())){
fangwuBean.setQd(et_info_dianqi_qd.getText().toString());
}
Intent intent = new Intent(AddFangwuActivity.this,AddFangwuActivity.class);
intent.putExtra("fw",fangwuBean);
setResult(RESULT_OK,intent);
finish();
});
}
2).主界面主信息条点击事件
public void viewFw(int i){
Intent intent = new Intent(EditInfo_DianqiActivity.this,DianqiInfoActivity.class);
intent.putExtra("fw", mFangwuBeans.get(i));//用mFangwuBeans接收信息
startActivity(intent);
}
3).3界面添加获取数据的方法
private void setValue(){
et_info_dianqi_fwbh.setText(mFangwuBean.getBh());
et_info_dianqi_qd.setText(mFangwuBean.getQd());
}
这样就实现了
//最后谢谢我师傅不厌其烦的帮我 哈哈哈 绝大多数代码是他完成的,记录一下,后续使用