2

I'm Working on Personal Details model, there have 2 radio option Married and Unmarried.On click of the Married radio button it will enable the Edit Text it will take the Number of Child of that Married person. Based on that entered value for the Edit Text. Dynamically Linear Layout is implementing. But The issues is when click on the Edit Text it's creating the Linear Layout(On erase of the Edit Text the Linear Layout also should erase).

My code is like this. In Main Activity

et_childCount.addTextChangedListener(new TextWatcher() {
   @Override
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
   @Override
       public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    @Override
       public void afterTextChanged(Editable s) {
           if (s != null) {
               countI = childCount;
               noOfChild = countI;
                if (noOfChild >= 1) {
                   for (int i = 1; i <= noOfChild; i++) {
                       LinearLayout linearLayout = new LinearLayout(getActivity());
                       LayoutParams params = new LinearLayout.LayoutParams(
                               LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                       linearLayout.setLayoutParams(params);
                       linearLayout.setOrientation(LinearLayout.VERTICAL);
                       TextView tv_BorderChildDetails = new TextView(getActivity());
                       tv_BorderChildDetails.setText("Child " + i);
                       tv_BorderChildDetails.setGravity(Gravity.CENTER_HORIZONTAL);
                       tv_BorderChildDetails.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                       linearLayout.addView(tv_BorderChildDetails);
                       EditText et_cName = new EditText(getActivity());
                       EditText et_cDob = new EditText(getActivity());
                       EditText et_cSchoolName = new EditText(getActivity());
                       et_cName.setHint("Child name");
                       et_cDob.setHint("Date of birth");
                       et_cSchoolName.setHint("School name");
                       childNameList.add(et_cName);
                       childDOBList.add(et_cDob);
                       childSchoolNameList.add(et_cSchoolName);
                       et_cName.setLayoutParams(params);
                       et_cDob.setLayoutParams(params);
                       et_cSchoolName.setLayoutParams(params);
                        linearLayout.addView(et_cName);
                       linearLayout.addView(et_cDob);
                       linearLayout.addView(et_cSchoolName); } } });
Anusha B
  • 91
  • 1
  • 10
  • Please anybody will give me the answer for this question – Anusha B Dec 19 '17 at 07:34
  • Sir will you please help me for this problem @Nithi Pate – Anusha B Dec 19 '17 at 07:37
  • elaborate your question with example. as per me you want to increase listview row (which is Edittext). else give me more detail on it – Yogesh Borhade Dec 19 '17 at 07:44
  • @Yogesh Borhade, I have one Edit Text Filed there user will give value as 1,2, 3 it's goes on. So when user give 1 means only one list view item should display(only one list item increment). If user gave Edit Text fied as 2 the 2 list view item should come. – Anusha B Dec 19 '17 at 07:50
  • @Yogesh Borhade ,setOnFocusChangeListener is not working for my EditText when click on that value is not taking the user give value is considering as "0" – Anusha B Dec 19 '17 at 08:02
  • instead of using focus you can use on textChangeListener and then you will do the further procedure.. – Yogesh Borhade Dec 19 '17 at 08:59
  • try with in textChangedListener – Yogesh Borhade Dec 19 '17 at 09:00
  • @Yogesh Boehade, will please give any link to refer because i'm new to this concept please. – Anusha B Dec 19 '17 at 09:02
  • 1)https://stackoverflow.com/questions/20824634/android-on-text-change-listener 2)https://android--code.blogspot.in/2015/08/android-edittext-textchangedlistener.html Please refer above link – Yogesh Borhade Dec 19 '17 at 09:03
  • @Yogesh Boehade, Thank You Sir. Will check. But actually Sir onFocuse is getting true but EditText value is not considering. – Anusha B Dec 19 '17 at 09:07
  • ok... go one by one , debug the code you will get the solution, step1) ontextChange check you r getting value which you are enter in edittext step 2)once you get the value do the further things like adding edttext by using count . – Yogesh Borhade Dec 19 '17 at 09:09
  • et_childCount.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus){ Toast.makeText(context, "on focus", Toast.LENGTH_LONG).show(); input = et_childCount.getText().toString(); System.out.print("inputVV" + input); //gettingVlaueOfEditText(focus); }else { Toast.makeText(getActivity(), "lost focus", Toast.LENGTH_LONG).show(); } } }); – Anusha B Dec 19 '17 at 09:09
  • @Yogesh Boehade, Ok Sir Thank i will check those links. and try to solve. – Anusha B Dec 19 '17 at 09:15
  • Why don't you better use a RecyclerView ? – anemo Dec 19 '17 at 09:20
  • @Sumit Anantwar, I'm not getting how to do because i don't have time. I'm not getting the logic that based on give value list item should increment – Anusha B Dec 19 '17 at 09:25

2 Answers2

0

You can use TextWatcher() like so:

editText.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {}

    @Override
    public void afterTextChanged(Editable s) {
        // Convert the editable to int.
        int userInput = Integer.parseInt(s.getText().toString());
        // First we want to copy the data ArrayList so we don't affect the original one.
        ArrayList<Data> new = copyArray(originalDataArrayList);
        // Then we want to run a for loop to change the item count depending on the userInput value.
        for (int i = 0; i < newArray.size(); i++){
          if (i >= userInput){
            newArray.remove(i);
        }
      }
        // Now we have ArrayList with the required size.
        // Here you create a new adapter and pass the new ArrayList to it and set the ListView adapter as the new adapter.
    }
});

