Show an Image and Text in a ListView - java

I'm developing an App, that shows a ListView with 30 items.
In each row, there should be a text and an Image.
I searches for different tutorials, but unfortunately all don't work for me.
So I'm asking you to have a short look to my code and tell me what's wrong in there, because when I start the app, the ListView is empty.
mainActivity.java:
setContentView(R.layout.auswertung);
ListView list = (ListView) findViewById(R.id.listView1);
list.setAdapter(new MyListAdapter(this, fragen));
MyListAdapter.java
import java.util.ArrayList;
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 android.widget.TextView;
public class MyListAdapter extends ArrayAdapter<Frage>{
private final Context context;
private ArrayList<Frage> fragen;
public MyListAdapter(Context context, ArrayList<Frage> f) {
super(context, R.layout.reihe);
this.context = context;
fragen = f;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null)
{
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.reihe, parent, false);
}
TextView textView = (TextView) rowView.findViewById(R.id.textView4);
ImageView imageView = (ImageView) rowView.findViewById(R.id.img1);
textView.setText("Frage " + (position+1));
if (fragen.get(position).getRichtig()==true)
{
imageView.setImageResource(R.drawable.richtig);
}
else
{
imageView.setImageResource(R.drawable.falsch);
}
return rowView;
}
}
auswertung.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="fill_parent"
android:layout_weight="0.96"
android:background="#drawable/hintergrund"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="35dp"
android:layout_marginTop="19dp"
android:text="Auswertung"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000" />
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="460dp"
android:layout_marginTop="60dp">
</ListView>
</RelativeLayout>
reihe.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip"
android:paddingBottom="6dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:id="#+id/textView4"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="5"
android:textSize="16dp"
android:textColor="#000000" />
<ImageView
android:id="#+id/img1"
android:layout_width="25dp"
android:layout_height="25dp" />
</LinearLayout>
As you see, I have a background image, and would also ask you, how can I make the background of the ListView invisible, so that I can see my background behind the text and the Image?

you should pass the the ArrayAdapter constructour your Frage data set, or at least,
ovveride ArrayAdapter.getCount(), and let it returns the number of items in ArrayList<Frage> f
int getCount() {
return (f == null) ? 0 : f.size();
}
here the doc for getCount()

Related

Image is visible but text is not visible in ListView

