• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Help required to create a ListView

abcd2000

Lurker
Sep 11, 2011
5
0
Hello,
I am struggling for creating a list view which should show one icon and a text in each row. The examples given in developer.android.com site for ListView is very simple and has only String to list in the ListView.

I am not sure how to define the layout for my requirement and how to build the Adapter class to list the icon and text at the same time.

Please help me to understand the ListView because the Book I have "Professional Android 2 Application Development" is really a useless book. Also the above site is also not very helpful.

Thanks in advance.
Cheers
 
I am trying to display a text and an icon in a ListView. Below is my code. When I run, I am not getting anything displayed in simulator. Can somebody please loot at the code below and tell me what is wrong?

I tried to make this code as suggested above.
------------------------------------------------------
listview.xml:
------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<ListView android:id="@android:id/list"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:padding="3dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"/>
</ListView>

------------------------------------------------------
FileActivity.java:
------------------------------------------------------
package com.vsb.file;

import android.app.ListActivity;
import android.os.Bundle;

public class FileActivity extends ListActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.listview);
FileAdapter adapter = new FileAdapter(this);
adapter.addFile(new FileView(this, null, "One", R.drawable.folder));
adapter.addFile(new FileView(this, null, "Two", R.drawable.file));
setListAdapter(adapter);
}
}

------------------------------------------------------
FileView.java:
------------------------------------------------------
package com.vsb.file;

import java.io.File;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FileView extends LinearLayout {

private File file;
private ImageView image;
private TextView text;
private int resId;

public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

public ImageView getImage() {
return image;
}

public void setImage(int resId) {
this.image.setImageResource(resId);
this.resId = resId;
}

public TextView getText() {
return text;
}

public void setText(CharSequence text) {
this.text.setText(text);
}

public FileView(Context context) {
super(context);
}

public FileView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public FileView(Context context, File file, CharSequence lable, int resId) {
super(context);
this.file = file;
text = new TextView(context);
text.setText(lable);
image = new ImageView(context);
this.resId = resId;
image.setImageResource(resId);
}

public int getResId() {
return resId;
}

public void setResId(int resId) {
this.resId = resId;
}

}

------------------------------------------------------
FileAdapter.java:
------------------------------------------------------
package com.vsb.file;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class FileAdapter extends BaseAdapter {

private List<FileView> list;
private Context context;

public FileAdapter(Context context) {
this.context = context;
list = new ArrayList<FileView>();
}

public void addFile(FileView fv) {
list.add(fv);
}
@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
FileView fv;
if(convertView == null) {
fv = new FileView(context, null, list.get(position).getText().getText(), list.get(position).getResId());
} else {
fv = (FileView)convertView;
fv.setText(list.get(position).getText().getText());
fv.setImage(list.get(position).getResId());
}
return fv;
}

}
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones