2011-06-25 67 views
0

我已經創建了一個示例,根據這個link ListView幾行的不同佈局。這裏是佈局無法更新列表視圖中的不同行我

Here is my layout

,因爲我想只有第一行有不同的佈局,每當位置爲0我申請佈局一個(名稱&電話),並休息我申請佈局兩(地址)。

我面對的問題是當我在適配器上觸發notifyDataSetChanged時,只有我的地址被更改,但名稱&電話行,即第一行不更新。當選擇子菜單項時,我正在觸發notifyDataSetChanged。

這是代碼。

public static final int TYPE_ADDRESS = 1; 
public static final int TYPE_NAME = 0; 
public static final int TYPE_MAX_COUNT =2; 

@Override 
public int getItemViewType(int position) { 
    if(position == 0) 
     return TYPE_NAME; 
    else 
     return TYPE_ADDRESS; 
} 
@Override 
public int getViewTypeCount() { 
    return TYPE_MAX_COUNT; 
} 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 


    ViewHolder holder=null; 
    int type = getItemViewType(position); 
    System.out.println("getView " + position + " " + convertView + " type = " + type); 




if (convertView == null) { 
    holder = new ViewHolder(); 


    switch (type) { 
     case TYPE_ADDRESS: 
        convertView = mInflater.inflate(R.layout.layout_address, null); 
        holder.address = (TextView) convertView.findViewById(R.id.address_id);     
        holder.address.setText(data.getAddress(position)); 


        break; 

     case TYPE_NAME:       
        convertView = mInflater.inflate(R.layout.layout_name, null);      
        holder.name = (TextView)convertView.findViewById(R.id.name_id); 
        holder.phone = (TextView)convertView.findViewById(R.id.phone_id); 



        holder.name.setText(data.getName(position)); 
        holder.phone.setText(data.getPhone(position)); 




        break; 
    } 

     convertView.setTag(holder);   

    }else { 

    holder = (ViewHolder) convertView.getTag(); 
    }  



    return convertView; 
} 



static class ViewHolder { 
     TextView address; 
     TextView name; 
     TextView phone; 

    } 

回答

0

據我所知,你需要管理的意見(比如設置文本)外if (convertView == null){}else{}塊,因爲你需要它在任何情況下被調用,不僅在視圖中創建。這可能是問題嗎?

if (convertView == null) { 
    holder = new ViewHolder(); 


    switch (type) { 
     case TYPE_ADDRESS: 
        convertView = mInflater.inflate(R.layout.layout_address, null); 
        holder.address = (TextView) convertView.findViewById(R.id.address_id);          

        break; 

     case TYPE_NAME:       
        convertView = mInflater.inflate(R.layout.layout_name, null);      
        holder.name = (TextView)convertView.findViewById(R.id.name_id); 
        holder.phone = (TextView)convertView.findViewById(R.id.phone_id); 

        break; 
    } 

     convertView.setTag(holder);   

    }else { 

    holder = (ViewHolder) convertView.getTag(); 
    }  

    switch (type) { 
     case TYPE_ADDRESS: 
       holder.address.setText(data.getAddress(position)); 
       break; 
     case TYPE_NAME:       
       holder.name.setText(data.getName(position)); 
       holder.phone.setText(data.getPhone(position)); 
       break;  
+0

bravo !!這是我想要的感謝 – Hunt