Copy ArrayList Method:

    private ArrayList<Data> copyArray(ArrayList<Data> array) {
    ArrayList<Data> newArray = new ArrayList<>();
    newArray.addAll(array);
    return newArray;
}
Ameer Taweel
  • 949
  • 1
  • 11
  • 37
  • OK Sir, but based on entered value of the Edit Text i need to increment the List View Item Know How can i achieve that? – Anusha B Dec 19 '17 at 09:17
  • By adding your code app getting crash when click of the EditText – Anusha B Dec 19 '17 at 09:23
  • Thank You Sir, Now i can able to get the EditText value. – Anusha B Dec 19 '17 at 09:35
  • I've changed the answer.. check it. – Ameer Taweel Dec 19 '17 at 09:44
  • Data and originalDataArrayList both are user defined right Sir?? – Anusha B Dec 19 '17 at 09:48
  • Exactly, because I don't know what type of data you are using I wrote Data instead. Now you have to change it to any type you want. – Ameer Taweel Dec 19 '17 at 09:50
  • Data it's a model Class or i can add String. I wrote Dynamic Code to implement it's working Good But the thing is.. After change of the Edit Text value the list should remove and re-entered value count of List Should display. – Anusha B Dec 19 '17 at 09:56
  • In the `afterTextChanged` method you need to create a new adapter, pass the """ NEW """ arraylist to it, and call `listView.setAdapter(newAdapter);` – Ameer Taweel Dec 19 '17 at 09:59
  • Ok Sir, Will Share any link to creating the List View with the adapter class. – Anusha B Dec 19 '17 at 10:05
  • Those are simple examples for creating a listView with an adapter: [Example1](https://javatutorial.net/android-listview-with-listadapter-example) [Example2](https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view) [Example3](http://windrealm.org/tutorials/android/android-listview.php) – Ameer Taweel Dec 19 '17 at 10:08
  • Sir,, If the Edit Text value is removed and gives another value then how can remove the view. – Anusha B Dec 19 '17 at 10:39
  • You mean if the user entered invalid number? – Ameer Taweel Dec 19 '17 at 10:40
  • Yes Sir actually user wants to enter 2 but if he entered as 1 means Dynamically the Linear Layout is implementing,,, But again when user remove and adds value as 2 it's considering both the value means previous and present and implementing Dynamic linear for previos and present value. – Anusha B Dec 19 '17 at 10:45
  • Sir Like this I implemented list LinearLayout linearLayout = new LinearLayout(getActivity()); LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); linearLayout.setLayoutParams(params); linearLayout.setOrientation(LinearLayout.VERTICAL); TextView tv_BorderChildDetails = new TextView(getActivity()); – Anusha B Dec 19 '17 at 10:46
  • So what you want, is when the user enter 1 and then 2, you want the layout to display 3 not 2 right? – Ameer Taweel Dec 19 '17 at 10:47
  • No Sir But the thing is previous given value of the edit Text is removed means that dynamically added Linear layout Should remove. – Anusha B Dec 19 '17 at 10:54
  • So you need to create a global `int` with initial value of 0, then whenever the user enters a new value, add it to the global value you create it. – Ameer Taweel Dec 19 '17 at 10:55
  • Ok Sir. But how to remove that Dynamically implemented Linear Layout View. – Anusha B Dec 19 '17 at 10:57
  • Use this, `yourLinearLayout.setVisibility(View.GONE);` – Ameer Taweel Dec 19 '17 at 10:58
  • No sir My question is Hoe to remove dynamically implemented Linear layout when Edit Text value is removed on second click of it(Edit Text) – Anusha B Dec 19 '17 at 11:00
  • Sorry, I don't understand. – Ameer Taweel Dec 19 '17 at 11:35
  • It's OK Sir. Actually the bug is for the first time when user gives value to Edit Text value that time depends on that value Linear Layout is creating. Again when user gives value to Edit Text that time the previously created Linear Layout should remove and depends on currently added value the Linear Layout to implement. – Anusha B Dec 19 '17 at 11:56
  • Sir your given logic is correct but please need more information please help me for this. – Anusha B Dec 20 '17 at 06:46
