如何在Listview中一次仅修改一行?

我正在尝试找出以下所列问题的解决方案.我有一个使用Simpleadapter生成的Listview.当我单击列表视图中的一行时,我想创建一个ID为可见的彩色版式.我能够做到这一点.但是我的问题从这里开始.当我单击另一行时,说第5行,则可以看到彩色布局,但是对于先前单击的行,该布局也可见.我想做的是使布局仅对单击的行可见(即,一次只能对一行显示,即当前单击的行对所有其余行都隐藏),并且布局对于先前点击的行.我尝试使用Viewholder,但这没有帮助.我的代码段如下.逐步引导我,因为我是Android的新手.

           final BaseAdapter k=new SimpleAdapter(getActivity(),val,R.layout.mytaskdata,new String[]{"sname","heading","desc","id","path","receiver","sender"},new int[]{R.id.textView1,R.id.textView2,R.id.textView3,R.id.hide1,R.id.hide2,R.id.hide3,R.id.hide4})
      {

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            final View v = super.getView(position, convertView, parent);
            TextView myname=(TextView)v.findViewById(R.id.textView1);
            TextView mydes=(TextView)v.findViewById(R.id.textView2);
            TextView mytopic=(TextView)v.findViewById(R.id.textView3);
            ImageView edit=(ImageView)v.findViewById(R.id.ImageView03);
            sent.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    RelativeLayout r=(RelativeLayout)arg1.findViewById(R.id.colorful);
                //  r.setVisibility(arg1.VISIBLE);
                    int temp=sent.getCheckedItemPosition();
                    Log.i("itemposition",""+temp);

                    Toast.makeText(getActivity(),"pos"+arg2+"hii"+positionThatSaysHi,1000).show();
                    if(arg2!=positionThatSaysHi)
                    {
                        r.setVisibility(arg1.VISIBLE);
                        positionThatSaysHi = arg2;
                        notifyDataSetChanged();
                    }
                    else
                    {
                        r.setVisibility(arg1.GONE);
                         notifyDataSetChanged();
                    }
            });
最佳答案
我建议将OnClickListener修改为仅记录所选行,然后调用notifyDataSetChanged().这将导致ListView通过调用适配器重绘其项目.因此,您只需要在getView()中检查此值即可确定“彩色”视图是否可见.

因此,更新后的代码将类似于:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
    final View v = super.getView(position, convertView, parent);
    TextView myname=(TextView)v.findViewById(R.id.textView1);
    TextView mydes=(TextView)v.findViewById(R.id.textView2);
    TextView mytopic=(TextView)v.findViewById(R.id.textView3);
    ImageView edit=(ImageView)v.findViewById(R.id.ImageView03);

    RelativeLayout r = (RelativeLayout)v.findViewById(R.id.colorful)            
    r.setVisibility(position == positionThatSaysHi ? View.VISIBLE : View.GONE);

    sent.setOnItemClickListener(new OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        {
            positionThatSaysHi = arg2;
            notifyDataSetChanged();
        }
    });
}

这样,您可以确保仅突出显示一个视图(同时还可以简化代码).

转载注明原文:如何在Listview中一次仅修改一行? - 代码日志