How to display grid view with images? - android

I'm trying to display grid view of images.I have tried the following code but my application gets force close after running.Where i have gone wrong?
My code:
AndroidGridLayoutActivity:
public class AndroidGridLayoutActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gv=(GridView)findViewById(R.id.grid_view);
gv.setAdapter(new ImageAdapter(this));
}
}
ImageAdapter.java:
public class ImageAdapter extends BaseAdapter {
private Context mcontext;
public Integer[] mThumbids={R.drawable.img9,R.drawable.img10,R.drawable.img11,R.drawable.img12,R.drawable.img13,R.drawable.img14};
public ImageAdapter(Context c)
{
mcontext=c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbids.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbids[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageView=new ImageView(mcontext);
imageView.setImageResource(mThumbids[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70,70));
return imageView;
}
}
main.xml:
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:stretchMode="columnWidth"/>

public class MainActivity extends Activity {
GridView gv1;
GridViewAdapter madapter;
int arr[]={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String arr1[]={"ashu","hello","yes","no"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv1=(GridView)findViewById(R.id.gridView1);
madapter=new GridViewAdapter();
gv1.setAdapter(madapter);
}
class GridViewAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
return arr1.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater infla=getLayoutInflater();
View v=infla.inflate(R.layout.main1,null);
TextView tv1=(TextView)v.findViewById(R.id.textView1);
ImageView iv1=(ImageView)v.findViewById(R.id.imageView1);
tv1.setText(arr1[position]);
iv1.setBackgroundResource(arr[position]);
return v;
}}
}
activity_main.xml
<GridView
android:id="#+id/gridView1" android:layout_width="match_parent" android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/> </RelativeLayout>
main1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:text="TextView" /> </RelativeLayout>

Related

populate different gridview elements in android activity on button click

In my android application how can I display different icons in the same GridView on button click event. I have to keep on interchanging between two sets of icons in my gridview on button click. What I have done is that I have made two activities each with a button and different set of icons in a GridView and keep on switching between those activities on button click. But is there any better approach for this that on same activity I can just change the gridview elements on button click? Thanks.
Edited
I am doing this somehow using the ViewSwitcher by the following code:
JavaActivity Code:
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
GridView EngGrid,UrduGrid;
final ViewSwitcher switcher;
Button Next, Previous;
EngGrid=(GridView) findViewById(R.id.gridView1);
EngGrid.setAdapter(new EngAdapter(this));
EngGrid.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
// Toast.makeText(MenuActivity.this, "" + position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), DisplayActivity.class);
i.putExtra("menu_id", position);
startActivity(i);
}
});
UrduGrid=(GridView) findViewById(R.id.gridView2);
UrduGrid.setAdapter(new UrduAdapter(this));
UrduGrid.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
// TODO Auto-generated method stub
// Toast.makeText(MenuActivity.this, "" + position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(getApplicationContext(), DisplayActivity.class);
i.putExtra("menu_id", position);
startActivity(i);
}
});
switcher = (ViewSwitcher) findViewById(R.id.ViewSwitcher);
Next = (Button) findViewById(R.id.button2);
Previous = (Button) findViewById(R.id.button1);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new AnimationUtils();
switcher.setAnimation(AnimationUtils.makeInAnimation
(getBaseContext(), true));
switcher.showNext();
}
});
Previous.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
new AnimationUtils();
switcher.setAnimation(AnimationUtils.makeInAnimation
(getBaseContext(), true));
switcher.showPrevious();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_test, menu);
return true;
}
public class UrduAdapter extends BaseAdapter{
public Integer[] mThumbIds = {
R.drawable.urdu_dua1, R.drawable.urdu_dua2,
R.drawable.urdu_dua3, R.drawable.urdu_dua4,
R.drawable.urdu_dua5, R.drawable.urdu_dua6,
R.drawable.urdu_dua7, R.drawable.urdu_dua8,
R.drawable.urdu_dua9, R.drawable.urdu_dua10,
R.drawable.urdu_dua11, R.drawable.urdu_dua12,
R.drawable.urdu_dua13, R.drawable.urdu_dua14,
R.drawable.urdu_dua15, R.drawable.urdu_dua16,
R.drawable.urdu_dua17, R.drawable.urdu_dua18,
R.drawable.urdu_dua19, R.drawable.urdu_dua20,
R.drawable.urdu_dua21
};
private Context mContext;
public UrduAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View MyView;
if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView =li.inflate(R.layout.urdumenuitem, parent,false);
}
else{
MyView =(View)convertView;
}
ImageView iv=(ImageView)MyView.findViewById(R.id.image1);
iv.setImageResource(mThumbIds[position]);
return MyView;
}
}
public class EngAdapter extends BaseAdapter{
public Integer[] mThumbIds = {
R.drawable.eng_pic1, R.drawable.eng_pic2,
R.drawable.eng_pic3, R.drawable.eng_pic4,
R.drawable.eng_pic5, R.drawable.eng_pic6,
R.drawable.eng_pic7, R.drawable.eng_pic8,
R.drawable.eng_pic9, R.drawable.eng_pic10,
R.drawable.eng_pic11, R.drawable.eng_pic12,
R.drawable.eng_pic13, R.drawable.eng_pic14,
R.drawable.eng_pic15, R.drawable.eng_pic16,
R.drawable.eng_pic17, R.drawable.eng_pic18,
R.drawable.eng_pic19, R.drawable.eng_pic20,
R.drawable.eng_pic21
};
private Context mContext;
public EngAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View MyView;
if(convertView == null){
LayoutInflater li=((Activity) mContext).getLayoutInflater();
MyView =li.inflate(R.layout.urdumenuitem, parent,false);
}
else{
MyView =(View)convertView;
}
ImageView iv=(ImageView)MyView.findViewById(R.id.image1);
iv.setImageResource(mThumbIds[position]);
return MyView;
}
}
}
XML_layout:
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ViewSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0EEE0"
>
<GridView
android:id="#+id/gridView1"
android:layout_below="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:horizontalSpacing="5dp"
android:layout_margin="10dp"
android:stretchMode="columnWidth"
android:gravity="center_vertical"
/>
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/gridView1"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="#drawable/settings"
android:focusable="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0EEE0"
>
<GridView
android:id="#+id/gridView2"
android:layout_below="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="2"
android:horizontalSpacing="5dp"
android:layout_margin="10dp"
android:stretchMode="columnWidth"
android:gravity="center_vertical"
/>
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/gridView2"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:background="#drawable/settings"
android:focusable="true" />
</RelativeLayout>
</ViewSwitcher>
Question still remains, how can we switch between the two sets of icons in the same GridView on button click.
In your GridView's adapter, you can create a method that changes the icons of the views, for example:
private boolean imageSetChange = false;
public void changeImages(boolean change){
this.imageSetChange = change;
notifyDataSetChanged();
}
Then in your getView() method, you switch the item depending on the value of imageSetChanged.