I am trying to create a custom layout for list view in android studio. I was able to successfully get image in the in ListView on runtime but wasn't able to print text in the list view . When i run my program in emulator it runs without any error but it doesn't show any text it only shows image from drawables.
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private final static String[] names={"A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A","A",
"A","A","A","A","A","A","A","A","A","A","A","A","A","A",};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView ls = findViewById((R.id.customlistview));
ls.setAdapter(new CustomListAdapter(getApplicationContext(),names,R.drawable.za));
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<ListView
android:id="#+id/customlistview"
android:layout_width="409dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
CustomListAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomListAdapter extends BaseAdapter {
private final Context context;
private final String[] names;
private final int image;
private final LayoutInflater layoutInflater;
public CustomListAdapter(Context context,String[] names,int image) {
this.context=context;
this.image=image;
this.names=names;
layoutInflater= (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return names.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rootview;
rootview = layoutInflater.inflate(R.layout.list_item_layout, null);
TextView tv= rootview.findViewById(R.id.textView);
ImageView iv=rootview.findViewById(R.id.imageView);
tv.setText(names[position]);
iv.setImageResource(image);
return rootview;
}
}
list_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:height="70dp"
android:gravity="center"
android:text="TextView"
android:textSize="18sp" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_weight="2"
app:srcCompat="#mipmap/ic_launcher" />
</LinearLayout>
The issue is in your list_item_layout file. There is an issue with match_parent in your layout try using below code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="horizontal">
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:height="70dp"
android:gravity="center"
android:text="TextView"
android:textSize="18sp" />
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="2"
app:srcCompat="#mipmap/ic_launcher" />
The problem was caused by the text colour , changing the colour of textview text in list_item_layout resolved the issue.

How to move to next activtity with bundle the touch event is not working

guys in my project i need to click the item on the list and move to next activity using bundle i need to carry some data also, but i have some issue..
Please help me to move to next activity i can't understand what is the mistake i did,when i click on the item it is not even responding..
here i just use "e_name.setText("hiiii")" for testing purpose its not working.
AT THE END I HAVE MENTIONED MY LAYOUT CODES ALSO.
if possible please send me a link to learn more about custom adapter and list view..
//Main activity.java
package com.prematix.ashok.friday_9;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
EditText e_name, e_desig, e_salary;
Button save;
String itemNames[];
String itemDescriptions[];
ListView itemsListView;
ArrayList<Item> list = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e_name = (EditText) findViewById(R.id.ed_name);
e_desig = (EditText) findViewById(R.id.ed_desig);
e_salary = (EditText) findViewById(R.id.ed_salary);
save = (Button) findViewById(R.id.save);
itemsListView = (ListView) findViewById(R.id.mylist);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//create adapter object
CustomListAdapter adapter = new CustomListAdapter(getApplicationContext(), generateItemsList());
//set custom adapter as adapter to our list view
itemsListView.setAdapter(adapter);
}
private ArrayList<Item> generateItemsList() {
itemNames = new String[]{e_name.getText().toString()};
itemDescriptions = new String[]{e_desig.getText().toString()};
for (int i = 0; i < itemNames.length; i++) {
list.add(new Item(itemNames[i], itemDescriptions[i]));
}
return list;
}
});
itemsListView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
e_name.setText("hiiiii");
}
});
// ListView itemsListView = (ListView)findViewById(R.id.list_view_items);
}
}
//CustommListAdapter.java
package com.prematix.ashok.friday_9;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by ${Nagaveni} on 2017-06-09.
*/
public class CustomListAdapter extends BaseAdapter {
private Context context;
private ArrayList<Item> items;
public CustomListAdapter(Context context,ArrayList<Item> items){
this.context = context;
this.items = items;
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int i) {
return items.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null) {
view = LayoutInflater.from(context).inflate(R.layout.layout_list_view_row_items, viewGroup, false);
}
//get current item to be displayed
Item currentitem = (Item)getItem(i);
//get the text view for item name and item designation
TextView name = (TextView)view.findViewById(R.id.name);
TextView designation = (TextView)view.findViewById(R.id.desig);
TextView salary = (TextView)view.findViewById(R.id.salary);
//sets the text for item name and item designation from the current item object
name.setText(currentitem.getNames());
designation.setText(currentitem.getDesignation());
salary.setText("10,000");
//returns the view for current row
return view;
}
}
//here is my layout code
layout_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">
<LinearLayout
android:layout_width="260dp"
android:layout_marginLeft="8dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="name"
android:id="#+id/name"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="designation"
android:id="#+id/desig"
android:textStyle="bold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="salary"
android:id="#+id/salary"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="70dp">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:orientation="vertical"
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.prematix.ashok.friday_9.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Name"
android:ems="10"
android:id="#+id/ed_name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Designation"
android:ems="10"
android:id="#+id/ed_desig" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Salary"
android:ems="10"
android:id="#+id/ed_salary" />
<Button
android:text="Save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/save" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mylist"/>
</LinearLayout>

Why do my Custom GridView show only one column?

I'v tried making a Custom GridView that adds proggramatically TextView items.
The TextViews needs to act as an ImageView only that it has a text within it.
But for some odd reason it shows me only 1 column out of 4 (but if its too big then 3 is my minimum capcity).
I look through similar cases for people who attempted the same thing, but all the same as it ended failing.
All I get is a one column Gridview each time.
I'd like to hear some opinions about it, in fix this issue.
My GridView:
<GridView
android:id="#+id/gridView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_marginTop="73dp"
android:layout_toEndOf="#+id/imageView15"
android:columnCount="4"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth" />
My item layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent"
android:backgroundTint="#android:color/black"
android:alpha="100"
android:layoutDirection="ltr">
<TextView
android:id="#+id/item"
android:layout_width="150px"
android:layout_height="150px"
android:layout_marginRight="50px"
android:textColor="#android:color/black"
android:textSize="10sp" />
</LinearLayout>
And finally the Adapter:
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class CustomListAdapter extends BaseAdapter {
private Activity context;
private List<TextView> items;
private static LayoutInflater inflater;
public CustomListAdapter(Activity context, List<TextView> items) {
this.context = context;
this.items = items;
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
public class Holder {
TextView tv;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.inventory_item, null);
Holder holder = new Holder();
holder.tv = (TextView) rowView.findViewById(R.id.item);
holder.tv.setText(items.get(position).getText());
holder.tv.setBackground(items.get(position).getBackground());
holder.tv.setTextColor(items.get(position).getTextColors());
return rowView;
}
}
Thanks in advance.
shows me only 1 column out of 4
you need to use: android:numColumns="4"
in your code:
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:background="#color/color_white"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="4"
android:padding="5dp"
android:scrollbars="none|horizontal"
android:stretchMode="columnWidth"></GridView>

Display Data in GridView with DataAdapter in Android

