Listview with many images isn't working - android

I'm creating an activity which will consist of a listview. Each element of the listview will contain one image and one text. When I'm running the app for example with just 10 elements it works just fine. But when I have 90 elements, my app stops working. Have you got any solutions to this problem?
PS. My app will not use internet.
My 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=".Testlistview" >
<ListView
android:id="#+id/list66"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:fastScrollEnabled="true"
android:persistentDrawingCache="scrolling"
android:scrollingCache="false">
</ListView>
</RelativeLayout>
My adapter:
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 customadaptertest extends ArrayAdapter<String> {
private final Activity context;
private final String[] web;
private final Integer[] imageId;
public customadaptertest(Activity context,
String[] web, Integer[] imageId) {
super(context, R.layout.listtestitem, web);
this.context = context;
this.web = web;
this.imageId = imageId;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.listtestitem, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.txt);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img);
txtTitle.setText(web[position]);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
My activity:
import android.content.res.TypedArray;
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;
import java.util.ArrayList;
import java.util.List;
public class Testlistview extends AppCompatActivity {
ListView list66;
String[] web = {
"some text"
} ;
Integer[] imageId = {
R.drawable.some_item_image
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testlistview);
customadaptertest adapter = new
customadaptertest(Testlistview.this, web, imageId);
list66=(ListView)findViewById(R.id.list66);
list66.setAdapter(adapter);
}
}
My item:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="#+id/img"
android:layout_width="50dp"
android:layout_height="50dp"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="50dp" />
</TableRow>
</TableLayout>

You should use ViewHolder pattern.
Take a look at this : Making ListView Scrolling Smooth.

Related

transform() or override() or centerCrop() not working with glide Android

This app currently is working perfectly fetches and displays images but when I try to add additional methods to glide in the ImageListAdapter class like override() or centerCrop() or transform() or placeholder(), it doesn't work. I mean it doesn't even let me add those, it shows in red and no similar suggestion but many tutorials suggest to use those methods to modify size, why does it not work. Am I doing something wrong?
MainActivity.java
package com.example.imnobody.photosearch;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.GridView;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] eatFoodyImages = {
"http://i.imgur.com/rFLNqWI.jpg",
"http://i.imgur.com/C9pBVt7.jpg",
"http://i.imgur.com/rT5vXE1.jpg",
"http://i.imgur.com/aIy5R2k.jpg",
"http://i.imgur.com/MoJs9pT.jpg",
"http://i.imgur.com/S963yEM.jpg",
"http://i.imgur.com/rLR2cyc.jpg",
"http://i.imgur.com/SEPdUIx.jpg",
"http://i.imgur.com/aC9OjaM.jpg",
"http://i.imgur.com/76Jfv9b.jpg",
"http://i.imgur.com/fUX7EIB.jpg",
"http://i.imgur.com/syELajx.jpg",
"http://i.imgur.com/COzBnru.jpg",
"http://i.imgur.com/Z3QjilA.jpg",
};
GridView gridView = (GridView) findViewById(R.id.gridview);
ImageListAdapter imageListAdapter = new ImageListAdapter(MainActivity.this,eatFoodyImages);
gridView.setAdapter(imageListAdapter);
}
}
ImageListAdapter.java
package com.example.imnobody.photosearch;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
/**
* Created by imnobody on 5/8/17.
*/
public class ImageListAdapter extends ArrayAdapter{
private Context context;
private LayoutInflater inflater;
private String[] imageUrls;
public ImageListAdapter(Context context, String[] imageUrls) {
super(context, R.layout.image_main, imageUrls);
this.context = context;
this.imageUrls = imageUrls;
inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (null == convertView) {
convertView = inflater.inflate(R.layout.image_main, parent, false);
}
Glide
.with(context)
.load(imageUrls[position])
.into((ImageView) convertView);
return convertView;
}
}
activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.imnobody.photosearch.MainActivity">
<GridView
android:layout_width="368dp"
android:layout_height="495dp"
android:id="#+id/gridview"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="4dp"
android:verticalSpacing="4dp"
android:gravity="center"
android:stretchMode="columnWidth"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp">
</GridView>
</android.support.constraint.ConstraintLayout>
image_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/imagemain"/>

