手把手教你写软件6.listview

在开发中,肯定会遇到一个界面显示不完我们想要显示的东西,android就给我提供了listview来显示界面,一旦内容超过了界面,就会自动隐藏,但是我们可以通过拖动来显示。
在我的项目中,我用listview来保存奖励的内容。内容肯定是数组,保存数组这里我们采用本地保存的另一种方法,FileOutputStream以及FileInputStream。

在开始之前,我先引入一个新内容--进度条。关于进度条,我采用的是最简单的线性进度条:
如图,首先设置五个viewButton,下面对应五个textView,这些就不必多说了吧。

在xml中添加进度条:

   <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageButton"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/imageButton"
        android:layout_marginTop="94dp"
        android:max="25"
        android:progress="0" />

 progressBar.incrementProgressBy(score);//添加值到进度条

每次点击图片,就会随机生成奖励,至于这些奖励是什么,完全由你自己决定。先列出你的奖励,然后跑随机数,如果你想设置一个比例,就跑1~100的随机数,0~30就表示几率是30%,以此类推。
所以,listview的作用就体现出来了。每次当你生成了你的奖励,你都应该把你的奖励保存起来,然后当你完成你应该可以将它删除。好了,知道了思路,那么实现就只是时间问题了。

具体实现:每次点击图片后,就应该将产生的随机奖励在下面的listview中显示出来,并且还要把奖励保存起来。对于删除,我们可以设置个点击事件,这里我们换个玩法,设置为长按事件,长按提示是否删除。
好了,是时候上代码了,相信你一看就明白。
 listView = (ListView) findViewById(R.id.LV);

        read();
        arrayAdapter= new ArrayAdapter<>(RewardActivity.this, android.R.layout.simple_list_item_1, arrayList1);//将arrayList1中的内容传给arrayAdapter
        listView.setAdapter(arrayAdapter);//将内容显示出来

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {   //长按item显示是否删除
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
                AlertDialog.Builder builder = new AlertDialog.Builder(RewardActivity.this);
                builder.setMessage("是否删除该项?");
                builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int position) {
                        arrayList1.remove(i);      
                        arrayAdapter.notifyDataSetChanged();             //实时更新界面
                        write();                   //调用读写方法,将更新后的arraylist1替换原来的内容
                    }
                });
                builder.setPositiveButton("取消", null);
                builder.create().show();
                return true;
            }
        });}
        public void write(){
        try {
            FileOutputStream file = openFileOutput("reward", MODE_PRIVATE);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(file));
            for (int i = 0; i < arrayList1.size(); i++) {
               bw.write(arrayList1.get(i));                 //将内容保存到reward
                bw.newLine();                              //换行保存
            }
            bw.close(); 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void read() {

        BufferedReader br = null;
        try {
            FileInputStream fil = openFileInput("reward");
            br = new BufferedReader(new InputStreamReader(fil));
            String line;
            while ((line = br.readLine() )!= null) arrayList1.add(line);     //readLine()表示读取每一行,必须放在while里面
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null){
                    try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
    }

然后再viewButton中,添加代码:

if(!TextUtils.isEmpty(res)){                         //如果添加的内容不为空
                        try{
                            FileOutputStream fos=openFileOutput("reward",MODE_APPEND); //MODE_APPEND表示如果reward已存在,就在后面继续添加内容
                            BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fos));
                            bw.append(res);
                            bw.newLine();        //表示每次添加的内容都在下一行
                            bw.close();
                            arrayList1.add(res);
                            arrayAdapter.notifyDataSetChanged();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
好了,关于listview的有关知识就说完了,当然就这点只是远远不够,有关更多的操作在后续的项目中如果遇到会再深入的探索

猜你喜欢

转载自blog.csdn.net/qq_37820491/article/details/80160159