0

I want after a button onClick generate1() show all items from my Arraylist(.txt) contains Edittext value.

I am a beginner in android java programming.

This is my onClick Method generate1():

    public void generate1(View view) throws IOException {

        ArrayAdapter<String> adapter;
        ArrayList<String> ls = new ArrayList<>();
        EditText editText = (EditText) findViewById(R.id.editText);

        BufferedReader br = null;

        br = new BufferedReader(new InputStreamReader(getAssets().open("themen.txt")));
        String word;
        while ((word = br.readLine()) != null) {
            ls.add(word);
        }
        br.close();
        ListView listView = (ListView) findViewById(R.id.listView);
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,ls);
        listView.setAdapter(adapter);

        for (String e : ls){
           if (e.toLowerCase().contains(editText.getText().toString())){
              //???? some toDO ???? (if i do here System.out.println(e); its work, but i want the Items to a listview!! 
           }
        }
    }
Manoj Kumar Dhakad
  • 1,862
  • 1
  • 12
  • 26
user8989
  • 3
  • 2
  • when iterating for-loop, add items to your array list `ls`, after that you need to refresh your adapter with new values – pavle Feb 15 '20 at 11:55

3 Answers3

0

you should create two global ArrayList .first you named 'ls' for all results.second i call 'lsFiltered' for filtered results; then...

change this

for (String e : ls){
       if (e.toLowerCase().contains(editText.getText().toString())){
          //???? some toDO ???? (if i do here System.out.println(e); its work, but i want the Items to a listview!! 
       }
    }

to this

for (int i = 0; i <ls.size() ; i++) {
        if (e.toLowerCase().contains(editText.getText().toString())){
            lsFiltered.add(ls.get(i));
        }
}
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,lsFiltered);
listView.setAdapter(adapter);
Salar Arabpour
  • 435
  • 2
  • 9
0

First create an adapter class like this:

public class DemoAdapter extends BaseAdapter {
    private ArrayList<String> data;
    private Context context;
    private LayoutInflater inflater;

    public DemoAdapter(ArrayList<String> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        MyViewHolder myViewHolder;

        if (view==null){
            view = inflater.inflate(android.R.layout.simple_list_item_1,viewGroup,false);
            myViewHolder = new MyViewHolder(view);
            view.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) view.getTag();
        }

        myViewHolder.myTv.setText(data.get(i));

        return view;
    }


    class MyViewHolder{
        TextView myTv;

        public MyViewHolder(View item){
            myTv = item.findViewById(android.R.id.text1);
        }
    }
}

Then in your generate1 method iniialize it like this:

DemoAdapter adapter = DemoAdapter(this,ls);
listView.setAdapter(adapter);
  • in the last line of the DemoAdapter class : item.findViewById(android.R.id.editText) i get can not resolve symbol 'editText'....i change bevor EditText mytv; – user8989 Feb 15 '20 at 12:10
  • as you are using simple_list_item_1, you have to use text1 as this is its item, you can also make your own layout and then access your widget accordingly. – Soumik Bhattacharjee Feb 15 '20 at 12:18
  • I tried but unfortunately without success. Thks for triying to help me Soumik – user8989 Feb 15 '20 at 12:31
  • Never give up :) ... can u look at my new method, its worked now but i think i forgot something. thks – user8989 Feb 15 '20 at 13:37
  • Really appreciate your try, the value is showing 10 times because everytime after setting one value it is not going out of the while loop, it is incrementing its value by 1 and then again setting same value in the next position until its full size. So, you just have to do this: `for (int j=0; j – Soumik Bhattacharjee Feb 15 '20 at 16:26
  • Thks a lot, its already worked. i have delete the while() and let just the for each(). – user8989 Feb 15 '20 at 16:29
0

public void generate1(View view) throws IOException {

    ArrayAdapter<String> adapter2;
    ArrayList<String> ls = new ArrayList<>();
    ArrayList<String> ls2 = new ArrayList<>();

    EditText editText = (EditText) findViewById(R.id.editText);

    BufferedReader br = null;

        br = new BufferedReader(new InputStreamReader(getAssets().open("themen.txt")));
        String word;
        while ((word = br.readLine()) != null) {
            ls.add(word);
        }
        br.close();
         ListView listView = (ListView) findViewById(R.id.listView);

    for (String e : ls) {

           int i = 0;
            while (i<ls.size()){

               if (e.toLowerCase().contains(editText.getText().toString())) {

                    ls2.add(i,e);
                }
                i++;
            }
        }

        adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ls2);
        listView.setAdapter(adapter2);
    }

}

i found another way and its worked :D, but the result in the listview is everytime viewed 10 times!!?? I forgot something??

user8989
  • 3
  • 2