-1

I am new to Android programming. I intend to make a shopping List using a ListView such that the user can strike through items on the list that he has bought. it should be such that

  1. List is made.

  2. He can strike through items bought.

  3. He can add items using the EditText and the Add button at the bottom of the layout.

  4. List is rendered after adding items like on point 3 with the stroke through items maintained.

On starting I click for many of the initial items I added in my program to have a stroke through and then I start adding items, I get an unexpected behaviour in the List. Some of the newly added items get a stroke through after I click ADD right from the outset when I havent even clicked for them to have a stroke through. Some of them dont. I dont understand this behaviour. Is it because of some property of ListView?

 ArrayList<String> shoppingList = new ArrayList<>();;
    ArrayList<String> strikeThroughList = null;
    ArrayList<String> strikeThroughText = null;
    ArrayAdapter<String> adapter = null;
    ListView lv = null;
    Button addBtn=null;
    EditText addEdtTxt=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addBtn=findViewById(R.id.addBtn);
        addBtn.setOnClickListener(this::btnClick);


        //shoppingList = new ArrayList<>();
        strikeThroughList = new ArrayList<>();
        strikeThroughText= new ArrayList<>();


        //One way to populate List
        Collections.addAll(shoppingList, "Eggs", "Yogurt", "Milk", "Bananas", "Apples", "Tide with bleach", "Cascade");
        adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, shoppingList);
        lv = (ListView) findViewById(R.id.listView);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

         getMenuInflater().inflate(R.menu.mnu,menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id=item.getItemId();

        if(id==R.id.clrList)
                 {

                    shoppingList.clear();
                    lv.setAdapter(adapter);

                    }


        return super.onOptionsItemSelected(item);
    }

    public void btnClick(View view) {


                      if(view.getId()==R.id.addBtn){ addEdtTxt=findViewById(R.id.addEdtTxt);

                            adapter.add(addEdtTxt.getText().toString());
                              adapter.notifyDataSetChanged();

                       }

                       }

    @Override
   public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

        TextView text = (TextView) view;
        text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);


        }
MarionaGlenni
  • 63
  • 1
  • 7
  • Whats the reason of this `lv.getChildAt(3)`? Why are you using this ? What you need is a [Custom Adapter](https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view). – ADM Feb 22 '19 at 11:48
  • I want to get the children views in the ListView and compare with the views that have been stroke through and saved in strokeThroughViews. If the views correspond, then I strike it again. I did this because I discovered that each time I click ADD after adding an item and the method btnClick(View view) gets called, the initial strokes go away – MarionaGlenni Feb 22 '19 at 11:52
  • Read https://stackoverflow.com/questions/6766625/listview-getchildat-returning-null-for-visible-children. You should not use `getChildAt` in a `AdapterView` if you know the working of `AdapterView`. – ADM Feb 22 '19 at 11:56
  • But when you added new View to strikeThroughView , you have not added new view its references of some other-view. And How you can check id of view because you are set id to added view . so id must be assigned its self. – Chetan Joshi Feb 22 '19 at 12:06
  • thanks ADM and Chetan. I think Chetans code given below is an apt solution since it sticks the stroke to the view and it gives it an id to compare later in the code – MarionaGlenni Feb 22 '19 at 12:34
  • Hello ADM, I edited my question. Please can you take a look – MarionaGlenni Feb 22 '19 at 15:21

1 Answers1

0

Try to with below code change while adding Views to strikeThroughView list.

@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//String selectedFromList = (String)(lv.getItemAtPosition(position));
TextView text = (TextView) view;
text.setPaintFlags(text.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
text.setId(2);
strikeThroughView.add(text);
}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43