I want to set images in grid view with multiple columns. Images can be vary in columns.How can i achieve it. Thanks in advance
Images in GridView
create activity_main.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:orientation="vertical"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="3" >
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</RelativeLayout>][1]][1]
Create MainActivity.
public class MainActivity extends Activity {
GridView gv;
Context context;
ArrayList prgmName;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv=(GridView) findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Create a folder by name drawable in res directory. Insert the images into drawable folder by name as below.
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
Create the layout as programlist.xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
Create Customclass ie CustomAdapter.
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.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 position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
You will get the Output like..
Related
I did some research but wasn't able to find a good example.
Current setup:
ArrayList<String> arrCursor = new ArrayList<String>();
List<mydata> md = mydatabase.getthedata("1");
for (mydata cn1 : md) {
arrCursor.add(cn1.getTheFirst());
arrCursor.add(cn1.getTheSecond());
}
ArrayAdapter arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrCursor);
gv.setAdapter(arrayAdapter); //the gridview
GridView layout:
<FrameLayout 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"
tools:context="com.myapp.chao">
<GridView
android:id="#+id/gvalldata"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="auto_fit"
android:gravity="center"
android:stretchMode="columnWidth"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp" />
</FrameLayout>
The above only displays a simple list rather than a custom one.
To start the custom adapter process, I have setup an item list layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvfirst"
android:layout_marginBottom="10dp"
android:gravity="center"
android:textSize="25dp" />
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tvsecond" />
</LinearLayout>
How can I add a custom adapter which will show the gridview using the item list layout and in boxes.
What I am looking for:
This should work make custom adapter and set that adapter to gridview
gv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.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 position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
Try inserting your own custom layout (R.layout.custom_layout) in the ArrayAdapter constructor instead of the android one (***android.**R.layout.simple_list_item_1*)
so E.g
ArrayAdapter arrayAdapter = new ArrayAdapter(getActivity(), R.layout.custom_layout, arrCursor);
I am creating a gridview with different images and texts from a class containing those images and texts in a fragment. The GridView is inside a TabHost. The TabHost is working fine. I can see other buttons in the same view. But nothing is inside the GridView. Here is the code:
public class MenuPage extends Fragment{
GridView CategoryGridview;
View MainMenuView, HomePageView, GalleryView;
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final Context context = getActivity();
this.MainMenuView = inflater.inflate(R.layout.mainmenupage, container, false);
this.GalleryView = inflater.inflate(R.layout.gallery, container, false);
this.CategoryGridview = (GridView)GalleryView.findViewById(R.id.gridViewGallery);
CategoryGridview.setAdapter(new GalleryAdapter(getActivity()));
:
:
:
return MainMenuView;
}
Adapter:
public class GalleryAdapter extends BaseAdapter{
ArrayList<Gallery> list;
Context context;
GalleryAdapter(Context context){
this.context = context;
list = new ArrayList<Gallery>();
int[] TempGalleryId = {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e, R.drawable.f, R.drawable.g, R.drawable.h, R.drawable.i, R.drawable.j, R.drawable.k, R.drawable.l, R.drawable.m, R.drawable.n, R.drawable.o, R.drawable.p, R.drawable.q, R.drawable.r, R.drawable.s};
String[] TempGalleryName = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s"};
for (int i = 0; i < 19; i++){
Gallery TempGallery = new Gallery(TempGalleryId[i], TempGalleryName[i]);
list.add(TempGallery);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
ViewHolder holder = null;
if(row == null){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.single_gallery, parent, false);
holder = new ViewHolder(row);
row.setTag(holder);
}
else{
holder = (ViewHolder) row.getTag();
}
Gallery temp = list.get(position);
holder.imageViewGallery.setImageResource(temp.GalleryId);
return row;
}
}
class ViewHolder{
ImageView imageViewGallery;
ViewHolder(View v)
{
imageViewGallery = (ImageView) v.findViewById(R.id.imageViewGallery);
}
}
class Gallery{
int GalleryId;
String GalleryName;
Gallery(int GalleryId, String GalleryName){
this.GalleryId = GalleryId;
this.GalleryName = GalleryName;
}
}
In mainmenupage.xml, one of the tab include gallery.xml file for animation with a gridview imageViewGallery and some buttons in it.
<LinearLayout
android:orientation="vertical"
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/DinnerList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<include
android:id="#+id/gallery"
layout="#layout/gallery" />
</LinearLayout>
SingleGallery.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageViewGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/a" />
</RelativeLayout>
This is Gallery.xml
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btnBack"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Back" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/button1"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<GridView
android:id="#+id/gridViewGallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnBack"
android:numColumns="auto_fit"
android:horizontalSpacing="15dp"
android:verticalSpacing="15dp"
android:stretchMode="spacingWidthUniform">
</GridView>
</RelativeLayout>
I tried change
CategoryGridview.setAdapter(new GalleryAdapter(getActivity()));
to
CategoryGridview.setAdapter(new GalleryAdapter(context));
and
this.CategoryGridview = GridView)MainMenuView.findViewById(R.id.gridViewGallery);
to
this.CategoryGridview = (GridView)GalleryView.findViewById(R.id.gridViewGallery);
still doesn't work.
I also tried open a new project with extend Activity(){}. It work fine with
CategoryGridview.setAdapter(new GalleryAdapter(this));
So I guess the problem is somewhere else. Can anyone help?
this is the code
image is not displayed into list view how can i solve this problem?I set uri value into bitmap.so what is the problem?changed the array and error is solved bt still image is not displayed into list view.
MainActivity.java
public class MainActivity extends Activity {
ListView lv;
Context context;
ArrayList prgmName;
Bitmap bitmap;
//bitmap=getBitmapFromUrl("http://content6.flixster.com/movie/11/17/75/11177548_pro.jpg");
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bitmap=getBitmapFromUrl("http://content6.flixster.com/movie/11/17/75/11177548_pro.jpg");
context=this;
Bitmap [] prgmImages ={bitmap,bitmap,bitmap,bitmap,bitmap,bitmap,bitmap,bitmap,bitmap};
lv=(ListView) findViewById(R.id.listView);
lv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private Bitmap getBitmapFromUrl(String src) {
// TODO Auto-generated method stub
try {
URL url =new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input=connection.getInputStream();
Bitmap mybiBitmap=BitmapFactory.decodeStream(input);
return mybiBitmap;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
Bitmap[] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, Bitmap[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.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 position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_item, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
// holder.tv.setText(result[position]);
// holder.img.setImageBitmap(imageId[position]);
if(result != null && result.length > position)
{
holder.tv.setText(result[position]);
}
if(imageId != null && imageId.length > 0)
{
holder.img.setImageBitmap(imageId[0]);
}
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
activity_main.xml
<LinearLayout 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:orientation="vertical"
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=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text=" Your Theaters..." />
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<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_gravity="center"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:text="TextView" />
</LinearLayout>
It probably happens because before bitmaps get loaded and saved to your array, you call adapter. Downloading images takes some time and your adapter is not waiting for them. See this example, it is using image loader: http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
Are there any NetworkOnMainThreadException in the log ?
When calling getBitmapFromUrl from onCreate, you are downloading the bitmap on the main thread, which is prohibited if your application is targeting Honeycomb or later. In your case it will fail silently because you are catching the exception but you will get a null result.
As loading images from network is very common, there are a lot of library that handle this (and a lot more like caching) for you, e.g. Universal-Image-Loader
or ion
I have List view in layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<ListView
android:id="#+id/listInbox"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:layout_weight="20"
android:listSelector="#drawable/list_selector" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="80"
android:orientation="horizontal" >
<Button
android:id="#+id/bdelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="#string/delete_selected" />
</LinearLayout>
</LinearLayout>
and This is the layout adapter for this list view
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:background="#drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="#+id/list_image"
android:layout_width="50dip"
android:layout_height="50dip" />
</LinearLayout>
<!-- Title Of Song -->
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:text="Title"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<!-- Artist Name -->
<TextView
android:id="#+id/artist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_marginTop="1dip"
android:layout_toRightOf="#+id/thumbnail"
android:gravity="left"
android:text="Sender"
android:textColor="#343434"
android:textSize="15sp" />
<!-- Rightend Duration -->
<!-- Rightend Arrow -->
<ImageView
android:id="#+id/ivArraw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/url" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/artist"
android:layout_alignBottom="#+id/artist"
android:layout_alignRight="#+id/artist"
android:visibility="invisible"
/>
</RelativeLayout>
and that what the adapter defined in my activity
lv.setAdapter(new ImageInboxAdapter(inbox.this, bitmap, messageNo, messageTitle, senderId, senderName,selected));
and this is the baseadapter code
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
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 ImageNewsAdapter extends BaseAdapter{
Context context;
String[] newsNo,title_news;
Bitmap[] bitmap;
public ImageNewsAdapter(Context context,String[] newsNo,String[] title_news, Bitmap[] bitmap ) {
this.context=context;
this.newsNo=newsNo;
this.title_news=title_news;
this.bitmap=bitmap;
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return title_news.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if(convertView==null){
gridView=new View(context);
gridView=inflater.inflate(R.layout.custom_news, null);
}else{
gridView = (View) convertView;
}
TextView tv=(TextView)gridView.findViewById(R.id.tvTitle);
tv.setText(title_news[position]);
/*RelativeLayout rlImageMain=(RelativeLayout)gridView.findViewById(R.id.RlImageTitle);
rlImageMain.setBackground(new BitmapDrawable(bitmap[position]));
*/
ImageView ivMain= (ImageView)gridView.findViewById(R.id.ivMain);
ivMain.setImageBitmap(bitmap[position]);
return gridView;
}
}
the question I want to get checked values from checkbox and return it to my activity
I found the solution
first make a static Boolean variable
static Boolean checkboxstate[];
then define it in the constructor with the size of the list
checkboxstate=new Boolean[messageNo.length];
then make your checkbox on click listener and fill the boolean array
if(checkboxstate[position]==null){
checkboxstate[position]=false;
}
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(((CheckBox)v).isChecked()){
checkboxstate[position]=true;
v.setSelected(true);
}else{
checkboxstate[position]=false;
v.setSelected(false);
}
}
});
finally
public class ImageInboxAdapter extends BaseAdapter{
Context context;
String[] messageNo,messageTitle,senderId,senderName;
Bitmap[] bitmap;
Boolean selected;
static Boolean checkboxstate[];
int checkedp;
public ImageInboxAdapter(Context context, Bitmap[] bitmap,String[] messageNo,String[] messageTitle, String[] senderId,String[] senderName,Boolean selected) {
this.context=context;
this.bitmap=bitmap;
this.messageNo=messageNo;
this.messageTitle=messageTitle;
this.senderId=senderId;
this.senderName=senderName;
this.selected=selected;
checkboxstate=new Boolean[messageNo.length];
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return messageNo.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#SuppressLint("NewApi")
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if(convertView==null){
gridView=new View(context);
gridView=inflater.inflate(R.layout.list_row_message, null);
}else{
gridView = (View) convertView;
}
TextView tvTitle=(TextView)gridView.findViewById(R.id.title);
tvTitle.setText(messageTitle[position]);
TextView tvSender=(TextView)gridView.findViewById(R.id.artist);
tvSender.setText(senderName[position]);
CheckBox cb=(CheckBox)gridView.findViewById(R.id.checkBox1);
ImageView ivArraw=(ImageView)gridView.findViewById(R.id.ivArraw);
/*RelativeLayout rlImageMain=(RelativeLayout)gridView.findViewById(R.id.RlImageTitle);
rlImageMain.setBackground(new BitmapDrawable(bitmap[position]));
*/
ImageView ivMain= (ImageView)gridView.findViewById(R.id.list_image);
ivMain.setImageBitmap(bitmap[position]);
if(selected==true){
cb.setVisibility(CheckBox.VISIBLE);
ivArraw.setVisibility(ImageView.INVISIBLE);
if(checkboxstate[position]==null){
checkboxstate[position]=false;
}
cb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(((CheckBox)v).isChecked()){
checkboxstate[position]=true;
v.setSelected(true);
}else{
checkboxstate[position]=false;
v.setSelected(false);
}
}
});
}
return gridView;
}
}
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);