2011-01-07 85 views
0

holder.text.setText(DATA [position]); (位置& 1)== 1?mIcon1:mIcon2); holder.icon.setImageBitmap((position & 1)== 1?mIcon1:mIcon2);android list view view addition

 return convertView; 
    } 

上面下面的代碼讓我只顯示圖標1和2,我已經添加了其他的圖像,但不知道如何將這些圖標添加到列表視圖。

任何幫助將不勝感激,因爲我只是新來的。

回答

1

我不確定這是不是你正在尋找,但你可以嘗試這樣的事情。

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.main); 
    setListAdapter(new CustomAdapter(this)); 
    selection=(TextView)findViewById(R.id.selection); 
} 

class CustomAdapter extends ArrayAdapter { 
     Activity context; 

     CustomAdapter(Activity context) { 
      super(context, R.layout.row, items); 
      this.context=context; 
     } 

public View getView(int position, View convertView, ViewGroup parent) { 
     View row=convertView; 
     if (row==null) { 
      LayoutInflater inflater=context.getLayoutInflater(); 
      row=inflater.inflate(R.layout.row, null); 
     } 

     TextView label=(TextView)row.findViewById(R.id.label); 
     label.setText(items[position]); 
     ImageView icon=(ImageView)row.findViewById(R.id.icon); 
     //you can put your own logic to add images here 
     if (items[position].length()>4) { 
      icon.setImageResource(R.drawable.delete); 
     } 
     else { 
      icon.setImageResource(R.drawable.ok); 
     } 
      return(row); 
     } 
    } 
+0

想要嘗試做的是將圖像添加到每一行。那麼我需要什麼?欣賞它。 – danish 2011-01-08 01:02:52