Customlistview not inflating

I was trying to implement simple customlistview.What i tried to do is to inflate a text and a picture inside each list row in the listview.I got no output i.e. layout was not inflating..I got a blank screen.I used 2 Java class and 2 xml files.Below is my code..Please let me know where is my mistake and what should i correct?
CListView.java
package com.example.customlistview;
public class Clistview extends Activity {
int p1[]={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String[] Shapes1={"circle","circle","circle","circle","circle"};
ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clistview);
lv=(ListView)findViewById(R.id.listView1);
CustomListViewAdapter clvadapter=new CustomListViewAdapter(Clistview.this,Shapes1,p1);
lv.setAdapter(clvadapter);
}
}
CustomListViewAdapter.java
package com.example.customlistview;
public class CustomListViewAdapter extends BaseAdapter {
Context context;
String shape1[];
int pic[];
public CustomListViewAdapter(Context c,String[] sh,int[] p)
{
this.context=c;
this.shape1=sh;
this.pic=p;
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
public String getItem(int pos) {
// TODO Auto-generated method stub
return shape1[pos];
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View v, ViewGroup vg) {
// TODO Auto-generated method stub
View itemview=v;
TextView tv1;
ImageView im1;
if(itemview==null)
{
LayoutInflater inflater=(LayoutInflater)((Activity)context).getLayoutInflater();
itemview=inflater.inflate(R.layout.clistview1,vg,false);
}
tv1=(TextView)itemview.findViewById(R.id.textView1);
im1=(ImageView)itemview.findViewById(R.id.imageView1);
tv1.setText(shape1[pos]);
im1.setImageResource(pic[pos]);
return itemview;
}
}
activity_clistview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Clistview" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp" >
</ListView>
</RelativeLayout>
clistview1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
/>
</LinearLayout>
Inside CustomListViewAdapter, change this
public int getCount() {
// TODO Auto-generated method stub
return shape1.length;
}
It is an important function that decides the number of items in a ListView.
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
You are returning 0 .
Return pic.length or shape1.length as per your requirement.
public int getCount() {
return pic.length;
}
Method getCount() returns the number of items in the datatset for the adapter.
getCount()

Display grid view with multiple images? android

I want to display the grid view with multiple images on my Galaxy Tab...
I am new to the development, i will really appreciate for any help.
Vivek
public class MainActivity extends Activity {
GridView gv1;
GridViewAdapter madapter;
int arr[]={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String arr1[]={"ashu","hello","yes","no"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv1=(GridView)findViewById(R.id.gridView1);
madapter=new GridViewAdapter();
gv1.setAdapter(madapter);
}
Adapter class:
class GridViewAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
return arr1.length; }
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position; }
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0; }
#Override public View getView(int position, View convertView,
ViewGroup parent) {
LayoutInflater infla=getLayoutInflater();
View v=infla.inflate(R.layout.main1,null);
TextView tv1=(TextView)v.findViewById(R.id.textView1);
ImageView iv1=(ImageView)v.findViewById(R.id.imageView1);
tv1.setText(arr1[position]);
iv1.setBackgroundResource(arr[position]);
return v; }}
}
activity_main.xml
<GridView
android:id="#+id/gridView1" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/> </RelativeLayout>
main1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView1"
android:text="TextView" /> </RelativeLayout>