0

You can use addTextChangedListener which is the property of EditText. Here is the code.

YourEditText.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        int count = Integer.parseInt(YourEditText.getText().toString());
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start,
    int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start,
    int before, int count) {

});

Change your edit text input type to number or you will face an error if somebody enters a text.

Listview listview= (ListView) findViewById(R.id.your_list_view);
Adapter adapter = new adapter (YourActivity.this, count);
listview.setAdapter(adapter );

Just enter your listview count in the count section and here is the adapter class

 public class Adapter extends BaseAdapter {

    // Declare Variables \\
    Context mContext;
    LayoutInflater inflater;
    int count;

    public Adapter (Context context, String count) {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
        this.count = Integer.parseInt(count);
        //---------------------------\\
    }

    public class ViewHolder {

    }

    @Override
    public int getCount() {
        return count; // Your Desired Count which you entered in EditText \\
    }

    @Override
    public Object getItem(int position) {
        return null; 
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.lv_items, null);
            yourTv = view.findViewById(R.id.lv_item_tv);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        return view;
    }}

Whenever you will change your listview count you have to initialize your adapter and listview. Hope it helps!

Rahul Singh Chandrabhan
  • 2,531
  • 5
  • 22
  • 33
  • Thank You Sir, it will help for me to implement the ListView But Sir i need to get the count value from the EditText and pass that count. Based on that count List View Item will increment – Anusha B Dec 19 '17 at 09:28
  • will please share that previous code how to implement the List View using count value – Anusha B Dec 19 '17 at 09:51
  • You can check now, if it is helpful please up-vote it – Rahul Singh Chandrabhan Dec 20 '17 at 03:56
  • Sir have one issue in View Holder class i need to get id for each of the View's in the layout right? and how it will implement based on the count and public Object getItem(int position) { return temp[position]; } means what? i didn't get that. – Anusha B Dec 20 '17 at 06:13
  • 1
    Well I have edited the code, what I assume you will take the ListView count from the user and also pass the arrayList containing data? Am I right. – Rahul Singh Chandrabhan Dec 20 '17 at 06:29
  • No Sir. My requirement is like this. I have Edit Text when user give value based on that value list should implement. For Example If user gives "1" then only one list should impalement. In list child 1(Text View) Child Name, Child School and Date-Of-Birth like that Edit Text Should list. This i did using dynamically implementing linear Layout But the Bug is when i click on edit text the Linear Layout is creating the child list. – Anusha B Dec 20 '17 at 06:42
  • 1
    Check the edited code, no need to dynamically add anything, just pass the count and it will display that number of listview items. – Rahul Singh Chandrabhan Dec 20 '17 at 09:00
  • But Sir I'm not using any Array List, Just i want One Text View and 3 Edit text. In that List. Is It possible. Im using that in Fragment So it will take context. – Anusha B Dec 20 '17 at 09:11
  • Check the edited code, just pass the editText value as count while initializing adapter. Yes context can be used and please edit your code so that I can see. – Rahul Singh Chandrabhan Dec 20 '17 at 09:46
  • Sir actually the Edit Text data for the list is adding in in List and also they used Model class. to store the Linear Data. So i got one logic that have to remove that Array List by checking the length of it.How can do that. – Anusha B Dec 20 '17 at 09:57
  • When text is removed from the Edit Text it's again implementing the List so when and how do i check it and remove the list. – Anusha B Dec 20 '17 at 09:59