Image not showing in picasso because gridview [duplicate] - android

This question already has answers here:
Custom Adapter getView() method is not called
(6 answers)
Closed 7 years ago.
the image is not showing , the app is launching but no image is dispalying , can you help me to fix that ? I guess problem with gridimage. plesae not that the place holder of picasso is not showing in the list view.
public class GridViewAdapter extends ArrayAdapter<GridImages> {
private Context mContext;
private int layoutResourceId;
private ArrayList<GridImages> mGridImages = new ArrayList<GridImages>();
public GridViewAdapter(Context mContext, int layoutResourceId, ArrayList<GridImages> mGridImages) {
super(mContext, layoutResourceId, mGridImages);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.mGridImages = mGridImages;
}
/**
* Updates grid data and refresh grid items.
* #param mGridData
*/
public void setGridData(ArrayList<GridImages> mGridImages) {
this.mGridImages = mGridImages;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null){
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
holder.imageView = (ImageView)convertView.findViewById(R.id.grid_item_image);
holder.titleTextView = (TextView)convertView.findViewById(R.id.grid_item_title);
convertView.setTag(holder);
}
else{
holder = (ViewHolder) convertView.getTag();
}
String url = "http://www.justedhak.comlu.com/images/uploaded_images.jpg";
Picasso.with(mContext)
.load(url)
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_launcher)
.noFade().resize(150,150)
.centerCrop()
.into(holder.imageView);
return convertView;
}
static class ViewHolder {
TextView titleTextView;
ImageView imageView;
}
}
this is main activity.
public class MainActivity extends ActionBarActivity {
GridViewAdapter mGridAdapter;
private ArrayList<GridImages> mGridImages;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list1);
mGridImages = new ArrayList<>();
mGridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, mGridImages);
listView.setAdapter(mGridAdapter);
}
activity main
<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="com.example.justedhak.MainActivity" >
<ListView
android:id="#+id/list1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:focusable="true"
android:clickable="true"/>
</RelativeLayout>
grid item layout
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="500dp"
android:layout_height="500dp"
/>
<TextView
android:id="#+id/grid_item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:maxLines="2"
android:layout_below="#+id/grid_item_image"
android:ellipsize="marquee"
android:textSize="12sp" />
</RelativeLayout>

Check your layout once.
try to put the specific height for image view and see.
If you didn't succeed add you layout code.

Related

ListView very slow when scrolling

I really need help, Listview which I used in my app is very slow while scrolling how can I fix it ? I reduce the images, use viewholder but nothing change..
I am beginner in android : )
also get this message : I/Choreographer: Skipped 32 frames! The application may be doing too much work on its main thread.
Here is my code
public class asker extends Activity {
ListView list;
String[] askerbaslik;
int[] images =
{R.drawable.barbar_icon, R.drawable.okcu_icon,
R.drawable.dev_icon,
R.drawable.goblin_icon,
R.drawable.duvar_kirici_icon, R.drawable.balon_icon,
R.drawable.buyucu_icon, R.drawable.sifaci_icon,
R.drawable.ejderha_icon,
R.drawable.pekka_icon, R.drawable.yavruejder_icon, R.drawable.madenci_icon};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
Resources res = getResources();
askerbaslik = res.getStringArray(R.array.asker_adlari);
list = (ListView) findViewById(R.id.listview);
ilyasadapter adapter = new ilyasadapter(this, askerbaslik, images);
list.setAdapter(adapter);
}
class ilyasadapter extends ArrayAdapter<String> {
Context context;
int[] images;
String[] titlearray;
ilyasadapter(Context c, String[] titles, int imgs[]) {
super(c, R.layout.liste_tasarim, R.id.asker_baslik, titles);
this.context = c;
this.images = imgs;
this.titlearray = titles;
}
class MyViewHolder {
TextView mytitle;
ImageView myimage;
MyViewHolder(View v) {
mytitle = (TextView) v.findViewById(R.id.asker_baslik);
myimage = (ImageView) v.findViewById(R.id.asker_icon);
}
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyViewHolder holder = null;
if (row == null) {
LayoutInflater inflater = (LayoutInflater)
context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.liste_tasarim, parent, false);
holder = new MyViewHolder(row);
row.setTag(holder);
Log.d("test", "creating a new row");
} else {
holder = (MyViewHolder) row.getTag();
Log.d("test", "Recycling stuff");
}
holder.myimage.setImageResource(images[position]);
holder.mytitle.setText(titlearray[position]);
return row;
}
}
}
Single row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_marginTop="5dp"
android:id="#+id/asker_icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/barbar_icon"
android:background="#drawable/rounded_corner"
></ImageView>
<TextView
android:id="#+id/asker_baslik"
android:layout_marginTop="40dp"
android:layout_marginLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Asker ismi"
android:textColor="#ffffff"
android:textSize="15dp"
android:fontFamily="sans-serif"
/>
</LinearLayout>
my listview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#455A64" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#263238"
android:dividerHeight="2px"
/>
</RelativeLayout>
Please try to remove the icons from the listview and see if that improves performance. You may be using images with too high of a resolution. I too have noticed performance anomalies when using high-resolution images.
Let me know if that helps. If not, there are a few other things that we can try.

