2014-09-04 89 views
0

這個問題可能會被詢問很多次,但我無法解決問題。每當我滾動自定義列表視圖數據都放錯位置我放了其他條件。請讓我知道我的錯誤。ListView Data Scroll Android

公共類ImageAdapter延伸BaseAdapter {

private LayoutInflater mInflater; 

public ImageAdapter(Context c) { 
    mInflater = LayoutInflater.from(c); 

} 

@Override 
public Object getItem(int position) { 
    return null; 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder holder; 



    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.griddata, null); 
     k = position; 
     holder = new ViewHolder(); 
     holder.txttitle = (TextView) convertView 
       .findViewById(R.id.txttitle); 
     holder.txtproduct = (TextView) convertView 
       .findViewById(R.id.txtproduct); 
     holder.txtcustname = (TextView) convertView 
       .findViewById(R.id.txtcustname); 
     holder.txtcompanyname = (TextView) convertView 
       .findViewById(R.id.txtcompanyname); 
     holder.txtaddress = (TextView) convertView 
       .findViewById(R.id.txtaddress); 
     holder.txttime = (TextView) convertView 
       .findViewById(R.id.txttime); 
     holder.txtrefid = (TextView)convertView.findViewById(R.id.txtrefid); 
     holder.btnphoto1 = (Button)convertView.findViewById(R.id.btnphoto1); 
     holder.btnphoto2 = (Button)convertView.findViewById(R.id.btnphoto2); 
     holder.btnsignpad = (Button)convertView.findViewById(R.id.btnsignature); 

     if (imgurl1list.get(position).equals("anyType{}")) { 
      holder.btnphoto1.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        i = 1; 
        type = title.get(position).toString(); 

        refid = v.getTag().toString(); 
        selectedbutton = holder.btnphoto1; 

        selectImage(); 

       } 
      }); 
     } 
     else{ 
      holder.btnphoto1.setText("Photo Exists"); 
     } 


     if (imgurl2list.get(position).equals("anyType{}")) { 
      holder.btnphoto2.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        i = 2; 
        type = title.get(position).toString(); 
        refid = v.getTag().toString(); 
        selectedbutton = holder.btnphoto2; 
        selectImage(); 
       } 
      }); 
     } 
     else{ 
      holder.btnphoto2.setText("Photo Exists"); 
     } 
     if (imgurl3list.get(position).equals("anyType{}")) { 
      holder.btnsignpad.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        type = title.get(position).toString(); 
        refid = v.getTag().toString(); 
        selectedbutton = holder.btnsignpad; 
        signpadselection(); 
       } 
      });  
     } 
     else{ 
      holder.btnsignpad.setText("Signature Exists"); 
     } 

     convertView.setTag(holder); 
    } 

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


    holder.txttitle.setText(title.get(position)); 
    holder.txtrefid.setText(uniquerefid.get(position)); 
    holder.txttitle.setTag(uniquerefid.get(position)); 
    holder.txtaddress.setText(address.get(position)); 
    holder.txtcustname.setText(customername.get(position)); 
    holder.txtcompanyname.setText(companyname.get(position)); 
    holder.txtproduct.setText(product.get(position)); 
    holder.txttime.setText(createdon.get(position)); 
    holder.btnphoto1.setTag(uniquerefid.get(position)); 
    holder.btnphoto2.setTag(uniquerefid.get(position)); 
    holder.btnsignpad.setTag(uniquerefid.get(position)); 




    return convertView; 
} 

class ViewHolder { 
    TextView txttitle, txtcompanyname, txtproduct, txtcustname, 
      txtaddress, txttime, txtrefid; 
    Button btnphoto1, btnphoto2, btnsignpad; 

} 

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return title.size(); 
} 

}

回答

0

您通過setOnClickListener指定綁定到position這是在第一次調用傳遞給getItem()創建視圖時,聽衆裏面。所以即使你滾動,position仍然是一樣的。你應該把它存儲在holder代替:

holder.position = position; 

,並更改

title.get(position).toString(); 

title.get(holder.position).toString(); 

要確保你改變了一切,從position參數刪除final預選賽上,因爲它不是必要了。

此外,getItemId()返回0不是一個好主意(雖然我不確定它會影響滾動,但肯定會影響其他事情)。相反,最好返回position

+0

感謝您的回答。但沒有工作:( – 2014-09-04 09:28:02

+0

@MayuriRuparel你可以發佈更新的代碼? – 2014-09-04 09:52:39