将所有项目从包含文本视图值的文件.txt(ArrayList)设置为列表视图[Android Java]

问题描述 投票:0回答:3

我想在按钮onClick generate1()之后显示我的Arraylist(.txt中的所有项目都包含Edittext值。

我是android java编程的初学者。

这是我的onClick方法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!! 
           }
        }
    }
java android android-studio android-listview
3个回答
0
投票

您应该创建两个全局ArrayList。首先为所有结果命名为'ls'。其次我为过滤的结果调用'lsFiltered';然后...

更改此

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

至此

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

0
投票

首先创建这样的适配器类:

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

然后在您的generate1方法中将其初始化如下:

DemoAdapter adapter = DemoAdapter(this,ls);
listView.setAdapter(adapter);

0
投票

public void generate1(View view)抛出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);
    }

}

我发现了另一种方法,并且它起作用了:D,但是listview中的结果每次被查看10次!我忘记了什么?

© www.soinside.com 2019 - 2024. All rights reserved.