我有ListView
,其中包括2个EditText
,我想在活动开始时初始化6个空的EditText
。该怎么办?
我无法添加列表空模型并进行设置,所以我不知道该如何处理。
这是listview_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/SerialNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:ellipsize="end"
android:padding="5dp"
android:singleLine="true">
<requestFocus />
</EditText>
<EditText
android:id="@+id/countItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:padding="5dp"
android:singleLine="true"
android:inputType="number">
</EditText>
这是我的布局;
activity_reader.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content""
android:text="btn" />
<GridLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@null" />
</GridLayout>
这是ListView适配器;
public class ListViewAdapter extends BaseAdapter {
public ArrayList<Model> productList;
Activity activity;
ViewHolder holder;
public ListViewAdapter(Activity activity, ArrayList<Census> productList) {
super();
this.activity = activity;
this.productList = productList;
}
@Override
public int getCount() {
return productList.size();
}
@Override
public Object getItem(int position) {
return productList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder {
EditText SerialNumber;
EditText countItem;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = activity.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.listview_row, null);
holder = new ViewHolder();
holder.SerialNumber = (EditText) convertView.findViewById(R.id.SerialNumber );
holder.countItem = (EditText) convertView.findViewById(R.id.countItem );;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.SerialNumber.addTextChangedListener(new TextWatcher() { @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
holder.SerialNumber.requestFocus();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
holder.censusRowSerialNumber.requestFocus();
}
@Override
public void afterTextChanged(Editable s) {
//holder.countItem.requestFocus();
}
});
Model item = productList.get(position);
holder.SerialNumber.setText(item.getID().toString());
holder.countItem.setText(String.valueOf(item.getQuantity()));
return convertView;
}
}
这里是活动;
public class ReaderActivity extends AppCompatActivity {
EditText countItem,SerialNumber;
private ArrayList<Model> csList;
Model item;
ListViewAdapter adapter;
ListView lview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reader);
csList = new ArrayList<Model>();
lview = (ListView) findViewById(R.id.listview);
adapter = new ListViewAdapter(this, csList);
SerialNumber= findViewById(R.id.SerialNumber);
countItem = findViewById(R.id.countItem );
lview.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
答案 0 :(得分:0)
要实现CRUD
操作,最好使用自定义布局。
例如,一个LinearLayout
的默认布局已经定义。现在,根据运行时要求/参数,您可以添加/编辑/删除所有这些。为此,逻辑可以完全由您决定。比创建静态视图和进行操作更方便。
让我向您展示自定义布局的准备工作并添加到rootiew中。我举了一个示例,其中布局landing_hori_view
具有EditText
和Button
public class LandingHorizontalView extends LinearLayout implements View.OnClickListener{
public LandingHorizontalView(Context context){
super(context);
initialize();
}
public LandingHorizontalView(Context context, ModelClassObject modelObject,ButtonClicked listener){
super(context);
mTypeContent = modelObject;//If you want to pass some params for population
mListener = listener;
initialize();
}
private void initialize(){
View view = LayoutInflater.from(getContext()).inflate(R.layout.landing_hori_view, this, true);
EditText edtOne =findViewById(R.id.txt_price_list);
Button button= findViewById(R.id.btn_place_order);
button.setTag(count);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v instanceof Button){
int count = (int) v.getTag();
mListener.onButtonClicked(count);
}
}
public interface ButtonClicked{
public void onButtonClicked(int index);
}
}
在您的父级活动中,实现界面(此处为ButtonClicked
)并处理点击事件/操纵UI等
@Override
public void onButtonClicked(int index) {
}
现在进入,如何将它们添加到父级布局,创建一个ScrollView
并包含一个LinearLayout
并将添加到LandinghorizontalView
的位置。
下面显示了我父母活动的内容
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/white"
app:layout_constraintTop_toBottomOf="@id/guideline"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/landingScrollLinearView"
>
</LinearLayout>
现在添加此vew,可以完成以下操作。
LandingHorizontalView horizontalView=new LandingHorizontalView(getActivity(),modelObject,this);
mScrollLinearView.addView(horizontalView);
现在按照您的评论,创建几个
LandingHorizontalView
并添加 扎根。现在,根据您的逻辑/用户交互,继续 修改mScrollLinearView