I have added many images in the assets folder and want to get those images from there and add it into a ListView.
When i am executing the app everything is perfect but the image container of the listview appears blank(transparent).
Output
The Java File:
package com.basil.victor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.io.IOException;
import java.io.InputStream;
public class Events extends Fragment {
private ListView listEvent;
String eventname[]={
"Name",
"of",
"the",
"events",
"are",
"present",
"here"
};
String eventlogoname[]={
"Logo",
"name",
"of",
"events",
"are",
"present",
"here"
};
Drawable[] arr=new Drawable[7];
String eventsubtitle []={
"Subtitles",
"of",
"the",
"events",
"are",
"present",
"here"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_events, null);
for(int i=0;i<7;i++) {
try {
InputStream stream = getContext().getAssets().open(eventlogoname[i] + ".jpg");
Drawable el = Drawable.createFromStream(stream, null);
arr[i] = el;
} catch (IOException e) {
e.printStackTrace();
}
}
EventList adapter = new
EventList(getActivity(), eventname, arr, eventsubtitle);
//ListView lv = (ListView)rootView.
listEvent=(ListView)view.findViewById(R.id.listEvent);
listEvent.setAdapter(adapter);
return view;
}
}
ListView adapter:
package com.basil.victor;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class EventList extends ArrayAdapter<String>{
private final Activity context;
private final String[] title;
private final Drawable[] banner;
private final String[] subtitle;
public EventList(Activity context,
String[] title, Drawable[] banner, String[] subtitle) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.banner = banner;
this.subtitle = subtitle;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.event_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.event_title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.event_banner);
TextView subTitle = (TextView) rowView.findViewById(R.id.event_subtitle);
txtTitle.setText(title[position]);
imageView.setImageDrawable(banner[position]);
subTitle.setText(subtitle[position]);
return rowView;
}
}
If you're getting a FileNotFound Exception,
Check if the image is inside a subfolder in assets and add the appropriate path
Check the case of the extension (I faced this problem for a long time before I realized my image had a .JPG (Uppercase) extension)
The final string that's being searched, check for random whitespaces
Why are using asset folder for images? if it is not necessary then you can use drawable for same put all images in drawable folder and
package com.basil.victor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.io.IOException;
import java.io.InputStream;
public class Events extends Fragment {
private ListView listEvent;
String eventname[]={
"Name",
"of",
"the",
"events",
"are",
"present",
"here"
};
int[] arr=new int[7];
String eventsubtitle []={
"Subtitles",
"of",
"the",
"events",
"are",
"present",
"here"
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_events, null);
arr[]={
R.drawable.Logo,
R.drawable.name,
R.drawable.of,
R.drawable.events,
R.drawable.are,
R.drawable.present,
R.drawable.here
};
EventList adapter = new
EventList(getActivity(), eventname, arr, eventsubtitle);
//ListView lv = (ListView)rootView.
listEvent=(ListView)view.findViewById(R.id.listEvent);
listEvent.setAdapter(adapter);
return view;
}
}
adapter:
package com.basil.victor;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class EventList extends ArrayAdapter<String>{
private final Activity context;
private final String[] title;
private final int[] banner;
private final String[] subtitle;
public EventList(Activity context,
String[] title, int[] banner, String[] subtitle) {
super(context, R.layout.list_single, title);
this.context = context;
this.title = title;
this.banner = banner;
this.subtitle = subtitle;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.event_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.event_title);
ImageView imageView = (ImageView) rowView.findViewById(R.id.event_banner);
TextView subTitle = (TextView) rowView.findViewById(R.id.event_subtitle);
txtTitle.setText(title[position]);
imageView.setImageResource((banner[position]);
subTitle.setText(subtitle[position]);
return rowView;
}
}
Do it like this
class MainActivity extends AppCompatActivity {
private ListView listEvent;
String arras[]={
"Logo",
"name",
"of",
"events",
"are",
"present",
"here"
};
Drawable[] arr=new Drawable[5];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventList adapter = new EventList(MainActivity.this, arras);
//ListView lv = (ListView)rootView.
listEvent=(ListView)findViewById(R.id.listEvent);
listEvent.setAdapter(adapter);
}
}
And Adapter class is like this
public class EventList extends BaseAdapter {
LayoutInflater inflater;
private final Activity context;
String arras[];
public EventList(Activity context,String[] arras) {
super();
this.context = context;
this.arras = arras;
inflater = (LayoutInflater) this.context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return arras.length;
}
#Override
public Object getItem(int position) {
return arras.length;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View myView, ViewGroup parent) {
if (myView == null) {
myView = inflater.inflate(R.layout.event_row, null, true);
}
ImageView imageView = (ImageView) myView.findViewById(R.id.event_banner);
try {
InputStream stream = context.getAssets().open(arras[position] + ".jpg");
Drawable el = Drawable.createFromStream(stream, null);
imageView.setImageDrawable(el);
} catch (IOException e) {
e.printStackTrace();
}
return myView;
}
}
Use a ViewHolder
ViewHolder makes the list view to load faster by caching the views. If it look up the view every time by calling the findViewById(), it will be very slow.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
ViewHolder holder; // to reference the child views for later actions
if (v == null) {
LayoutInflater vi =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.mainrow, null);
// cache view fields into the holder
holder = new ViewHolder();
holder.ImageView = (ImageView) v.findViewById(R.id.event_banner);
// associate the holder with the view for later lookup
v.setTag(holder);
} else {
// view already exists, get the holder instance from the view
holder = (ViewHolder) v.getTag();
}
// no local variables with findViewById here
// use holder.ImageView where you were
// using the local variable nameText before
try {
InputStream stream = context.getAssets().open(arras[position] + ".jpg");
Drawable el = Drawable.createFromStream(stream, null);
holder.ImageView.setImageDrawable(el);
} catch (IOException e) {
e.printStackTrace();
}
return v;
}
// somewhere else in your class definition
static class ViewHolder {
ImageView imageView;
}
Related
package com.example.test;
import android.content.Context;
import android.provider.DocumentsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class ProgramAdapter extends ArrayAdapter<String> {
Context context;
int[] images;
String[] programName;
String[] programDescription;
public ProgramAdapter( Context context, String [] programName, int[] images, String[] programDescription) {
super(context, R.layout.single_item, R.id.textView1, programName);
this.context = context;
this.images = images;
this.programName = programName;
this.programDescription = programDescription; }
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View singleItem = convertView;
ProgramViewHolder holder = null;
if(singleItem == null){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
singleItem = LayoutInflater.inflate(R.layout.single_item, parent, false);
holder = new ProgramViewHolder(singleItem);
singleItem.setTag(holder); }
else{
holder = (ProgramViewHolder) singleItem.getTag(); }
holder.itemImage.setImageResource(images[position]);
holder.programTitle.setText(programName[position]);
holder.programDescription.setText(programDescription[position]);
singleItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "You clicked:" + programName[position], Toast.LENGTH_SHORT ).show();
}
});
return singleItem;
}
}
I am trying to create a Listview image with text in Android Studio. There is only one error shown up in the following line:
singleItem = LayoutInflater.inflate(R.layout.single_item, parent, false);
I have 3 classes: MainActivity, ProgramAdapter and ProgramViewHolder
Non-static method 'inflate(int, android.view.ViewGroup, boolean)' cannot be referenced from a static context
You are not using layoutInflater object
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater is a class.
singleItem = LayoutInflater.inflate(R.layout.single_item, parent, false);
Change it to
singleItem = layoutInflater.inflate(R.layout.single_item, parent, false);
I followed this lesson here which talk about json parsing and image loading in list view the example works good but it has a problem in imageview when scrolling up & down all images reloading so can anyone solve this problem?
this the adapter of listview
package com.wingnity.jsonparsingtutorial;
import java.io.InputStream;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ActorAdapter extends ArrayAdapter<Actors> {
ArrayList<Actors> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;
public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
super(context, resource, objects);
vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Resource = resource;
actorList = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// convert view = design
View v = convertView;
if (v == null) {
holder = new ViewHolder();
v = vi.inflate(Resource, null);
holder.imageview = (ImageView) v.findViewById(R.id.ivImage);
holder.tvName = (TextView) v.findViewById(R.id.tvName);
holder.tvDescription = (TextView) v.findViewById(R.id.tvDescriptionn);
holder.tvDOB = (TextView) v.findViewById(R.id.tvDateOfBirth);
holder.tvCountry = (TextView) v.findViewById(R.id.tvCountry);
holder.tvHeight = (TextView) v.findViewById(R.id.tvHeight);
holder.tvSpouse = (TextView) v.findViewById(R.id.tvSpouse);
holder.tvChildren = (TextView) v.findViewById(R.id.tvChildren);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.imageview.setImageResource(R.drawable.ic_launcher);
new DownloadImageTask(holder.imageview).execute(actorList.get(position).getImage());
holder.tvName.setText(actorList.get(position).getName());
holder.tvDescription.setText(actorList.get(position).getDescription());
holder.tvDOB.setText("B'day: " + actorList.get(position).getDob());
holder.tvCountry.setText(actorList.get(position).getCountry());
holder.tvHeight.setText("Height: " + actorList.get(position).getHeight());
holder.tvSpouse.setText("Spouse: " + actorList.get(position).getSpouse());
holder.tvChildren.setText("Children: " + actorList.get(position).getChildren());
return v;
}
static class ViewHolder {
public ImageView imageview;
public TextView tvName;
public TextView tvDescription;
public TextView tvDOB;
public TextView tvCountry;
public TextView tvHeight;
public TextView tvSpouse;
public TextView tvChildren;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
Used the Android Universal Image-Loader This is best
Declare
private ImageLoader imageLoader1;
On Create
imageLoader1 = ImageLoader.getInstance();
imageLoader1.init(ImageLoaderConfiguration.createDefault(getActivity()));
no_image here a drawable image without any image load in Cache
DisplayImageOptions
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageOnLoading(R.drawable.no_image) // resource or drawable
.showImageForEmptyUri(R.drawable.no_image) // resource or drawable
.showImageOnFail(R.drawable.no_image)
.considerExifParams(true)
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
imageLoader1.displayImage(yourpath.replace(" ", "%20"), ivprofile, options);
You Can Use Piccaso Also and Glide also
here is the code after editing it works like a charm
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
public class ActorAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Actors> movieItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public ActorAdapter(Activity activity, List<Actors> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
#Override
public int getCount() {
return movieItems.size();
}
#Override
public Object getItem(int location) {
return movieItems.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_item, null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.flag);
TextView title = (TextView) convertView.findViewById(R.id.rank);
// getting movie data for the row
Actors m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getImage(), imageLoader);
// title
title.setText(m.getName());
return convertView;
}
}
I am trying to display images in a listview with AsyncTask but I am getting an error. Saying...
Attempt to invoke virtual method 'java.lang.String
java.lang.Object.toString()' on a null object reference
This is error is happening on
public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString();
}
And
LoadImage loadImage = new LoadImage(imageView);
Bitmap bitmap = loadImage.doInBackground();
Here is LoadImage.java
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Environment;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
/**
* Created by Yudii on 27/05/2016.
*/
class LoadImage extends AsyncTask<Object, Void, Bitmap>{
private ImageView imv;
private String path;
public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString();
}
#Override
protected Bitmap doInBackground(Object... params) {
Bitmap bitmap = null;
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + path);
if (file.exists()) {
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
}
return bitmap;
}
#Override
protected void onPostExecute(Bitmap result) {
if (!imv.getTag().toString().equals(path)) {
/* The path is not same. This means that this
image view is handled by some other async task.
We don't do anything and return. */
// return;
}
if(result != null && imv != null){
imv.setVisibility(View.VISIBLE);
imv.setImageBitmap(result);
}else{
imv.setVisibility(View.GONE);
}
}
}
And here is CustomList.java
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomList extends ArrayAdapter<String>{
private final Activity context;
private final Integer[] imageId;
public CustomList(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.list_single, web);
this.context = context;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, null, true);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
LoadImage loadImage = new LoadImage(imageView);
Bitmap bitmap = loadImage.doInBackground();
imageView.setImageResource(imageId[position]);
return rowView;
}
}
And here is MainActivity.java
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView list;
String[] web = {
"Google Plus",
"Twitter",
"Tall",
};
Integer[] imageId = {
R.drawable.gggggggggg,
R.drawable.hhhhhhhhhh,
R.drawable.ttttttttttt
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new
CustomList(MainActivity.this, web, imageId);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
}
});
}
}
You need to change loadImage.execute(); instead of Bitmap bitmap = loadImage.doInBackground();
Why do you need AsyncTask? Try to use library Picasso and open images by one string:
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + path);
Picasso.with(context).load(file).into(imageView);
Bitmap objects are very weight and you need to clean memory after each Bitmap image for prevent OutOfMemoryError. I recommend you to use libraries.
Add this in your dependencies :-
compile 'com.squareup.picasso:picasso:2.5.2'
and then load your image :-
Picasso.with(this)
.load(drawerImage) // ImagePath
.placeholder( R.mipmap.download)
.error( R.mipmap.add_photo)
.into(imageDrawerView); //ImageView
1,Reason:
As I see, in your getView function:
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
// You need to set tag to your image view
// Ex: imageView.setTag("your_image_file_path") below this line
your imageView has no tag so the below code will get NullPointerException.
public LoadImage(ImageView imv) {
this.imv = imv;
this.path = imv.getTag().toString(); // Crashed here.
}
Also, I see in your getView function has this code:
imageView.setImageResource(imageId[position]);
That means "your ImageView will set resource corresponding to defined imageId array". So you no need AsyncTask class, let remove it and change your getView like below:
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_single, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
// Remove below code
// LoadImage loadImage = new LoadImage(imageView);
// Bitmap bitmap = loadImage.doInBackground();
imageView.setImageResource(imageId[position]);
return rowView;
}
Your Asynctask class is needed whenever you use path of image file, not drawable resources.
2, Improvement:
You can learn more about ViewHolder pattern when using ListView, RecyclerView to improve performance, from Google Search. Below is an example with your current code:
#Override
public View getView(int position, View view, ViewGroup parent) {
View holder = null;
if (view == null) {
holder = new ViewHolder();
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.list_single, parent, false);
holder.imageView = (ImageView) view.findViewById(R.id.img);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.imageView.setImageResource(imageId[position]);
return view;
}
static class ViewHolder { // should be static class
ImageView imageView;
}
I am a beginner programmer so please bear with me. I have been able to create a listview with images in it and thought I could modify it to add a checkbox. However, this setup does not work. Any suggestions to how I may fix my code to create a listview with both an image and checkbox?
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class CheckboxList extends ListActivity{
private LayoutInflater mInflater;
private Vector<RowData> data;
RowData rd;
static CheckBox box0,box1,box2,box3;
static final String[] title = new String[] {
"Box 1", "Box 2",
"Box 3 ","Box 4" };
static final CheckBox[] boxes= new CheckBox[] {box0,box1,box2,box3};
private Integer[] imgid = {
R.drawable.magnum,R.drawable.mg,R.drawable.mk,
R.drawable.mk46};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mInflater = (LayoutInflater) getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
data = new Vector<RowData>();
for(int i=0;i<title.length;i++){
try {
rd = new RowData(i,title[i],boxes[i]);
} catch (ParseException e) {
e.printStackTrace();
}
data.add(rd);
}
CustomAdapter adapter = new CustomAdapter(this, R.layout.check_list,
R.id.title, data);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
public void onListItemClick(ListView parent, View v, int position,
long id) {
Toast.makeText(getApplicationContext(), "You have selected "
+(position+1)+"th item", Toast.LENGTH_SHORT).show();
}
private class RowData {
protected int mId;
protected String mTitle;
protected CheckBox mCBox;
RowData(int id,String title,CheckBox cbox){
mId=id;
mTitle = title;
mCBox=cbox;
}
#Override
public String toString() {
return mId+" "+mTitle+" "+mCBox;
}
}
private class CustomAdapter extends ArrayAdapter<RowData> {
public CustomAdapter(Context context, int resource,
int textViewResourceId, List<RowData> objects) {
super(context, resource, textViewResourceId, objects);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
TextView title = null;
CheckBox box = null;
ImageView i11=null;
RowData rowData= getItem(position);
if(null == convertView){
convertView = mInflater.inflate(R.layout.check_list, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
title = holder.gettitle();
title.setText(rowData.mTitle);
box = holder.getcheckbox();
box.setChecked(false);
i11=holder.getImage();
i11.setImageResource(imgid[rowData.mId]);
return convertView;
}
private class ViewHolder {
private View mRow;
private TextView title = null;
private CheckBox cbox = null;
private ImageView i11=null;
public ViewHolder(View row) {
mRow = row;
}
public TextView gettitle() {
if(null == title){
title = (TextView) mRow.findViewById(R.id.title);
}
return title;
}
public CheckBox getcheckbox() {
if(null == cbox){
cbox = (CheckBox) mRow.findViewById(R.id.checkBox);
}
return cbox;
}
public ImageView getImage() {
if(null == i11){
i11 = (ImageView) mRow.findViewById(R.id.img);
}
return i11;
}
}
} }
Look at this links.it may helpfull......
http://www.vogella.com/articles/AndroidListView/article.html
CustomListview With Images and Checkboxes Example
I want to let the adapter check if the submitted value is a file
but when I check
it only checks the first value so
for eg.
I submit
app (directory)
meta (directory)
this shoul look like this :
app with the directory icon
meta with the directory icon
but the out put is only app as directory
The adapter:
package org.alexander.fuchs.compress;
import java.io.File;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class adapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
static class ViewHolder {
public TextView text;
public ImageView image;
}
public adapter(Activity context, String[] names) {
super(context, R.layout.rowlayout, names);
this.context = context;
this.names = names;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.rowlayout, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.label);
viewHolder.image = (ImageView) rowView
.findViewById(R.id.icon);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
String s = names[position];
holder.text.setText(s);
File entry = new File(s);
if(entry.isDirectory() == true)
{
holder.image.setImageResource(R.drawable.ok);
}
else
{
holder.image.setImageResource(R.drawable.no);
}
return rowView;
}
}