class 1:
public class FruitsMenuAdapter extends BaseAdapter{ private String []web=null; private int[] imageId=null; Context context; public FruitsMenuAdapter (Context context,String[] web,int[] imageId) { this.web = web; this.context=context; this.imageId=imageId; } @Override public int getCount() { return web.length; } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { View view; LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if(convertView==null){ view =new View(context); view=inflater.inflate(R.layout.fruits_list,null); ImageView imageView=(ImageView)view.findViewById(R.id.imagefruits); TextView textView=(TextView)view.findViewById(R.id.textfruits); textView.setText(web[position]); imageView.setImageResource(imageId[position]); } else{ view=(View)convertView; } return view; } }
class 2:
public class FruitsActivity extends AppCompatActivity { String[] web = { "Mango", "Panana", "apple", "orange", "papaya", "grape", "wotermelon", "pear", "pineapple", "guava", "gapefruits", "asianbear", } ; int[] imageId = { R.drawable.fruits, R.drawable.fruits, R.drawable.fruits, R.drawable.fruits, R.drawable.fruits, R.drawable.fruits, R.drawable.fruits, R.drawable.fruits, R.drawable.fruits, R.drawable.fruits, R.drawable.fruits, R.drawable.fruits, }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_fruits); GridView gridView=(GridView)findViewById(R.id.gridfruits) ; FruitsMenuAdapter adapt=new FruitsMenuAdapter(FruitsActivity.this,web,imageId); gridView.setAdapter(adapt); }}
No comments:
Post a Comment