如何在列表视图一次只能修改一个行? [英] How to modify only one row at a time in Listview?

查看:90
本文介绍了如何在列表视图一次只能修改一个行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出以下所列问题的解决方案。我有一个列表视图生成使用 Simpleadapter 。当我在ListView点击某行我想打一个布局带有ID 多彩为可见。我能做到这一点。但我的问题从这里开始。当我点击另一行说行5丰富多彩的布局数量是可见的,但布局也为previously点击排太明显。我想要做的是使布局丰富多彩可见的只有点击行(即它应该只有一排可以看到在一个时间,即目前点击行和隐藏所有剩余行)和布局应该得到无形previously点击行。我试着用做viewholder ,但它并不能帮助。我的code片断如下。引导我一步一步,因为我很新到Android。

 最后BaseAdapter K =新SimpleAdapter(getActivity(),缬氨酸,R.layout.mytaskdata,新的String [] {SNAME,标题,说明,ID ,路径,接收器,发件人},新的INT [] {R.id.textView1,R.id.textView2,R.id.textView3,R.id.hide1,R.id.hide2, R.id.hide3,R.id.hide4})
      {        @覆盖
        公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup){
            // TODO自动生成方法存根
            最后的视图V = super.getView(位置,convertView,父母);
            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的编辑=(ImageView的)v.findViewById(R.id.ImageView03);
            sent.setOnItemClickListener(新OnItemClickListener(){                @覆盖
                公共无效onItemClick(适配器视图<>为arg0,ARG1查看,
                        INT ARG2,长ARG3){
                    // TODO自动生成方法存根
                    RelativeLayout的R =(RelativeLayout的)arg1.findViewById(R.id.colorful);
                // r.setVisibility(arg1.VISIBLE);
                    INT TEMP = sent.getCheckedItemPosition();
                    Log.i(itemposition,+温度);                    Toast.makeText(getActivity(),POS+ ARG2 +HII+ positionThatSaysHi,1000).show();
                    如果(ARG2!= positionThatSaysHi)
                    {
                        r.setVisibility(arg1.VISIBLE);
                        positionThatSaysHi = ARG2;
                        notifyDataSetChanged();
                    }
                    其他
                    {
                        r.setVisibility(arg1.GONE);
                         notifyDataSetChanged();
                    }
            });


解决方案

我会建议修改 OnClickListener 来只是记录选定行,然后调用 notifyDataSetChanged()。这将导致在ListView通过调用适配器重绘其项目。因此,你只需要检查 getView()此值确定的多彩的观点是否应为可见或不可见。

所以更新code会是这样的:

  @覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
{
    最后的视图V = super.getView(位置,convertView,父母);
    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的编辑=(ImageView的)v.findViewById(R.id.ImageView03);    RelativeLayout的R =(RelativeLayout的)v.findViewById(R.id.colorful)
    r.setVisibility(?位置== positionThatSaysHi View.VISIBLE:View.GONE);    sent.setOnItemClickListener(新OnItemClickListener()
    {
        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3)
        {
            positionThatSaysHi = ARG2;
            notifyDataSetChanged();
        }
    });
}

这样,可以确保只有一个视图将突出显示(同时还简化了您的code)。

I am trying to figure out the solution for the following listed problem. I have a Listview generated using Simpleadapter. When I click on a row in the listview I want to make a Layout with an id colorful as visible. I am able to do this. But my problem starts here. When I click on another row say row number 5 the colorful layout is visible, but the layout is also visible for the previously clicked row too. What I want to do is make the layout colorful visible for only the clicked row (i.e it should be visible for only one row at a time i.e is currently clicked row and hidden for all the remaining rows) and the layout should get invisible for the previously clicked rows. I tried doing with viewholder but it doesn't help. My code snippet is below. Guide me step by step as I am very new to 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();
                    }
            });

解决方案

I would suggest modifying the OnClickListener to just record the selected row, then call notifyDataSetChanged(). This will cause the ListView to redraw its items by calling the adapter. Therefore, you just need to check this value in getView() to determine whether the "colorful" view should be visible or not.

So the updated code would be something like:

@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();
        }
    });
}

That way, you ensure that only one view will be highlighted (while also simplifying your code).

这篇关于如何在列表视图一次只能修改一个行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