I have try the following code for displaying data from data adapter in gridview.
Following link is for GridView with DataAdapter Tutorial.
I am wanted the following output :
I have follow the tutorial and get the output as i wanted, but unfortunetly when scroll the page,
application will be closed automatically.
ERROR : "Application Failure Detected, Please Try Again"
How to resolve these problem?
code : main.xml file.
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:stretchMode="columnWidth"
android:cacheColorHint="#00000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:clipChildren="true"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip" />
code : MainActivity.java file.
package com.example.gridviewdata;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.GridView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new DataAdapter(this));
}
code : customgrid.xml file
<?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">
<TableLayout android:id="#+id/TableLayout01"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<TableRow android:id="#+id/TableRow01"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtId"
android:layout_gravity="center_horizontal" />
<TextView android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtName"
android:layout_gravity="center_horizontal" />
</TableRow>
</TableLayout>
</LinearLayout>
code : DataAdapter.java file
package com.example.gridviewdata;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class DataAdapter extends BaseAdapter {
Context mContext;
private String [] id = {"S001","S002","S003","S004","S005","S006","S007","S008","S009","S010","S011","S012"};
private String [] name={"Rohit","Rahul","Ravi","Amit","Arun","Anil","Kashif","Nayan","Jay","Sagar","Jairaj","Vishal"};
private LayoutInflater mInflater;
public DataAdapter(Context c)
{
mContext=c;
mInflater = LayoutInflater.from(c);
}
public int getCount()
{
return id.length;
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder=null;
if(convertView==null)
{
convertView = mInflater.inflate(R.layout.customgrid,parent,false);
holder = new ViewHolder();
holder.txtId=(TextView)convertView.findViewById(R.id.txtId);
holder.txtId.setPadding(100, 10,10 , 10);
holder.txtName=(TextView)convertView.findViewById(R.id.txtName);
holder.txtName.setPadding(100, 10, 10, 10);
if(position==0)
{
convertView.setTag(holder);
}
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.txtId.setText(id[position]);
holder.txtName.setText(name[position]);
return convertView;
}
static class ViewHolder
{
TextView txtId;
TextView txtName;
}
}
Give any solution for these problem.
if any other resource is available then give me an reference.
Solution :
In getView() method return all items, and i have setting the tag just in the first item,
when android calls again getView for position 1, i am try to get the tag from convertView,
but i didn't called setTag for convertView 1, then getTag returns a null object, and i am try to get the object txtName from a null object, and then the error occurs.
removing the if(position==0) then application work perfectly and give the scrolling.

Can't work out how to get an image to display in a Listview

I've watched videos, search stack overflow and tried to implement what i have learnt into my code, but i just can't get the Listview to display an image to the right.
Here is my page1.xml (which contains the listview):
<?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="vertical"
android:background="#ffd9d9d9">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_gravity="center"
android:dividerHeight="8dip"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:scrollbars="none"
android:clipToPadding="false"
/>
</LinearLayout>
Here is my custom row.xml (which is working flawlessly)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowtext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical"
android:textColor="#ff858585"
android:textSize="20sp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:minHeight="80dp"
android:background="#drawable/card"/>
And here is my Backups.java:
package com.YvesB.tobin;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.io.File;
public class Backups extends Activity {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.RedTheme);
setContentView(R.layout.page1);
ActionBar ab = getActionBar();
ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);
ListView listView1 = (ListView) findViewById(R.id.listView);
File directory = new File(extStorageDirectory
+ "/Android/data/com.tobin.redirect");
String[] filenames = directory.list();
if (filenames == null){
}
else{
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.row, filenames);
listView1.setAdapter(adapter);
}
}
}
Thanks for the help, the png i want to sue is called drive.png !
On a side note, you guys are all awesome, going out of your way to help others!
The first thing to do is to change your row.xml. It should contain an ImageView that displays your drive.png. Something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowtext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fontFamily="sans-serif-light"
android:gravity="center_vertical"
android:textColor="#ff858585"
android:textSize="20sp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:minHeight="80dp"
android:background="#drawable/card"/>
<ImageView
android:id="#+id/drive"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="5px"
android:layout_marginRight="20px"
android:layout_marginTop="5px"
android:src="#drawable/drive" >
</ImageView>
</LinearLayout>
Then you need to write a custom ArrayAdapter, something like:
public class MyArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public MyArrayAdapter(Context context, String[] values) {
super(context, R.layout.row, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.row, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.rowtext);
ImageView imageView = (ImageView) rowView.findViewById(R.id.drive);
textView.setText(values[position]);
// Change icon based on name or position
// ...
return rowView;
}
}
Then you have to use this custom adapter instead of the standard ArrayAdapter<String>.
Examples are taken (and a bit customized) from this Tutorial:
http://www.mkyong.com/android/android-listview-example/
Hope it helps and does what you have in mind.

Categories