更新Listview项目

时间:2015-04-22 15:19:31

标签: android listview android-listview

我需要在将每个项目行添加到listView后更新。我使用abc_slide_in_bottom进行动画

我想要的是,当一个项目从底部滑动后添加到ListView时,上一个项TextView(具有一些文本值)应该更改为ImageView

MainActivity:

   protected void onCreate(Bundle savedInstanceState) {
    dataObj=new Data();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myListView=(ListView)findViewById(R.id.lv_preFlight);
    goBtn=(Button)findViewById(R.id.btn_go);
    context=this.getApplicationContext();

    setData();//to add first item to the dataObj
    customAdapter=new dataListAdapter(dataObj,context,MainActivity.this);

    myListView.setAdapter(customAdapter);
   LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.abc_fade_in), 2f);
    myListView.setLayoutAnimation(lac);

}

每当将新项目添加到ListView时,我都会调用onUpdate来更新旧项目。 MainActivity中的OnUpdate函数

  public void onUpdate(int position)
{
    View v=myListView.getChildAt(position);

        if(v!=null)
        {
            TextView titleTV=(TextView)v.findViewById(R.id.tv_itemName);
            TextView statusTV=(TextView)v.findViewById(R.id.tv_itemStatus);
            ImageView imageIV=(ImageView)v.findViewById(R.id.iv_icon);
            if(statusTV.getVisibility()==View.VISIBLE)
            {
                Log.e(dataListAdapter.TAG,"pos "+position+" title: "+titleTV.getText());
                statusTV.setVisibility(View.INVISIBLE);
                imageIV.setVisibility(View.VISIBLE);
            }

        }
        dataObj.itemName.add(items[position+1]);
        dataObj.status.add("Checking...");
        dataObj.showImg.add(false);
        customAdapter=new dataListAdapter(dataObj ,context,MainActivity.this);
        customAdapter.notifyDataSetChanged();
        count++;


}

这里我添加了Listview的自定义适配器。 我的自定义适配器的getView方法:

    public View getView(final int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View row = convertView;

    if (row == null)
    {
        row = inflater.inflate(R.layout.preflight_row_layout, parent, false);
        holder = new ViewHolder();
        row.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) row.getTag();
    }
    holder.title = (TextView) row.findViewById(R.id.tv_itemName);
    holder.status = (TextView) row.findViewById(R.id.tv_itemStatus);
    holder.img=(ImageView)row.findViewById(R.id.iv_icon);
    holder.title.setText(Title.get(position));
    holder.status.setText(Status.get(position));
    holder.img.setImageResource(R.drawable.tick_image);

    if (position+1==this.Title.size())//when new item added call to onUpdate
    {

        update.onUpdate(position);//customListner's function called
    }

    return (row);
}

row_layout.xml包含2 TextView个项目标题,项目状态("检查....")和ImageView。 我正在尝试更新ListView中的先前项目,从"检查...."将新项目添加到TextViewImageViewListView。 我要求与this link匹配,但我无法更新旧项目,此外项目随机添加到listview

1 个答案:

答案 0 :(得分:0)

获取对Animation的引用并在其上设置一个监听器

Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.abc_slide_in_bottom);
anim.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation arg0) {}

        @Override
        public void onAnimationRepeat(Animation arg0) {}

        @Override
        public void onAnimationEnd(Animation arg0) {
            // here the animation has ended
           // so you call notifydatasetchanged here, so before you add
           // an item to the list update needs to be true; so after the
          // getView method it is false;
        }
    });

现在从我如何思考你现在给你一个逻辑,这是我的逻辑,当一个项目添加到ListView时,它添加到支持它的数据对象,如果它是一个ArrayList然后是列表中的最后一项,因此在您的getView中,您可以检查List中的最后一项并添加修改,此处的修改将在您的更新该特定项目的getView方法,为了消除每次更新最后一项,您可以添加boolean以检查是否已添加项目,可以放置boolean作为数据对象中的字段,以便您检查并将其切换为false,然后更改View

修改1 您的自定义数据对象

public class Data{
String title,nxtstatus,prevstatus; boolean update;
//any additional stuff can go here depends on you
}

然后您将使用ArrayListArrayList<Data> data = new ArrayList<Data>();然后为View创建另一个类public class MyViews { TextView title, status,prevStatus; ImageView prevImg;} 然后在你的getView方法

 public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = convertView;
MyViews myv; // you might be required to initialize this
Data data = getItem(position); //getItem is a super class's method
if(row == null){
row = inflater.inflate(R.layout.row_layout, parent, false);
myv = new MyViews();
myv.title = (TextView) row.findViewById(R.id.tv_itemName);
myv.status = (TextView) row.findViewById(R.id.tv_itemStatus);
myv.prevImg=(ImageView)newRow.findViewById(R.id.iv_icon);
myv.prevImg.setVisibiltiy(View.GONE);
title.setText(data.title);
status.setText(data.status);
row.setTag(myv);}else{myv = row.getTag(); //some casting will go on here    
}    
if(data.update){ // if update is true it means it was just added
   myv.prevStatus.setVisibility(View.GONE);        
   myv.prevImg.setVisibility(View.VISIBLE);
   data.update = false; // the next pass this view will not be updated
}
//then finally return the row;
return row;}

我认为这足以让你前进,转到最基本的动画代码吧?