Search code examples
androidlistviewandroid-adapterbaseadapter

initialize empty EditText in ListView Adapter


I have ListView and it include 2 EditTextI want to initialize 6 empty EditText when activity started.How can I do that?

I can not add list null Model and set it so I dont know how can i handle it.

This is 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>

This is my layout;

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>

Here is the ListView Adapter;

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;
}

}

Here is the Activity;

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();


}

Solution

  • For achieving CRUD operations, its good to go with Custom Layouts.

    For instance a LinearLayout, who's default layout is already defined. Now according to runtime requirement/parameters you can add/edit/delete all these. For which the logic can be completely yours. This is handy than creating a static view and manipulating.

    Let me show you custom layouts preparation and adding into a rootiew. I have given example where layout landing_hori_view is having an EditText and 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);
        }
    }
    

    In your parent activity, implement the interface (here ButtonClicked) and handle click events/manipulating UI etc

    @Override
        public void onButtonClicked(int index) {
    
    }
    

    Now coming to , how to add these to parent layout, create a ScrollView and include one LinearLayout to which LandinghorizontalView will be added.

    Below shows content of my parent Activity

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

    Now for adding this vew , the following can be done.

        LandingHorizontalView horizontalView=new LandingHorizontalView(getActivity(),modelObject,this);
        mScrollLinearView.addView(horizontalView);
    

    Now as per your comment, create few LandingHorizontalView and add to root. Now according to your logic/user interaction , keep on modifying mScrollLinearView