如何在列表项中显示图像

时间:2014-07-17 06:33:37

标签: android

您好我正在使用列表视图。当我点击删除按钮时,我希望那个时间只显示删除图像最初我隐藏了deleteimage(redminus标记)。http://pasteboard.co/1yJZyUz7.png http://pasteboard.co/1yK2BsYb.png我尝试了代码

public class ViewBudget extends FragmentActivity  {
     boolean editmode=false;
    @Override
    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.viewbug);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main1);
        TextView ctitle = (TextView) findViewById(R.id.customtitlebar);

        //asyncktask
        new ImageDownload().execute("");


        lv = (ListView)findViewById(android.R.id.list);

        //  lv.setAdapter(new IconicAdapter(this));
        Button del=(Button)findViewById(R.id.button2);
        del.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub                    
                editmode=true;
            notifyDataSetChanged(); 
            }

        });


        lv.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> a, View v,int position, long id) 
            {

        });

    }



    @SuppressWarnings("rawtypes")
    class IconicAdapter extends ArrayAdapter {
        Activity context;

        @SuppressWarnings("unchecked")
        IconicAdapter(Activity context) {
            super(context, R.layout.viewbudget, str2);
            this.context = context;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = context.getLayoutInflater();
            View row = inflater.inflate(R.layout.viewbudget, null);
            TextView label = (TextView) row.findViewById(R.id.label);
            ImageView deli=(ImageView)row.findViewById(R.id.imageView2);
            TextView delll=(TextView)row.findViewById(R.id.textView10);

            if(editmode) {
             deli.setVisibility(View.VISIBLE);
        }
        else {
            deli.setVisibility(View.GONE);
        }


          }

但单击“删除”按钮后无法显示。感谢。

2 个答案:

答案 0 :(得分:3)

notifyDataSetChanged()在此处不起作用,notifyDataSetChanged()仅在更改数据大小(数组或ArrayList)时起作用。

您需要将新适配器设置为列表视图,以保持滚动位置,您可以使用以下代码 -

//collecting current scroll pos
int selectedPos = lv.getFirstVisiblePosition();
View v = lv.getChildAt(0);
int scrollOffset = (v == null) ? 0 : v.getTop();

//reset new adapter
lv.setAdapter(new IconicAdapter(this));

//restoring scroll pos
lv.setSelectionFromTop(selectedPos, scrollOffset);

答案 1 :(得分:0)

你能改变一下吗?

deli.setVisibility(View.GONE);

成:

deli.setVisibility(View.INVISIBLE);

注意:请勿在.xml文件中为该属性设置任何可见性。