listView with just images - android

I am trying to create a listView with just images. Please this is not a stupid question so any help is appreciated. I think my code is correct, but when I run it, it just shows the contentView but empty. It does not show the images I tell it to show.I have created the java file and then two xml files, one with the listView which is the contentView for the java file, and the other for one single row of the listView.
My java file:
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
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.ListView;
public class Craft extends Activity {
int[]images={R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,R.drawable.five,R.drawable.six,R.drawable.seven;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.craft);
listView=(ListView)findViewById(R.id.listView);
MyAdapter adapter=new MyAdapter(Craft.this,images);
listView.setAdapter(adapter);
}
class MyAdapter extends ArrayAdapter<String> {
Context c;
int[] images;
int count = 0;
public MyAdapter(Context c,int imgs[]) {
super(Craft.this, R.layout.single_row);
this.c = Craft.this;
this.images = imgs;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.single_row, parent, false);
Log.d("Plates", "Count: " + count++);
ImageView myImage = (ImageView) row.findViewById(R.id.imageView23);
myImage.setImageResource(images[position]);
return row;
}
}
}
my craft.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:background="#drawable/woodcrop">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center_vertical"
android:layout_weight="1" />
My single_row.xml file
<?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/woodcrop">
<ImageView
android:layout_margin="20dp"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/one"
android:id="#+id/imageView23" />
Any help is appreciated!

You should change your ListView height to:
<ListView
.
.
.
android:layout_height="match_parent"/>
I see, you have not overriden getCount() in your adapter:
So your listview dosen't know that there are items.
You should add this to your adapter:
#Override
public int getCount() {
return images.length;
}

Ty to use this super statement
Super(c , R.layout.single_row,imgs);

Related

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

How to show row numbers on Android ListViews?

I have a ListView set with an ArrayAdapter in my Android app. It lists a set of string values. How do I make it show the row number in front of each list item? Like this:
One
Two
Three
Simply print the position, its in the getView's first param. Dont forget to +1 becuause position starts from 0
I guess you're trying to achieve this,
If so, here's the working code :
MainActivity.java
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView lv;
String[] title = {"Windows","Apple","Android" };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.listView1);
CustomAdapter adapter = new CustomAdapter(this,title);
lv.setAdapter(adapter);
}
}
class CustomAdapter extends ArrayAdapter<String>
{
Context context;
String[] title;
CustomAdapter(Context c, String[] title)
{
super(c, R.layout.listitem,title);
this.context = c;
this.title=title;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = vi.inflate(R.layout.listitem, parent, false);
TextView titlee = (TextView) row.findViewById(R.id.textView1);
int pos = position+1;
titlee.setText(+pos + ". " + title[position]);
pos++;
return row;
}
}
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" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
lisitem.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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Title"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

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?

Getting ID from ListView item when clicking a checkbox

I use a ListView with a custom adapter to build a list with ckeckboxes. Now I dont know how i can fetch the "ID" from the selected box.
The xml file for the items look like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<TextView android:id="#+id/class_open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:onClick="clickHandler" />
<TextView
android:id="#+id/medi_name"
android:text="Test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/checkbox" />
</RelativeLayout>
I need the value from: "class_open"
this is my activity:
package de.bodprod.dkr;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class MediNotmediUserListNew extends Activity{
LayoutInflater inflater;
ArrayList<HashMap<String, Object>> medi_notmedi_items;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.medi_notmedi_user_list_new);
ListView mediListCheck = (ListView) findViewById(android.R.id.list);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
medi_notmedi_items = new ArrayList<HashMap<String,Object>>();
SystemDatabaseHandler db = new SystemDatabaseHandler(getApplicationContext());
medi_notmedi_items = db.getAllNotmedis();
final CustomAdapter adapter = new CustomAdapter(this, R.layout.medi_notmedi_user_list_new, medi_notmedi_items);
mediListCheck.setAdapter(adapter);
}
public void clickHandler(View view) {
}
private class CustomAdapter extends ArrayAdapter<HashMap<String, Object>>{
public CustomAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> Strings) {
super(context, textViewResourceId, Strings);
}
private class ViewHolder{
TextView class_open, title;
}
ViewHolder viewHolder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView=inflater.inflate(R.layout.medi_notmedi_user_list_new_item, null);
viewHolder=new ViewHolder();
viewHolder.class_open=(TextView) convertView.findViewById(R.id.class_open);
viewHolder.title=(TextView) convertView.findViewById(R.id.medi_name);
convertView.setTag(viewHolder);
}else{
viewHolder=(ViewHolder) convertView.getTag();
}
viewHolder.class_open.setText(medi_notmedi_items.get(position).get("sql_id").toString());
viewHolder.title.setText(medi_notmedi_items.get(position).get("wirk").toString());
return convertView;
}
}
}
How I can get the value from the class_open field in the clickHandler?
Is it possible to assign the value directly to the checkbox and request it?
Is it possible to request the value from the class_open field?
Please excuse my bad english
Markus

ListView with RelativeLayout not highlighting background on press

I want to have a list of elements (using a ListView) and each element in list is styled with a relative layout. Currently, the items are being displayed correctly, however, for some reason the listview items dont glow green when they're clicked. Why is this?
I have removed all code to minimals and it still does this.
Activity class
package com.test;
import android.app.Activity;
public class My_ListView extends Activity
{
private ListView_Adapter listViewAdapter;
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview);
// initialise the list-view object
listViewAdapter = new ListView_Adapter(this);
listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(listViewAdapter);
for (int i=0;i<20;i++)
{
listViewAdapter.add("item "+i);
}
}
public void clicked(View v)
{
v.setBackgroundColor(0xFF0000FF);
}
}
The listview item adapter class
package com.test;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListView_Adapter extends ArrayAdapter<String>
{
public ListView_Adapter(Context c)
{
super(c, R.layout.my_listview_item);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
StationFinder_ListViewItemHolder holder = null;
if (row == null)
{
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.my_listview_item, parent, false);
holder = new StationFinder_ListViewItemHolder(row);
row.setTag(holder);
}
else
{
holder = (StationFinder_ListViewItemHolder) row.getTag();
}
holder.populateFrom(getItem(position));
return row;
}
static class StationFinder_ListViewItemHolder
{
private TextView destination = null;
StationFinder_ListViewItemHolder(View row)
{
destination = (TextView) row.findViewById(R.id.text1);
}
void populateFrom(String locationDistance)
{
destination.setText(locationDistance);
}
}
}
my_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
my_listview_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="clicked"
>
<TextView android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
I think you may have to call invalidate() after setting the background color.
You may want to do this instead by setting the listSelector and possibly drawSelectorOnTop for your ListView. That way the selection/deselection and clicks will be handled in the normal manner.
Edit - also, since you're using a ListView, you probably want to listen for clicks by setting an OnItemClickListener on your ListView.

Categories

Resources