2014-11-21 61 views
0

我正在工作baseadapter。我成功地在列表視圖中調整圖像和文本。我有3個圖像和文本。現在我想通過 位置來改變列表視圖中點擊圖片和文字的背景,這是一個我底座適配器代碼Android更改圖像和文本在列表中查看按位置與開關

public class SlideMenuAdapter extends BaseAdapter { 
private Context mContext; 
private final String[] menu_items_id; 

private final int[] Imageid; 
TextView textView; 
ImageView imageView; 

private static LayoutInflater inflater = null; 

public SlideMenuAdapter(Context context, String[] names, int[] Imageid) 
     { 
    mContext = context; 

    this.Imageid = Imageid; 
    this.menu_items_id = names; 
    inflater = (LayoutInflater) mContext 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 

@Override 
public int getCount() { 

    return Imageid.length; 
} 

@Override 
public Object getItem(int position) { 

    return null; 
} 

@Override 
public long getItemId(int position) { 

    return 0; 
} 

@SuppressLint("ViewHolder") 
@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View grid; 

    grid = new View(mContext); 
    grid = inflater.inflate(R.layout.azercell_slidemenu_adaper, null); 
    textView = (TextView) grid.findViewById(R.id.desc); 
    imageView = (ImageView) grid.findViewById(R.id.photo); 

    textView.setText(menu_items_id[position]); 

    textView.setTypeface(MainmoviesActivity.dejavusans); 





    imageView.setImageResource(Imageid[position]); 
    return grid; 
} 

} 這是一個主要的Java代碼

int[] menu_images = { R.drawable.menu_schedule, R.drawable.menu_about, 
     R.drawable.menu_booking }; 

int[] menu_imagesover = { R.drawable.schedule_ove, R.drawable.about_over, 
     R.drawable.booking_over }; 
private String[] menu_namesEng = { "schedule_ove", "about_over", "about_over" }; 






    listView.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
       int position, long id) { 

      TextView textView1 = (TextView) parent.findViewById(R.id.desc); 

      ImageView imageView=(ImageView)parent.findViewById(R.id.photo); 
      switch (position) { 
      case 0: 
       textView1.setTextColor(Color.parseColor("#8f332a")); 
       imageView.setImageResource(menu_imagesover[position]); 
       break; 

      case 1: 
       textView1.setTextColor(Color.parseColor("#ffffff")); 
       imageView.setImageResource(menu_imagesover[position]); 
       break; 

      default: 
       break; 
      } 


     } 

    }); 

我可以改變形象,文字只列出視圖的冷杉項目,當我點擊第二項時,第一項的圖像和文字又改變了。我究竟做錯了什麼?如果有人知道解決方案,請幫助我。

+0

使用View持有人獲得更好的性能。 – Piyush 2014-11-21 13:44:23

回答

相關問題