用android中的新数据刷新ListView

时间:2013-03-22 11:23:08

标签: java android

我正在尝试构建一个函数,当在listview中单击文件夹时,它将在同一个活动中显示该文件夹中的文件列表。即用不同的数据刷新相同的列表视图。我在数据库中构建了一个类似结构的目录,并从中获取文件和文件夹列表。

public class SkydriveFolderList extends Activity
{
    ArrayList<String> filelist1=new ArrayList<String>();
    ArrayList<String> filepath=new ArrayList<String>();
    ArrayList<String> isfolder=new ArrayList<String>();
    ArrayList<Integer> fileid=new ArrayList<Integer>();

    ListView lv;
    EditText et;
    ArrayAdapter<SearchResults> arrayAdapter;
    Button back;
    TextView path;![enter image description here][1]

    public void onCreate(Bundle SkydriveFolderList)
    {
        Intent i = getIntent();
        filelist1 = i.getStringArrayListExtra("FileList");
        filepath = i.getStringArrayListExtra("FilePath");
        isfolder = i.getStringArrayListExtra("isFolder");
        fileid = i.getIntegerArrayListExtra("FileId");

        super.onCreate(SkydriveFolderList);
        ArrayList<SearchResults> searchResults = GetSearchResults();

        setContentView(R.layout.filelist);
        lv = (ListView)findViewById(R.id.skydrive_list);
        et=(EditText)findViewById(R.id.inputSearch);
        path=(TextView)findViewById(R.id.filepath);
        back=(Button)findViewById(R.id.back);

        arrayAdapter =new ArrayAdapter<SearchResults>(this,android.R.layout.simple_list_item_1,searchResults);
        // ((ListView) arrayAdapter).setAdapter();
        lv.setAdapter(new MyCustomBaseAdapter(this, searchResults)); 
        //lv.setAdapter(arrayAdapter);

    et.addTextChangedListener(new TextWatcher()
    {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3)
        {
            // When user changed the Text
            SkydriveFolderList.this.arrayAdapter.getFilter().filter(cs);
        }

        @Override
        public void afterTextChanged(Editable arg0)
        {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3)
        {
            // TODO Auto-generated method stub

        }


    });


    lv.setOnItemClickListener(new OnItemClickListener()

    {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
        {

       Object o = parent.getItemAtPosition(position);
       SearchResults fullObject = (SearchResults)o;
      // prestationEco str=(prestationEco)o;//As you are using Default String Adapter
       Toast.makeText(getBaseContext(),fullObject.getFilename(),Toast.LENGTH_SHORT).show();
        }

        });


}

http://i.stack.imgur.com/QCIsr.png

请检查以上链接以查看该应用的快照。

现在,在单击文档文件夹时,应使用该文件夹中的文件列表刷新列表视图。我该怎么办?

1 个答案:

答案 0 :(得分:1)

当用户点击刷新整个列表视图的列表视图时,您可以调用adapter.notifyDataSetChanged()这将刷新您的视图而不会出现任何延迟或崩溃....这对您来说是一个更好的方法..希望这会有所帮助。