2013-12-12 61 views
-1

我想要得到它,這樣,當圖像1顯示,一個TextView顯示其名稱Android的BaseAdapter,退貨項目編號

這裏是我的名字的數組

String[] soundcloudDates = { 
    "Image 1", 
    "Image 2", 
    "Image 3", 
    "Image 4", 
}; 

,這是我的BaseAdapter使用來自GitHub的EcoGallery庫 所有圖像都能正確滾動,但是它們的輸出爲全部位置 更多時候它開始打印輸出位置= 1 ;,直到位置= 3時纔會更改;

private class ImageAdapter extends BaseAdapter { 
    private Context context; 

    ImageAdapter(Context context) { 
     this.context = context; 
    } 

    public int getCount() { 
     return 4; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     // Not using convertView for simplicity. You should probably use it 
     // in real application to get better performance. 
     ImageView imageView = new ImageView(context); 
     int resId; 
     switch (position) { 
     case 0: 
      resId = R.drawable.logo; 
          position = 0; 
          dateView.setText(soundcloudDates[position]); 


      break; 
     case 1: 
      resId = R.drawable.soundcloudtest; 
          position = 0; 
          dateView.setText(soundcloudDates[position]); 


      break; 
     case 2: 
      resId = R.drawable.logo; 
          position = 0; 
          dateView.setText(soundcloudDates[position]); 


      break; 
     case 3: 
      resId = R.drawable.ic_launcher; 
          position = 0; 
          dateView.setText(soundcloudDates[position]); 

      break; 
     default: 
      resId = R.drawable.ic_launcher; 
     } 
     imageView.setImageResource(resId); 
     return imageView; 
    } 
} 

我已經嘗試將setText()放在不同的地方,不起作用。

我不明白什麼即時做錯了:(任何幫助將是巨大的

+1

你爲什麼要設置'位置= 0' – Apoorv

+0

這是從一個遺留下來的我?試圖將位置傳遞迴一個全局變量,看看這是否會工作 –

回答

1

更改這些:

public int getCount() { 
    return soundcloudDates.length; 
} 

public Object getItem(int position) { 
    return soundcloudDates[position]; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    // Not using convertView for simplicity. You should probably use it 
    // in real application to get better performance. 
    ImageView imageView = new ImageView(context); 
    String text = (String) getItem(position); 
    int resId; 
    switch (position) { 
    case 0: 
     resId = R.drawable.logo; 
      break; 
    case 1: 
     resId = R.drawable.soundcloudtest; 
      break; 
    case 2: 
     resId = R.drawable.logo; 
      break; 
    case 3: 
     resId = R.drawable.ic_launcher; 
      break; 
    default: 
     resId = R.drawable.ic_launcher; 
    } 
    dateView.setText(text); 
    imageView.setImageResource(resId); 
    return imageView; 
} 
+0

沒有什麼改變,就像之前textview正在改變,但它似乎是隨機的。一個圖像將設置爲圖像1然後當我回到它它將是圖像4 –

+0

您的dateView TextView在您的getView()方法中看起來沒有初始化。 – Pheel125

+0

好的我如何初始化它?更新到您的最新建議不起作用。感謝您的幫助 –