从内部存储获取字符串数据当我将其设置为listview时,仅显示最后一项

时间:2016-11-08 07:29:00

标签: android listview

从内部存储获取字符串数据。当我尝试在列表视图上显示该数据时,仅显示最后一项。 这是我的代码。

 listView=(ListView)findViewById(R.id.listview);


    Scanner s = null;
    try {
        s = new Scanner(new BufferedReader(new FileReader("/storage/sdcard0/client_new.txt")));
        while (s.hasNext()) {
            String str = s.next();
            char[] myChar = str.toCharArray();
            System.out.println(myChar);
            String str_new = String.valueOf(myChar);
            String[] items={str_new};
            final ArrayList<String> list = new ArrayList<String>();
            for (int i = 0; i < items.length; ++i)
            {
                list.add(items[i]);
            }

            Toast.makeText(ExistingContacts.this,str_new,Toast.LENGTH_LONG).show();
            ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,list);
            listView.setAdapter(adapter);


        }
    }catch (Exception e)
    {
        e.printStackTrace();;
    }

1 个答案:

答案 0 :(得分:0)

使用final ArrayList list = new ArrayList();在while循环之前

public class Shape
{
    public Color color { get; set; }
    public Point origin { get; set; }
}

public class MyRectangle : Shape
{
    public Size size { get; set; }

    public Rectangle ToRectangle()
    {
        return new Rectangle(origin, size);
    }
}

public partial class Form1 : Form
{
    Point mouseDownPoint;

    public Form1()
    {
        InitializeComponent();
    }

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        mouseDownPoint = e.Location;
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        MyRectangle rect = new MyRectangle();
        rect.origin = mouseDownPoint;
        rect.size = Size.Subtract((Size)e.Location, (Size)mouseDownPoint);

        pictureBox1.CreateGraphics().DrawRectangle(new Pen(Brushes.Black), rect.ToRectangle());
    }
}