Android custom Listadapter - java

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;
}
}

Related

Trying to Create a listView image with text in Android Studio

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);

Adding Drawables from assets into ListView

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;
}

caching images from json in listview?

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;
}
}

Android and picasso loading listview

I m actually trying to load different images with Picasso(http://square.github.io/picasso/) in a listview using a simple code and it doesnt work.
In fact, I need to load remote pictures, and display them in a simple listview, without using fragments (because I dont need it at all)
I dont know what's going wrong... Can you help ?
This is my adapter code :
package id2tel.surikatsante.view.adapter;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.koushikdutta.ion.Ion;
import com.squareup.picasso.Picasso;
import java.util.List;
import id2tel.surikatsante.R;
import id2tel.surikatsante.view.rowItem.AnomalieRowItem;
/**
* Cette classe herite de BaseAdapter
*
* #author Vincent Danti
* #version 4.0
* #since 1.0
*/
public class AnomalieAdapter extends BaseAdapter {
Context context;
List<AnomalieRowItem> rowItems;
public AnomalieAdapter(Context context, List<AnomalieRowItem> items) {
this.context = context;
this.rowItems = items;
}
private class ViewHolder {
TextView txtTitre;
ImageView img;
ImageView imgCheck;
LinearLayout layout;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater
.inflate(R.layout.item_anomalies, null);
holder = new ViewHolder();
holder.txtTitre = (TextView) convertView
.findViewById(R.id.titre);
holder.layout = (LinearLayout) convertView.findViewById(R.id.item);
holder.img = (ImageView) convertView
.findViewById(R.id.img);
holder.imgCheck = (ImageView) convertView.findViewById(R.id.check);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final AnomalieRowItem rowItem = (AnomalieRowItem) getItem(position);
holder.txtTitre.setText(rowItem.getLibelle());
if (rowItem.getM_Anomalie().getM_icon() != null) {
Picasso.with(context).load(rowItem.getM_Anomalie().getM_icon()).placeholder(R.drawable.waiting).into(holder.img);
} else {
holder.img.setBackgroundColor(0xffED695A);
}
if (rowItem.isChecked()) {
holder.layout.setBackgroundColor(0xffdddddd);
holder.imgCheck.setVisibility(View.VISIBLE);
} else {
holder.layout.setBackgroundColor(0xffefefef);
holder.imgCheck.setVisibility(View.GONE);
}
return convertView;
}
#Override
public int getCount() {
return rowItems.size();
}
/**
* #param position est une position gps (hérite de toto)
* #return object
*/
#Override
public Object getItem(int position) {
return rowItems.get(position);
}
#Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}
I always pass in the .error() method of Picasso....
thanks for advance

Load selected fields from ArrayList to ListView.

I would like to load only selected fields from my ArrayList to ListView.
I couldn't find that example so I ask.
I have an ArrayList of a structure as follows:
ArrayList<LogInfo> logInfoArray
Where LogInfo class have fields as follows:
public ArrayList<Point[][]> strokes;
public LinkedList<byte[]> codes;
public int[] times; //contains fields of calendar class
I want to put in my ListView in each row selected fields from "times" and "codes"
How can I achieve that? I would like to use a cursor, if possible.
You can use a custom adapter extending from ArrayAdapter to which you can pass the ArrayList<LogInfo> .
You can then ovverride the getView(..) method of the Adapter to set the fields you want in the row of the Listview .
UPDATE
From this example at Android Custom Adapters
import java.util.List;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class InteractiveArrayAdapter extends ArrayAdapter<LogInfo> {
private final List<LogInfo> list;
private final Activity context;
public InteractiveArrayAdapter(Activity context, List<LogInfo> list) {
super(context, R.layout.rowbuttonlayout, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text1, text2;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text1 = (TextView) view.findViewById(R.id.label1);
viewHolder.text2 = (TextView) view.findViewById(R.id.label2);
view.setTag(viewHolder);
} else {
view = convertView;
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text1.setText(list.get(position).getName1());
holder.text2.setText(list.get(position).getName2());
return view;
}
}

Categories