Error in ImageButton

I'm errors and do not know how to fix them
MainActivity
setListAdapter(new ConteudoActivity(ctx, R.layout.suport_activity,
option_icon, option_text));
ConteudoActivity
ImageButtonib = (ImageButton)convertView.findViewById(R.id.image_adicionar);
ib.setImageDrawable(mButton.getDrawable(position));
Activity Array
package com.kolla.projeto;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class ConteudoActivity extends ArrayAdapter < String > {
private LayoutInflater mInflater;
private ImageButton mButton;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public ConteudoActivity(Context ctx, int viewResourceId,
String[] strings, TypedArray icons, TypedArray ibutton) {
super(ctx, viewResourceId, strings);
mInflater = (LayoutInflater) ctx.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mButton = ibutton;
mViewResourceId = viewResourceId;
}
#
Override
public int getCount() {
return mStrings.length;
}
#
Override
public String getItem(int position) {
return mStrings[position];
}
#
Override
public long getItemId(int position) {
return 0;
}
#
Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView) convertView.findViewById(R.id.option_icon);
iv.setImageDrawable(mIcons.getDrawable(position));
ImageButton ib = (ImageButton) convertView.findViewById(R.id.image_adicionar);
ib.setImageDrawable(ImageButton.getDrawable(position));
TextView tv = (TextView) convertView.findViewById(R.id.option_text);
tv.setText(mStrings[position]);
return convertView;
}
}
MainActivity
package com.kolla.projeto;
import android.app.ListActivity;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Bundle;
public class MainActivity extends ListActivity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context ctx = getApplicationContext();
Resources res = ctx.getResources();
String[] option_icon = res.getStringArray(R.array.Logo);
TypedArray option_text = res.obtainTypedArray(R.array.NomeProd);
setListAdapter(new ConteudoActivity(ctx, R.layout.suport_activity,
option_icon, option_text));
}
}
ListView
<?xml version="1.0" encoding="utf-8"?>
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.kolla.projeto.MainActivity">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</RelativeLayout>
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<ImageView
android:id="#+id/option_icon"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView
android:id="#+id/option_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16dp"
android:layout_weight="6">
</TextView>
<ImageView
android:id="#+id/image_adicionar"
android:layout_width="50dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
Try this,
image.setImageDrawable(getResources().getDrawable(R.drawable.drawable_name));

Simple Custom ListView Application. Crashes every time

I am a newbie in Android, so I am really sorry if the error that you will encounter is a blunder. I tried making a Custom ListView (An ImageView and a TextView in each row) by using custom_row.xml as the layout file and CustomAdapter.java as the customized adapter.
I do not understand why this application crashes every time I run the same. The exception (As shown in Android Monitor) is caused at this very line in the CustomAdapter.java file.
View customView = myInflater.inflate(R.layout.custom_row, parent, false);
I am attaching all of the Java and Layout codes for the reference. Thanks.
CustomAdapter.java
package com.abhinavankur.listexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ArrayAdapter;
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, String[] teams) {
super(context,R.layout.custom_row,teams);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater myInflater = LayoutInflater.from(getContext());
View customView = myInflater.inflate(R.layout.custom_row, parent, false);
String singleTeam = getItem(position);
TextView myText = (TextView) customView.findViewById(R.id.myText);
ImageView myImage= (ImageView) customView.findViewById(R.id.myImage);
myText.setText(singleTeam);
myImage.setImageResource(R.drawable.mine);
return customView;
}
}
custom_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_width="80dp"
android:layout_height="80dp"
android:id="#+id/myImage"
android:src="#drawable/mine"
android:layout_margin="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/myText"
android:layout_margin="5dp" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.abhinavankur.listexample.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myListView">
</ListView>
</RelativeLayout>
MainActivity.java
package com.abhinavankur.listexample;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String teams[] = {"Real Madrid","Manchester United","Barcelona","Chelsea","Bayern Munich","PSG","Borussia Dortmund","Liverpool","Arsenal","Valencia","Villareal","Leicester City","AS Roma"};
ListAdapter myAdapter = new CustomAdapter(this, teams);
ListView myList = (ListView) findViewById(R.id.myListView);
myList.setAdapter(myAdapter);
Toast.makeText(this,"List Shown",Toast.LENGTH_LONG).show();
myList.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String team = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(MainActivity.this,team,Toast.LENGTH_SHORT).show();
}
}
);
}
}
Change your CustomAdapter as below. You were inflating the layout in a wrong way.
class CustomAdapter extends ArrayAdapter<String> {
Context context;
CustomAdapter(Context context, String[] teams) {
super(context,R.layout.custom_row,teams);
this.context = context
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_row, null);
}
....
....
}