Make imageView in gridView clickable

I have this gridView with imageView items, but i can't make it clickable. I'm kind of new in android and i don't understand how to make theses images clickable. I tried some answers that I have read in other posts but it doesn't work.
Help...
Another question! If i want to pass a hard coded String to another activity, but this string changes every time I click on an imageView item. Can I declare a static string and then I say string="word" and then call it from the other activity with Activity.string? Would i have the value in the other activity?
Please find below my xml files, gridViewAdapter and activity!
services_list_activity.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=".ServicesListActivity">
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="4dp"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</RelativeLayout>
gris_row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/item_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:src="#drawable/atm">
</ImageView>
<TextView
android:id="#+id/item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textSize="15sp" >
</TextView>
</LinearLayout>
CustomGridViewAdapter.java
public class CustomGridViewAdapter extends ArrayAdapter<Item> {
Context context;
int layoutResourceId;
ArrayList<Item> data = new ArrayList<Item>();
public CustomGridViewAdapter(Context context, int layoutResourceId,
ArrayList<Item> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.txtTitle = (TextView) row.findViewById(R.id.item_text);
holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
row.setTag(holder);
} else {
holder = (RecordHolder) row.getTag();
}
Item item = data.get(position);
holder.txtTitle.setText(item.getTitle());
holder.imageItem.setImageBitmap(item.getImage());
return row;
}
static class RecordHolder {
TextView txtTitle;
ImageView imageItem;
}
}
ServicesListActivity.java
public class ServicesListActivity extends Activity implements AdapterView.OnItemClickListener{
protected static final Toast text = null;
//String to pass to another activity
public static String types;
GridView gridView;
ArrayList<Item> gridArray = new ArrayList<Item>();
CustomGridViewAdapter customGridAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.services_list_activity);
//set grid view item
Bitmap atmIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.atm);
Bitmap bankIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.bank);
Bitmap barIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.bar);
Bitmap cafeIcon = BitmapFactory.decodeResource(this.getResources(), R.drawable.cafe);
gridArray.add(new Item(atmIcon,"ATMs"));
gridArray.add(new Item(bankIcon,"Banks"));
gridArray.add(new Item(barIcon,"Bars"));
gridArray.add(new Item(cafeIcon,"Cafés"));
gridView = (GridView) findViewById(R.id.gridView1);
customGridAdapter = new CustomGridViewAdapter(this, R.layout.grid_row, gridArray);
gridView.setAdapter(customGridAdapter);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//intent
Intent data = new Intent(getApplicationContext(), ListActivity.class);
switch (position) {
case 0:
//String to send to another activity
types= "restaurant";
startActivity(data);
break;
case 1:
startActivity(data);
break;
case 2:
startActivity(data);
break;
case 3:
startActivity(data);
break;
default:
break;
}
}
}
Add this to your ImageView:
android:clickable="true"
With feedback:
android:background="?attr/selectableItemBackground"
Then set an OnClickListener in your Activity:
yourImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});

How to set OnItemClickListener on custom list view

