2011-04-25 115 views

回答

6

製作圖像列表/圖像陣列(圖像編號)&將圖像設置在下一行。

 @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     View v; 
     if(convertView==null){ 
      LayoutInflater li = getLayoutInflater(); 
      v = li.inflate(R.layout.icon, null); 
      TextView tv = (TextView)v.findViewById(R.id.icon_text); 
      tv.setText("Profile "+position); 
      ImageView iv = (ImageView)v.findViewById(R.id.icon_image); 
    ---> iv.setImageResource(R.drawable.icon); 

     } 
     else 
     { 
      v = convertView; 
     } 
     return v; 
    } 

更改此iv.setImageResource(R.drawable.icon);

iv.setImageResource(images[position]);

這是我的全部代碼

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

public class MenuItemAdapter extends BaseAdapter 
{ 
    Context context; 

    private String[] menuNames = {"Menu 1", 
            "Menu 2", 
            "Menu 3", 
            "Menu 4", 
            "Menu 5", 
            "Menu 6"}; 
    private int[] menuIcons = { R.drawable.img0, 
           R.drawable.img1, 
           R.drawable.img2, 
           R.drawable.img3, 
           R.drawable.img4, 
           R.drawable.img4}; 


    public MenuItemAdapter(Context context) 
    { 
     // TODO Auto-generated constructor stub 
     this.context = context; 
    } 

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

    @Override 
    public Object getItem(int position) 
    { 
     // TODO Auto-generated method stub 
     return menuNames[position]; 
    } 

    @Override 
    public long getItemId(int position) 
    { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    private class ViewHolder 
    { 
     public ImageView icon; 
     public TextView label; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     // TODO Auto-generated method stub 
     View vi = convertView; 
     ViewHolder holder; 

     if(convertView == null) 
     { 
      vi = LayoutInflater.from(context).inflate(R.layout.menu_item_layout, null); 
      holder = new ViewHolder(); 

      holder.icon = (ImageView) vi.findViewById(R.id.menu_icon); 
      holder.label = (TextView) vi.findViewById(R.id.menu_text); 

      vi.setTag(holder); 
     } 
     else 
     { 
      holder = (ViewHolder) vi.getTag(); 
     } 
     holder.icon.setImageResource(menuIcons[position]); 
     holder.label.setText(menuNames[position]); 
     return vi; 
    } 

} 

未滿之一:只有我的代碼部分

<GridView 
     android:id="@+id/menu_list" 
     android:numColumns="3" 
     android:gravity="center_horizontal" 
     android:horizontalSpacing="5dp" 
     android:verticalSpacing="5dp" 
     android:stretchMode="columnWidth" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
    /> 

這是活動

public class YourActivity extends Activity implements OnItemClickListener { 

    private GridView menuList; 

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

     menuList = (GridView) findViewById(R.id.menu_list); 
     menuList.setAdapter(new MenuItemAdapter(this)); 
     menuList.setOnItemClickListener(this); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, 
      long id) 
    { 
     // TODO Auto-generated method stub 

    } 

} 
+0

添加我的完整代碼。 – Shaiful 2011-04-25 10:32:32

+0

非常感謝。你也可以把你的XML和其他相關文件 – user667340 2011-04-25 10:35:33

+0

只有我的代碼的一部分。修改以創建自己的。 – Shaiful 2011-04-25 10:40:44