how to apply Gridview with fixrows and horizontal scrollbar

I want to implemented gridview with three rows and multiple column. I tried below code but not able to achieve what i want.
my xml file is
<LinearLayout
android:layout_width="300dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="340dp"
android:layout_height="wrap_content"
android:fillViewport="true" >
<GridView
android:id="#+id/gridView"
android:layout_width="340dp"
android:layout_height="385dp"
android:layout_marginBottom="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:padding="5dp"
android:scrollbars="horizontal">
</GridView>
</HorizontalScrollView>
</LinearLayout>
and activity file code is
gridView.setAdapter(new ImageAdapter(this,id_list,favflag_list,thumb_img_list));
gridView.setNumColumns(5);
is it achievable or any other way
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
Binding Adapter :
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_2,
R.drawable.pic_3, R.drawable.pic_4,
R.drawable.pic_5, R.drawable.pic_6,
R.drawable.pic_7, R.drawable.pic_8,
R.drawable.pic_9, R.drawable.pic_10,
R.drawable.pic_11, R.drawable.pic_12,
R.drawable.pic_13, R.drawable.pic_14,
R.drawable.pic_15
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
Main Activity :
public class AndroidGridLayoutActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
}
}
it is depend on your data. if you add more data it will change its size. Example. if you have 9 picture to store in gridview. you must to set android:numColumns = "3" in xml, it will show you 3 rows.
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Custom xml layout :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imagepart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/textpart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
Main Activity :
public class CustomGridView extends Activity {
// references to our images
private Integer[] mThumbIds = {
R.drawable.androider_01,
R.drawable.androider_02,
R.drawable.androider_03,
R.drawable.androider_04,
R.drawable.androider_05,
R.drawable.androider_06,
R.drawable.androider_07,
R.drawable.androider_08,
R.drawable.androider_09,
R.drawable.androider_10,
R.drawable.androider_11,
R.drawable.androider_12,
R.drawable.androider_13,
R.drawable.androider_14,
R.drawable.androider_15,
R.drawable.androider_16
};
public class MyAdapter extends BaseAdapter {
private Context mContext;
public MyAdapter(Context c) {
// TODO Auto-generated constructor stub
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mThumbIds.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mThumbIds[arg0];
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View grid;
if(convertView==null){
grid = new View(mContext);
LayoutInflater inflater=getLayoutInflater();
grid=inflater.inflate(R.layout.mygrid, parent, false);
}else{
grid = (View)convertView;
}
ImageView imageView = (ImageView)grid.findViewById(R.id.imagepart);
TextView textView = (TextView)grid.findViewById(R.id.textpart);
imageView.setImageResource(mThumbIds[position]);
textView.setText(String.valueOf(position));
return grid;
}
}
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new MyAdapter(this));
}
}

How to add ImageView, TextView and Button in ListView in android

In my application I am trying to create a ListView that contains an ImageView a TextView and a Button. I have created a separate XML file and drag all the above mentioned elements in that XML and in my main java file I have created an Object of BaseAdapter and in the getView() method I have declared these elements but when I run the application I cant see the list. I haven't used the BaseAdapter, so I am missing with some code in order to view the List. I also want to apply some operations on the button so please let me know that code also.
Code for my main.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Code for my list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="0dp"
android:layout_marginTop="15dp"
android:text="Medium Text"
android:textSize="15dp" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="25dp"
android:text="Button" />
</LinearLayout>
Code for my main.java file:
public class CustomListActivity extends Activity {
/** Called when the activity is first created. */
ListView lv;
LayoutInflater inflator;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.listView1);
BaseAdapter bs = new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vw = inflator.inflate(R.layout.list_items, null);
ImageView img = (ImageView)findViewById(R.id.imageView1);
TextView tv = (TextView)findViewById(R.id.textView1);
Button btn = (Button)findViewById(R.id.button1);
return vw;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
};
}
}
Thanks in advance....
ok,
public class App_Adapter extends BaseAdapter implements OnClickListener{
private Activity mActivity;
private List<App_List> mList;
private static LayoutInflater inflater=null;
private PackageManager pm;
private String appclass;
private ApplicationTask mApplicationTask;
private String link=null;
public App_Adapter (FavouriteApp favouriteApp,List<App_List> mAppList, String appclass) {
// TODO Auto-generated constructor stub
this.mActivity= favouriteApp;
this.mList= mAppList;
this.appclass = appclass;
inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pm=mActivity.getPackageManager();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View mView=convertView;
if (convertView == null)
mView = inflater.inflate(R.layout.app_adapter, parent,false);
App_List mAppList= mList.get(position);
**here i am setting two textview and one button**
((TextView) mView.findViewById(R.id.textView_appName)).setText(mAppList.getApp_Name());
((TextView) mView.findViewById(R.id.textView_appDescription)).setText(mAppList.getApp_Description());
boolean status = isAppInstalled(mAppList.getApp_Pkg());
Button btn = (Button) mView.findViewById(R.id.button_appStatus);
**// register the button for clicklistener**
btn.setOnClickListener(this);
return mView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
and call this adapter class from your activity.
You are definitely missing lv.setAdapter(bs);

Categories

Resources