I have a custom list view that fill with an image and text view in each row, and it shown correct :[1
but my problem is that I want to show the text that in a Toast in setOnItemClickListener. here is my codes:
public class Showing {
public int icon;
public String title;
public Showing(){
super();
}
public Showing(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}}
and my adapter is:
public class ShowingAdapter extends ArrayAdapter<Showing> {
Context context;
int layoutResourceId;
Showing data[] = null;
public ShowingAdapter(Context context, int layoutResourceId, Showing[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ShowingHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ShowingHolder();
holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
holder = (ShowingHolder)row.getTag();
}
Showing showing = data[position];
holder.txtTitle.setText(showing.title);
holder.imgIcon.setImageResource(showing.icon);
return row;
}
static class ShowingHolder
{
ImageView imgIcon;
TextView txtTitle;
}}
And here are my layouts for Header:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/txtHeader"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#FFFFFF"
android:padding="10dp"
android:text="Action Photos"
android:background="#336699" />
</LinearLayout>
And each row
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageView android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_marginRight="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_gravity="left" />
<TextView android:id="#+id/txtTitle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:textStyle="bold"
android:textSize="22dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" />
</LinearLayout>
So here is My Main code :
public class MainActivity extends Activity {
private ListView listView1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Showing action_data[] = new Showing[]
{
new Showing(R.drawable.cut, "Cut"),
new Showing(R.drawable.delete, "Delete"),
new Showing(R.drawable.favorites, "Favorites"),
new Showing(R.drawable.redo, "Redo"),
new Showing(R.drawable.up, "Up")
};
ShowingAdapter adapter = new ShowingAdapter(this,R.layout.listview_item_row, action_data);
listView1 = (ListView) findViewById(R.id.listView1);
View header = (View) getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
***// I want to make a toast here...
// but the 'VIEW' is LinearLayout and
// I want to retrive the 'TEXT VIEW.getText'***
}
});
}}
You don't need to act on the layout. You can simply retrieve the object associated with your row. So inside the onItemClick() you can get it in this way:
Showing showing = (Showing)adapter.getitem(position)
now, showing contains all the information you need. So you can get the text:
showing.title
or if you make title private (as it supposed to be), you would use a getter:
showing.getTitle();

How to implement list view with two parallel imageview in a row

Well I do not know what should be the proper title for the question , but my problem is quite amazing, do not know is it possible or not. Here is my question
I have series of images and I want to set them in a ListView. following is a ListView row Xml.(named anim_list_row)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="100"
>
<ImageView
android:layout_width="0dp"
android:layout_height="350dp"
android:scaleType="fitXY"
android:id="#+id/iv_dummy_left"
android:layout_marginLeft="5dp"
android:layout_weight="50"/>
<ImageView
android:layout_width="0dp"
android:layout_height="350dp"
android:scaleType="fitXY"
android:id="#+id/iv_dummy_right"
android:layout_weight="50"
android:layout_marginRight="5dp"/>
</LinearLayout>
in this you can see that I want to set the 2 different images in two different let say left and right ImageView. Following is my adapter class
public class MListAdapter extends BaseAdapter {
private Context context;
private ArrayList<AnimListItems> mAnimListItems;
public MListAdapter(Context context, ArrayList<AnimListItems> mAnimListItems){
this.context = context;
this.mAnimListItems = mAnimListItems;
}
#Override
public int getCount() {
return mAnimListItems.size();
}
#Override
public Object getItem(int position) {
return navDrawerItems.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.anim_list_row, null);
}
ImageView imgIconLeft = (ImageView) convertView.findViewById(R.id.iv_dummy_left);
ImageView imgIconRight = (ImageView) convertView.findViewById(R.id.iv_dummy_right);
//TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
imgIconLeft.setImageResource(navDrawerItems.get(position).getIcon());
//if I do not do +1 here it sets same image to both left and right ImageView
imgIconRight.setImageResource(navDrawerItems.get(position+1).getIcon());
// txtTitle.setText(navDrawerItems.get(position).getTitle());
return convertView;
}
}
so here is problem this list view is working but it is assigning the same images to both ImageView in the row. and if I do +1 as following
imgIconRight.setImageResource(navDrawerItems.get(position+1).getIcon())
then it helps in changing the image view at right side in the row, but the 2nd row repeat the image in its first imageview (I mean the image in the left ImageView of the 2nd row is same as the image in right ImageView of first row.)
So what is a solution of
Repeating images in a rows.
And How can I get each ImageView id and its resourceid of the image so that I can come to know which image has been clicked. And then I can able to set that image into another activity's ImageView. I mean I wanted to know which image has been clicked by the user , so I want to set same image in the ImageView of other activity.
Hello If you want to user one single array then i have made one demo example for your problem quickly i think it will help you.
MainActivity.java
public class MainActivity extends ActionBarActivity {
Item item;
ArrayList<Object> obj = new ArrayList<Object>();
MListAdapter adapter;
ListView lv;
int[] arrEnabledImageIds = new int[] { R.drawable.ic_launcher,
R.drawable.ic_delete_icon, R.drawable.ic_delete_icon,
R.drawable.ic_launcher, R.drawable.ic_delete_icon,
R.drawable.ic_launcher };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
for (int i = 0; i < arrEnabledImageIds.length; i++) {
Item itemInfo = new Item();
itemInfo.image1 = arrEnabledImageIds[i];
i++;
itemInfo.image2 = arrEnabledImageIds[i];
obj.add(itemInfo);
}
adapter = new MListAdapter(this, R.layout.row, obj);
lv.setAdapter(adapter);
}
}
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"
>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Item.java
public class Item implements Serializable {
public static final long serialVersionUID = 1L;
public int image1;
public int image2;
}
row.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/ivLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/ivRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
MListAdapter.java
public class MListAdapter extends ArrayAdapter<Object> {
private Context context;
int resId;
private ArrayList<Object> mAnimListItems;
public MListAdapter(Context context, int textViewResourceId,
ArrayList<Object> mAnimListItems) {
super(context, textViewResourceId, mAnimListItems);
this.resId = textViewResourceId;
this.context = context;
this.mAnimListItems = mAnimListItems;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(resId, null);
}
ImageView imgIconLeft = (ImageView) convertView
.findViewById(R.id.ivLeft);
ImageView imgIconRight = (ImageView) convertView
.findViewById(R.id.ivRight);
// TextView txtTitle = (TextView) convertView.findViewById(R.id.title);
Item item = (Item) mAnimListItems.get(position);
imgIconLeft.setImageResource(item.image1);
// if I do not do +1 here it sets same image to both left and right
// ImageView
imgIconRight.setImageResource(item.image2);
// txtTitle.setText(navDrawerItems.get(position).getTitle());
return convertView;
}
}
try to use two different array/arraylist to store left and rightside images seprately.
settag as l+position for left image
settag as r+position for right image
you can identify the clicked image by
imgIconLeft.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(v.getTag.toString().contains("l"){
// then it's a left button.
String position=v.getTag.toString().subString(1,v.getTag.toString().length()-1);
// to get position
}
});

