从ArrayList填充Listview,然后由.txt文件填充

时间:2015-03-14 16:32:41

标签: android listview android-asynctask

我正在研究这个Android应用程序,它首先从互联网上下载.txt文件然后填充arraylist和最终listview。问题是Listview似乎没有填充,尽管我的文件下载和阅读是一切都好,因为我用TOAST测试了它。

这是我的代码

使用AsyncTask下载文件和阅读

    public class DownloadFile extends AsyncTask<String, Void, String> {
        String filenamelist=TEST_FILE_NAME;
        @Override
        protected String doInBackground(String... params) {


           //Here i download the file

            } catch (Exception e) {
                e.printStackTrace();
            }
return "Executed";
        }

        @Override
        protected void onPostExecute(String result) {

            try {
                InputStream instream = new FileInputStream(new File(getCacheDir(), filenamelist));
                InputStreamReader inputreader = new InputStreamReader(instream);
                BufferedReader bReader = new BufferedReader(inputreader);
                String key,value = null;

                do{
                    key = bReader.readLine();
                    if(key != null)
                        value = bReader.readLine();
//channelname is the ArrayList                    
channelname.add(key);
                    channelmaps.put(key,value);




                } while (key != null && value != null);




            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
    }

ArrayList声明

 ArrayList<String> channelname = new ArrayList<String>();

列表视图

 final ListView listView = (ListView) findViewById(android.R.id.list);
    channelname.removeAll(Collections.singleton(null));

    ChannelAdapter cAdapter = new ChannelAdapter(this, channelname);
    listView.setAdapter(cAdapter);

ChannelAdapter类

private class ChannelAdapter extends ArrayAdapter<String>
    {
        ArrayList<String> channelname;
        public ChannelAdapter(Context context, ArrayList<String> channelname)
        {
            super  (context, R.layout.activity_list_channels, channelname);
            this.channelname = channelname;
        }

        public View getView(int position, View convertView, ViewGroup parent)
        {
            try
            {
                if(convertView==null)
                {
                    LayoutInflater inflater = getLayoutInflater();
                    convertView = inflater.inflate(R.layout.mylistchannel,parent, false);
                }
                String  name = getItem(position);
                TextView txtview = (TextView) convertView.findViewById(R.id.Channelname);
                txtview.setText(name);
                return convertView;
            }
            catch(Exception e)
            {
            }
            return null;
        }
        @Override
        public int getCount()
        {
            return channelname.size();
        }

        @Override
        public String getItem(int position)
        {
            return channelname.get(position);
        }

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

那么我的代码有什么问题?为什么列表视图没有填充?

1 个答案:

答案 0 :(得分:0)

你应该做

cAdapter.notifyDataSetChanged();

在您阅读文件中的所有项目的周期之后