在自定义列表arrayadapter中附加数据

时间:2015-08-28 10:58:38

标签: android android-arrayadapter

我有一个带有图像arrayadapter的自定义列表。我需要将一些新数据添加到该列表arrayadapter中。我试图通过

添加数据
your_array_list.add("foo");
your_array_list.add("bar");

但这不起作用帮助我将数据添加到列表视图

这是我的适配器类

public ApplicationAdapter(Context context, int textViewResourceId,
            List<ApplicationInfo> appsList) {
        super(context, textViewResourceId, appsList);
        this.context = context;
        this.appsList = appsList;
        packageManager = context.getPackageManager();
    }

    @Override
    public int getCount() {
        return ((null != appsList) ? appsList.size() : 0);
    }

    @Override
    public ApplicationInfo getItem(int position) {
        return ((null != appsList) ? appsList.get(position) : null);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (null == view) {
            LayoutInflater layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutInflater.inflate(R.layout.snippet_list_row, null);
        }



        ApplicationInfo data = appsList.get(position);
        if (null != data) {
            TextView appName = (TextView) view.findViewById(R.id.app_name);
            TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
            ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);

            appName.setText(data.loadLabel(packageManager));
            packageName.setText(data.packageName);
            iconview.setImageDrawable(data.loadIcon(packageManager));
        }
        return view;
    }

这是我的活动代码

applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
        ;
        ApplicationAdapter listadaptor = new ApplicationAdapter(AllAppsActivity.this,
                    R.layout.snippet_list_row, applist);

1 个答案:

答案 0 :(得分:1)

如果是第一次初始化:

applist.add("foo");
ApplicationAdapter listadaptor = new ApplicationAdapter(AllAppsActivity.this,R.layout.snippet_list_row, applist);
如果您愿意添加数据,请稍后

applist.add("foo");
listadaptor.notifyDataSetChanged();

从您的代码中我看到,作为适配器的参数,您将获得一个对象列表(List<ApplicationInfo>),因此您需要添加applist.add("foo")而不是添加字符串ApplicationInfo。对象类似于:

ArrayList<ApplicationInfo> yourList = new ArryList<>();
yourList.add(applist);