2011-03-02 36 views
0

我有一個按鈕,當我點擊它時,我想它顯示與圖像&文本(靜態文本)的列表視圖。我怎樣才能做到這一點?在Android中的圖像和文本的列表視圖

My code.. 
public class category extends ListActivity { 
    Button category; 
    TextView selection; 
    private static final String[] country = { "Iceland", "India", "Indonesia", 
     "Iran", "Iraq", "Ireland", "Israel", "Italy", "Laos", "Latvia", 
     "Lebanon", "Lesotho ", "Liberia", "Libya", "Lithuania", 
     "Luxembourg" }; 
     private static final String[] curr = { "ISK", "INR", "IDR", "IRR", "IQD", 
     "EUR", "ILS", "EUR", "LAK", "LVL", "LBP", "LSL ", "LRD", "LYD", 
     "LTL ", "EUR" 

     }; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.category); 

     category=(Button)findViewById(R.id.category); 



     class EfficientAdapter extends BaseAdapter { 
      private LayoutInflater mInflater; 

      public EfficientAdapter(OnClickListener onClickListener) { 
      mInflater = LayoutInflater.from((Context) onClickListener); 

      } 

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

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

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

      public View getView(int position, View convertView, ViewGroup parent) { 
      ViewHolder holder; 
      if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.listview, null); 
      holder = new ViewHolder(); 
      holder.text = (TextView) convertView 
      .findViewById(R.id.TextView01); 
      holder.text2 = (TextView) convertView 
      .findViewById(R.id.TextView02); 

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

      holder.text.setText(curr[position]); 
      holder.text2.setText(country[position]); 

      return convertView; 
      } 

      class ViewHolder { 
      TextView text; 
      TextView text2; 
      } 
      } 
     category.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       ListView l1 = (ListView) findViewById(R.id.ListView); 
       l1.setAdapter(new EfficientAdapter(this)); 

      } 

     }); 

    } 

} 
+0

開始一個活動,它只包含一個button.onClick按鈕使用一個intent來調用這個活動。這是你想要的嗎? –

+0

謝謝你的答覆。但我需要在同一頁面顯示列表視圖(圖片和文字),當我點擊按鈕。 – sanjay

回答

0
  • 「R.layout.listview」 在此佈局文件,其中U在getView方法R膨脹,添加一個ImageView的。

  • 現在在視圖持有者中添加imageview代替第二個textview文本2。

    class ViewHolder { 
        TextView text; 
        ImageView IM2; 
    } 
    
  • 在getView這樣做:

    holder.IM2 = (ImageView) convertView 
          .findViewById(R.id.ImageView02); 
    
  • 在地方holder.text2.setText(country[position]);,在您的ImageView設置背景圖片。

ex。 holder.IM2.setBackgroundDrawable(getResources().getDrawable(R.drawable.vimage));

謝謝。

相關問題