List image is empty

i'm having some trouble with my code app: i'm trying to display in an activity a list of image taken from an array but when i try to execute the app i've an empty list without error in debug. Can anyone help me? Here is my activity:
String [] vett;
ListView list;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_techpuno);
list=(ListView)findViewById(android.R.id.list);
Intent intent = getIntent();
vett=intent.getExtras().getStringArray("nomeImmagine");
for(int i = 0;i<vett.length;i++){
Log.d("test", vett[i]);}
//list.setListAdapter(new AdapterTecniche(this, vett));
list.setAdapter(new AdapterTecniche(this, R.layout.activity_techp,vett));
}
And here it is the adapter:
public class AdapterTecniche extends ArrayAdapter<String> {
private final int resource;
private final String[] img;
public ImageLoader imageLoader;
LayoutInflater inflater;
public AdapterTecniche(Context context,int resource, String[]img) {
// your code
super(context, R.layout.activity_techpuno);
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.context = context;
this.resource=resource;
this.img=img;
imageLoader=new ImageLoader(getContext().getApplicationContext());
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
View rowView = convertView;
if(rowView == null) {
Log.d("errore","Immagini non presenti");
rowView = inflater.inflate(R.layout.immagini, parent, false);
viewHolder=new ViewHolder();
viewHolder.imageView=(ImageView) rowView.findViewById(R.id.list_image);
rowView.setTag(viewHolder);
}
else
{
viewHolder=(ViewHolder)rowView.getTag();
}
if(img==null){
Log.d("errore","Immagini non presenti");
}else{
int resourceId = context.getResources().getIdentifier(img[position],"drawable", context.getPackageName());
for(int i=0;i<img.length;i++){
Log.d("err", img[i]);
}
imageLoader.DisplayImage(resourceId, viewHolder.imageView);
}
return rowView;
}
static class ViewHolder {
ImageView imageView;
}
}
Immagini.xml (each image layout)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/list_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_gravity="center_horizontal" />
</LinearLayout>
And Activity layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/list" />
</LinearLayout>
setContentView(R.layout.activity_techpuno);
should be before
list=(ListView)findViewById(R.id.listView);
Because Activity firstly need to have binded/inflated layout, after that you can "find" your views from it.
Edit
Use ArrayAdapter(Context context, int resource, T[] objects) constructor to give objects to it.
Or override getCount() because your adapter have no idea how many items he got.

Categories

Resources