Listview with images not displaying

I have created custom Listview that has to show images as a list but images are not getting displayed.
I have 6 Images with dimensions 400x100 i want them to display as rows in listview.
I cant able to find where exactly i went wrong. Please help me . Thanks in advance.
Main Activity source code.
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.app.Activity;
public class MainActivity extends Activity {
ListView list;
Integer[] imageId = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomList adapter = new
CustomList(MainActivity.this,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) {
}
});
}
}
CustomList adapter
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 CustomList extends ArrayAdapter<ImageView>{
private final Activity context;
private final Integer[] imageId;
public CustomList(Activity context, Integer[] imageId) {
super(context, R.layout.list_single);
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);
imageView.setImageResource(imageId[position]);
return rowView;
}
}
acitivity_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: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" >
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
list_single.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</TableRow>
</TableLayout>
Have you tried to remove the RelativeLayout inside which your ListView is? If there is only the ListView in it it's not useful.
You can also try to set android:layout_width and android:layout_height to match_parent.
Did you also try to add something else in your list item (like a TextView with a mock value), to see if it is displayed?

Restarting application when try to scroll the listview

I am developing the program that used ListActivity. So, when i scroll the listview to down, it cause to restart application and start from first activity. My code is as follows :
package com.daarkoob.food;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Window;
import java.util.ArrayList;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
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 android.widget.Toast;
public class FoodListActivity extends ListActivity {
private ArrayList<Food> M_foods = new ArrayList<FoodListActivity.Food>();
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.foodlist);
SQLiteDatabase sqliteDatabase = SQLiteDatabase.openOrCreateDatabase(
"/data/data/" + this.getPackageName() + "/data.db", null);
Cursor cursor = sqliteDatabase.rawQuery("SELECT * FROM FOOD", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
M_foods.add(new Food(cursor.getString(2), cursor.getString(1)));
cursor.moveToNext();
}
FoodAdapter adapter = new FoodAdapter(this);
setListAdapter(adapter);
cursor.close();
sqliteDatabase.close();
}
public class Food {
public Food(String foodName, String foodType) {
this.M_foodName = foodName;
this.M_foodType = foodType;
}
String M_foodName;
String M_foodType;
}
public class FoodAdapter extends ArrayAdapter<Food> {
private Context M_context = null;
public FoodAdapter(Context context) {
super(context, R.layout.foodlist_view_item, M_foods);
this.M_context = context;
}
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) M_context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(
R.layout.foodlist_view_item, parent, false);
}
TextView textView = (TextView) convertView
.findViewById(R.id.foodlist_view_item_textview);
textView.setText(M_foods.get(position).M_foodName);
return convertView;
}
}
}
And the foodlist_view_item.xml is :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/border"
android:orientation="vertical" >
<TextView
android:id="#+id/foodlist_view_item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="right|center_vertical"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#android:color/black"
android:textStyle="bold" />
</RelativeLayout>
and the foodlist.xml is as follows :
<?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:background="#drawable/background"
android:orientation="vertical" >
<TextView
android:id="#+id/foodlist_title_bar"
android:layout_width="match_parent"
android:layout_height="40dip"
android:background="#drawable/textlines"
android:gravity="right|center_vertical"
android:text="#string/foodlist_titlebar_text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:dividerHeight="1dip" >
</ListView>
</LinearLayout>
What thing is wrong ? Why restarting occurs ?
Thanks in advance :)

Categories

Resources