I have gone through this & this SO questions but unable to figure out why the first item of my list view is not visible. I have checked the size of arraylist and it's correct.
if there is 2 items then it shows only 1.
if there is 1 item then it shows 0.
listview file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/videoListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp" />
</RelativeLayout>
listview item xml file:
<?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="wrap_content"
android:paddingTop="10dp">
<ImageView
android:layout_width="150dp"
android:layout_height="90dp"
android:id="#+id/videoImageView"
android:layout_marginLeft="10dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="35dp"
android:id="#+id/titleTextView"
android:layout_marginRight="10dp"
android:layout_toRightOf="#+id/videoImageView"
android:layout_marginLeft="10dp"
android:textSize="12sp"
android:textColor="#000000"
android:ellipsize="end"
android:maxLines="2"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:id="#+id/timeTextView"
android:layout_alignStart="#+id/titleTextView"
android:layout_alignLeft="#+id/titleTextView"
android:layout_below="#+id/titleTextView"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/downloadImageView"
android:layout_alignStart="#+id/timeTextView"
android:layout_alignLeft="#+id/timeTextView"
android:layout_below="#+id/timeTextView"
android:layout_alignBottom="#+id/videoImageView"
android:src="#drawable/ic_vector_download"
android:layout_marginTop="5dp"/>
<ProgressBar
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/downloadProgressBarView"
android:layout_alignStart="#+id/timeTextView"
android:layout_alignLeft="#+id/timeTextView"
android:layout_below="#+id/timeTextView"
android:layout_alignBottom="#+id/videoImageView"
android:layout_marginTop="5dp"
android:visibility="gone"/>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="#+id/shareImageView"
android:layout_below="#+id/timeTextView"
android:layout_alignBottom="#+id/videoImageView"
android:layout_toRightOf="#+id/downloadImageView"
android:layout_marginTop="5dp"
android:layout_marginLeft="7dp"
android:src="#drawable/ic_vector_share"/>
</RelativeLayout>
adapter class:
public class OfflineVideoAdapter extends ArrayAdapter<OfflineVideo> {
Context context;
OfflineVideoDBHelper db;
List<OfflineVideo> list;
public OfflineVideoAdapter(Context context, int resource, List<OfflineVideo> list) {
super(context, resource, list);
this.context = context;
this.list = list;
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final OfflineVideoHolder holder;
if (convertView == null) {
holder = new OfflineVideoHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.video_list_item, parent, false);
holder.video = (ImageView)convertView.findViewById(R.id.videoImageView);
holder.title = (TextView)convertView.findViewById(R.id.titleTextView);
holder.publishedAt = (TextView)convertView.findViewById(R.id.timeTextView);
holder.delete = (ImageView)convertView.findViewById(R.id.downloadImageView);
holder.share = (ImageView)convertView.findViewById(R.id.shareImageView);
convertView.setTag(holder);
} else {
holder = (OfflineVideoHolder)convertView.getTag();
}
final OfflineVideo offlineVideo = getItem(position);
Picasso.with(context).load(offlineVideo.imageURL).into(holder.video);
holder.title.setText(offlineVideo.title);
holder.publishedAt.setText(CommonUtils.calculateTime(offlineVideo.publishedAt));
holder.delete.setImageResource(R.drawable.ic_vector_delete);
return convertView;
}
}
Change the ListView attribute layout_width and layout_height to Fill_Parent
it worked for me.
I was having the same issue because my ListView layout_width and layout_height was "match_constraint" and the toolbar was overlapping the first item. Just adjust the height of the ListView. It works for me.
Related
I have made a custom ArrayAdapter and tried to add the another TextView "Breed" of the animals, but when i execute the program , its not showing the Breed. Where am i doing wrong ? I am scratching my head since long. please help where is the mistake ?
MainActivity.Java
public class MainActivity extends AppCompatActivity {
ListView simpleList;
ArrayList<Item> animalList=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = (ListView) findViewById(R.id.simpleListView);
animalList.add(new Item("Lion",R.drawable.lion,"Khatarnak"));
animalList.add(new Item("Tiger",R.drawable.tiger,"Fudu"));
animalList.add(new Item("Monkey",R.drawable.monkey,"Lallu"));
animalList.add(new Item("Elephant",R.drawable.elephant,"Jabardast"));
animalList.add(new Item("Dog",R.drawable.dog,"ItemDog"));
animalList.add(new Item("Cat",R.drawable.cat,"MeeMee"));
MyAdapter myAdapter=new MyAdapter(this,R.layout.list_view_items,animalList);
simpleList.setAdapter(myAdapter);
}
}
MyAdapter.Java
public class MyAdapter extends ArrayAdapter<Item> {
ArrayList<Item> animalList = new ArrayList<>();
public MyAdapter(Context context, int textViewResourceId, ArrayList<Item> objects) {
super(context, textViewResourceId, objects);
animalList = objects;
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_items, null);
TextView textView = (TextView) v.findViewById(R.id.textView);
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
TextView breedView = (TextView)v.findViewById(R.id.breed);
textView.setText(animalList.get(position).getAnimalName());
imageView.setImageResource(animalList.get(position).getAnimalImage());
breedView.setText(animalList.get(position).getBreed());
return v;
}
}
Item.Java
public class Item {
String animalName;
int animalImage;
String breedName;
public String getBreed() {
return breedName;
}
public void setBreed(String breed) {
this.breedName = breedName;
}
public Item(String animalName,int animalImage,String breedName)
{
this.animalImage=animalImage;
this.animalName=animalName;
this.breedName = breedName;
}
public String getAnimalName()
{
return animalName;
}
public int getAnimalImage()
{
return animalImage;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="2dp"/>
</RelativeLayout>
list_view_items.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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
This is most likely a problem with the linear layout for each item. The orientation is horizontal, which lays each view one after the other horizontally. The problem is that the text view for the animal type has a width of fill_parent leaving no space for the breed view. If you change it to wrap_content it'll probably work for some of the cases, but might not work with everything.
In any case I think this is a problem with the views' positions and sizes. Try and move them around. Perhaps even a simple change would be placing them vertically:
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
There's perhaps a more efficient way of doing this, but this is just an example. The inner linear layout places one text view on to of the other.
PS: your getCount method is not really doing anything. You can remove it.
replace your layout with my code your issue will resolve.
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
I'm building an app for a news website, I have created a ListView and a layout for each item, which has quite a bit of white space, but on the list view all the white space is removed.
Here's what I mean: http://imgur.com/a/mLuuE
This is an item layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="175dp"
android:gravity="center"
android:layout_weight="0"
android:id="#+id/article_layout"
android:background="#drawable/gradientdemo">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center|bottom"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Titolo dell'articolo"
android:textSize="#dimen/abc_text_size_headline_material"
android:layout_gravity="left"
android:id="#+id/article_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="3/9/16 - Autore"
android:layout_gravity="left"
android:id="#+id/article_info" />
</LinearLayout>
</LinearLayout>
This is my custom adapter:
public class ArticleAdapter extends ArrayAdapter<Article> {
public ArticleAdapter(Context context, int textViewResourceId){
super(context, textViewResourceId);
}
public ArticleAdapter(Context context, int resource, List<Article> items ){
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.article_item, null);
}
Article article = (Article) getItem(position);
TextView title = (TextView) v.findViewById(R.id.article_title);
TextView info = (TextView) v.findViewById(R.id.article_info);
if( title != null ) {
title.setText( article.getTitle() );
}
if( info != null ) {
info.setText( article.getAuthor() );
}
return v;
}
}
Hope it's readable, I'm kind of new to android development.
Your cell's LineaerLayout:
android:layout_height="0dp"
android:layout_weight="1"
change to:
android:layout_height="wrap_content"
you don't need layout_weight
Delete the android:layout_weight="1" and add a correct height, e.g. android:layout_height="60dp":
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center|bottom"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Titolo dell'articolo"
android:textSize="#dimen/abc_text_size_headline_material"
android:layout_gravity="left"
android:id="#+id/article_title" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="3/9/16 - Autore"
android:layout_gravity="left"
android:id="#+id/article_info" />
</LinearLayout>
in the linearLayout use wrapcontent, erase layout_weigth and add weigthsum=1
Then in textViews change height to 0dp and add to each one layout_weight=0.5
I think this will work
I fixed this by changing this line in the adapter
v = vi.inflate(R.layout.article_item, null);
to this one:
v = vi.inflate(R.layout.article_item, parent, false);
I already read tons of similar questions, but I found no solution that fixed my problem, so please do not mark this as duplicate.
I have a ListFragment with a FrameLayout containing a ListView, populated through a custom ArrayListAdapter.
Each row of the list has three elements:
Checkbox, EditText and ImageButton.
My problem is that when I tap on an EditText to change the string it contains the keyboard shows up, but the focus immediately goes away and I'm not able to write.
Please note that I already have added to my Activity in the manifest file
android:windowSoftInputMode="adjustResize"
even the
android:descendantFocusability="afterDescendants"
android:descendantFocusability="beforeDescendants"
doesn't solve the problem.
Here is my XML code:
row_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/item_checkbox"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
/>
<EditText
android:id="#+id/item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="60px"
android:text="Test text"
android:singleLine="true"
android:layout_toRightOf="#id/item_checkbox"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/item_important"
android:layout_toStartOf="#+id/item_important" />
<ImageButton
android:id="#+id/item_important"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:visibility="visible"
android:layout_marginLeft="10dp" />
</RelativeLayout>
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
>
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
It would be great if you could suggest me some resource where to understand how focusability works in this case.
Thank you
UPDATE
I found out the problem is created by the keyboard showing up: I guess the view recycling is the reason why the EditText loses focus.
I solved my issue using the new RecyclerView class. It seems that was a problem of the ListView
Try this code
row_item.xml
<?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="wrap_content"
android:orientation="horizontal" >
<CheckBox
android:id="#+id/item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:focusable="false"/>
<EditText
android:id="#+id/item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/item_important"
android:layout_toRightOf="#id/item_checkbox"
android:layout_toStartOf="#+id/item_important"
android:minHeight="60px"
android:focusable="true"
android:singleLine="true" />
<ImageButton
android:id="#+id/item_important"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="visible" />
</RelativeLayout>
fragment_list.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
>
<ListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"/>
</FrameLayout>
Activity Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_list);
ArrayList<String> mArrayList = new ArrayList<String>();
/**mArrayList.add("Test");
mArrayList.add("Test");
mArrayList.add("Test");
mArrayList.add("Test");**/
CustomeAdapter adapter = new CustomeAdapter(this, mArrayList);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setItemsCanFocus(true);
listView.setAdapter(adapter);
}
public class CustomeAdapter extends ArrayAdapter {
ArrayList<String> mArrayList;
#SuppressWarnings("unchecked")
public CustomeAdapter(Context context, ArrayList<String> users) {
super(context, R.layout.row_item, users);
mArrayList = new ArrayList<String>();
mArrayList = users;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(getContext()).inflate(R.layout.row_item, parent, false);
viewHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.item_checkbox);
viewHolder.mEditText = (EditText) convertView.findViewById(R.id.item_text);
viewHolder.mImageView = (ImageView) convertView.findViewById(R.id.item_important);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.mEditText.setText(mArrayList.get(position));
return convertView;
}
// View lookup cache
private class ViewHolder {
CheckBox mCheckBox;
EditText mEditText;
ImageView mImageView;
}
}
Did you set android:focusable="true" and android:focusableInTouchMode="true" on your EditText?
Also the android:windowSoftInputMode="adjustPan" in your AndroiManifest file.
Im working on a ListView and each row has two square imageViews. The drawable is given dynamically by url. The problem is that the picture does not fit screen.
the image from url is already square but i want the image to scale up or down to fit the screen.
right now it looks like first screenshot but i want them to look like the second one.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
>
<ImageView
android:id="#+id/list_item_image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<ImageView
android:id="#+id/list_item_image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
</LinearLayout>
this is Adapter
public class ListAdapter extends ArrayAdapter<ListItem>{
private ArrayList<ListItem> items;
private Activity context;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;
public ListAdapter(Activity context, int textViewResourceId, ArrayList<ListItem> items)
{
super(context, textViewResourceId, items);
this.items = items;
this.context = context;
inflater = (LayoutInflater)context.getLayoutInflater();
imageLoader = new ImageLoader(context);
}
public static class ViewHolder{
public ImageView image1;
public ImageView image2;
}
#Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
View rowView = convertView;
ViewHolder holder;
if (rowView == null) {
rowView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.image1 = (ImageView)rowView.findViewById(R.id.list_item_image1);
holder.image2 = (ImageView)rowView.findViewById(R.id.list_item_image2);
rowView.setTag(holder);
}
else
holder = (ViewHolder)rowView.getTag();
imageLoader.DisplayImage("http://dev.ts.hs.kr/jjhoon713/images/1400023976194990.jpg", holder.image1);
imageLoader.DisplayImage("http://dev.ts.hs.kr/jjhoon713/images/1400023976194990.jpg", holder.image2);
return rowView;
}
}
try like this,
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/list_item_image1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitXY"
/>
<ImageView
android:id="#+id/list_item_image2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_weight="1"
android:scaleType="fitXY"
/>
</LinearLayout>
Try with below code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_margin="5dp"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2"
>
<ImageView
android:id="#+id/list_item_image1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:scaleType="centerCrop"
/>
<ImageView
android:id="#+id/list_item_image2"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher"
android:scaleType="centerCrop"
/>
</LinearLayout>
take a linear layout with weightsum=2 and assign 1 weight to each imageview like below:
<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="wrap_content" // you can hardcode height if your view
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="#+id/imageview1"
android:layout_width="0dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:layout_height="wrap_content" // can change to fill parent if you hardcode the height of your view
android:src= ""// your image if static, else set dynamically
android:scaleType="fitxy"/>
<ImageView
android:id="#+id/imageview2"
android:layout_height=""// can change to fill parent if you hardcode the height of your view
android:layout_width="0dp"
android:adjustViewBounds="true"
android:layout_weight="1"
android:src="wrap_content" // your image if static, else set dynamically
android:scaleType="fitxy"/>
</LinearLayout>
my problem was that layout width of list_view.xml was wrap content.
since parent of list items are list_view so weight didnt work.
i changed them to fill_parent and now it works well
Above is the main screen of my app with a custom adapter for listview. How do I put a different icon for each row in the listview? I tried putting an ImageView in the xml but it overwrites my whole screen only showing an icon.
MainAcitivty 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:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/hp_color"
tools:context=".MainActivity" >
<TextView
android:id="#+id/cityText"
style="?android:attr/textAppearanceMedium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textSize="28sp" />
<ImageView
android:id="#+id/condIcon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_below="#id/cityText"
android:contentDescription="weather icon" />
<TextView
android:id="#+id/condDescr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/condIcon"
android:textSize="20sp" />
<ListView
android:id="#+id/mainListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<ImageButton
android:id="#+id/btnPicturePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="picturePage"
android:src="#drawable/photo" />
Custom adapter:
public class WeatherAdapter extends ArrayAdapter<String> {
private final Activity context;
private ArrayList<String> weatherData;
static class ViewHolder {
public TextView text;
public ImageView image;
}
public WeatherAdapter(Activity context, ArrayList<String> weatherData) {
super(context, R.layout.list, weatherData);
this.context = context;
this.weatherData = weatherData;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
// reuse views
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.list, null);
// configure view holder
ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) rowView.findViewById(R.id.rowTextView);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
ArrayList<String> s = new ArrayList<String>(weatherData);
String []sa = new String [s.size()];
sa = s.toArray(sa);
holder.text.setText(sa[position]);
if (sa[position].startsWith("Air pressure")) {
rowView.setBackgroundResource(R.color.skyblue) ;
}
return rowView;
}
List xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#color/white" >
</TextView>
Your list.xml should be something similar to this (taken from one my files)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:gravity="center_vertical" android:orientation="horizontal"
android:background="#drawable/xml_pressed_bg" android:clickable="true" android:layout_width="fill_parent"
android:layout_height="54dip"
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ttshow="http://schemas.android.com/apk/res-auto">
<ImageView android:id="#+id/imgThumb" android:layout_width="42.0dip" android:layout_height="42.0dip" android:scaleType="fitXY"
android:src="#drawable/sample_dj" />
<TextView android:textSize="14.0sp" android:textColor="#ff666666" android:id="#+id/txtName"
android:layout_width="180dip" android:layout_height="wrap_content"
android:singleLine="true" android:shadowColor="#99ffffff" style="#style/TextShadowLight"
android:text="Hello" android:paddingLeft="10dip"/>
</LinearLayout>
Every row item has an image (imgThumb). This needs to be changed or set to a static image, depending on your needs. This is done in the adapters' getView() method.
imgThumb.setImageResource(R.drawable.my_image);
Private TypedArray img;
img=getResource.obtainTypedArray(R.array.imgIcon)
if (sa[position].startsWith("Air pressure")) {
rowView.setBackgroundResource(R.color.skyblue) ;
holder.image.setImageResource(img.gerResourceId[0]);
}else if (sa[position].startsWith("Air Condition")) {
holder.image.setImageResource(img.gerResourceId[1]);
}