更新Android listview

时间:2015-08-26 12:29:18

标签: android

我正在从SQlite开发ListView内部的Horizo​​ntalListView应用程序。当我向下滚动ListView时,我的horizo​​ntallistview将以双重输入的方式增加。我尝试使用 adapter.notifyDataSetChanged(); ,但它对horizo​​ntalListView没有影响。关于我应该怎么做的想法。

List<All_Post> allDesc = dbhelper.getAllDescriptions();
            for (All_Post all_Post : allDesc)
            {
                descArray.add(all_Post);
            }

            final MyListAdapter adapter = new MyListAdapter(this, R.layout.all_post_row, descArray);
            listView.setAdapter(adapter);
            adapter.notifyDataSetChanged();

 listView.setOnDetectScrollListener(new OnDetectScrollListener() {
            @Override
            public void onUpScrolling() {
        /* do something */

                adapter.notifyDataSetChanged();
                Log.e("onUpScrolling ", "!!!!!!");
            }

            @Override
            public void onDownScrolling() {
        /* do something */
                adapter.notifyDataSetChanged();
                Log.e("onDownScrolling ", "!!!!!!");
            }
        });

这是我的带有Horizo​​ntalListView的适配器getView()方法

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        Holder holder = null;
        if (row == null) 
        {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            row = vi.inflate(R.layout.all_post_row, null);
            holder = new Holder();
            holder.horizontalScrollView = (HorizontalScrollView)row.findViewById(R.id.hlist);
            row.setTag(holder);
        }
        else
        {
            holder = (Holder) row.getTag();
        }

        dbhelper = new MyDbHelper(AllPosts_Page.this);
        SQLiteDatabase db = dbhelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from ActivityObjectList where activityId " + "= ? ", new String[]{strDescription});
        ArrayList<String> imageArray = new ArrayList<String>();
        if (cursor.moveToFirst())
        {
            do
            {
                String imagePath = cursor.getString(cursor.getColumnIndex("imageaudioPath"));
                Log.e("imagePath "," = " + imagePath);
                String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
                String path = baseDir + "/SDImages/" + imagePath;

                imageArray.add(path);
            }
            while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        int totalImagesPerActivityId = imageArray.size();
        Log.e("total", "ImagesPerActivityId is  = " + totalImagesPerActivityId);

        holder.linearLayout=(LinearLayout)row.findViewById(R.id.innerlay);
        for (int i = 0; i <= totalImagesPerActivityId ; i++)
        {
            final ImageView imageView = new ImageView (getContext());
            imageView.setTag(i);
            imageView.setImageResource(R.drawable.img_placeholder);
            holder.linearLayout.addView(imageView);
            imageView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    // TODO Auto-generated method stub
                    Log.e("Tag",""+imageView.getTag());
                }
            });
        }

        return row;
    }

    class Holder {

        HorizontalScrollView horizontalScrollView;

    }
}

如何实现Horizo​​ntalListView?感谢赞赏。

1 个答案:

答案 0 :(得分:0)

您应该在填充新的子视图之前删除所有子视图:

holder.linearLayout=(LinearLayout)row.findViewById(R.id.innerlay);
holder.linearLayout.removeAllViews();
for (int i = 0; i <= totalImagesPerActivityId ; i++)