我的目的是从运行时增长列表视图中获取数据(如下图所示),其中包含Edittext和Spinners,并使用json将此数据发送到远程服务器。
我是在正确的道路上还是有另一种方法可供使用。
我想从listview中访问Edittext和Spinner,它在运行时展开。
我的问题是,每当添加新视图时,它都会重叠在上一个视图上。
相反,它应该添加到第一个视图下方。 (解决了这个问题。请参阅编辑后的代码。)
另一件事是如何访问edittext&每个视图的微调器?我想接受来自用户和&amp ;;的这些数据。将其保存在设备中。怎么做? (在private void SaveUserData() {}
方法中)。
以下是我的完整代码。
于2018年3月20日编辑
MainActivity.java
package tandel.amit.expandablelistview;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView listView;
Button buttonSave;
FloatingActionButton fab;
Adapter adapter;
int count = 0;
String[] Heading = {"User 1","User 2","User 3","User 4"};
String Name = "Name";
String ID = "ID";
String Gender = "Gender";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
buttonSave = findViewById(R.id.btnSave);
fab = findViewById(R.id.fabAdd);
buttonSave.setOnClickListener(onClickListener);
fab.setOnClickListener(onClickListener);
adapter = new Adapter(getApplicationContext(), R.layout.row_layout);
listView.setAdapter(adapter);
AddView(count++);
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnSave:
SaveUserData(); // Save the data to Local Database
break;
case R.id.fabAdd:
AddView(count++); // Add new View in listview
break;
default:
break;
}
}
};
private void AddView(int i) {
DataProvider dataProvider = new DataProvider(Heading[i],Name,Gender,ID);
adapter.add(dataProvider);
adapter.notifyDataSetChanged();
}
private void SaveUserData() {
}
}
Adapter.java
package tandel.amit.expandablelistview;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/*
* Created by Amit Tandel on 2/2/2018.
*/
public class Adapter extends ArrayAdapter{
List list = new ArrayList();
public Adapter(@NonNull Context context, int resource) {
super(context, resource);
}
@Override
public void add(@Nullable Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return this.list.size();
}
@Nullable
@Override
public Object getItem(int position) {
return this.list.get(position);
}
static class DataHandler{
TextView Heading,Name,Gender,ID;
EditText UserName,UserID;
Spinner UserGender;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
DataHandler dataHandler = new DataHandler();
if (convertView == null){ // If row is empty, we need to create row
LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.row_layout,parent,false);
dataHandler.Heading = convertView.findViewById(R.id.tvHeading);
dataHandler.Name = convertView.findViewById(R.id.tvName);
dataHandler.Gender = convertView.findViewById(R.id.tvGender);
dataHandler.ID = convertView.findViewById(R.id.tvID);
dataHandler.UserName = convertView.findViewById(R.id.etName);
dataHandler.UserID = convertView.findViewById(R.id.etID);
dataHandler.UserGender = convertView.findViewById(R.id.spGender);
convertView.setTag(dataHandler);
}else {
dataHandler = (DataHandler) convertView.getTag(); // If row already present then get that row
}
DataProvider dataProvider;
dataProvider = (DataProvider) this.getItem(position);
if (dataProvider != null) {
dataHandler.Heading.setText(dataProvider.getHeading());
dataHandler.Name.setText(dataProvider.getName());
dataHandler.Gender.setText(dataProvider.getGender());
dataHandler.ID.setText(dataProvider.getID());
}
return convertView;
}
}
DataProvider.java
package tandel.amit.expandablelistview;
/*
* Created by Amit Tandel on 2/2/2018.
*/
public class DataProvider {
private String Heading;
private String Name;
private String Gender;
private String ID;
public DataProvider(String Heading, String Name, String Gender, String ID) {
this.setHeading(Heading);
this.setName(Name);
this.setGender(Gender);
this.setID(ID);
}
public String getHeading() {
return Heading;
}
public void setHeading(String heading) {
Heading = heading;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getGender() {
return Gender;
}
public void setGender(String gender) {
Gender = gender;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context="tandel.amit.expandablelistview.MainActivity">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
n
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Save" />
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:id="@+id/fabAdd"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:src="@drawable/plus"
android:layout_marginBottom="80dp" />
</RelativeLayout>
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:id="@+id/tvHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="User 1"
android:textSize="18sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/tvName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Name"
android:textSize="16sp" />
<EditText
android:id="@+id/etName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/tvGender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gender"
android:textSize="16sp" />
<Spinner
android:id="@+id/spGender"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="@+id/tvID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ID"
android:textSize="16sp" />
<EditText
android:id="@+id/etID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="8dp"
android:background="#000000" />
</LinearLayout>