I want to create a ListView populated by my own layout and my data. So I looked at examples and I found that I need to extend ArrayAdapter<>.
So I made a CustomAdapter and did everything by following an example.
There is no error in the code but the ListView #id/dashboard doesn't appear in my MainActivity.
I can see the TextView from activity_main.xml but not the ListView.
The sliding menu of the DrawerLayout is working too.
I inflate overview.xml in my CustomAdapter.
Can you give me an hint why it's not working? I'm working on it for hours.
Thank's
CustomAdapter
public class CustomAdapter extends ArrayAdapter<Ssoverview> {
private int layoutId;
private Context context;
private ArrayList<Ssoverview> list;
public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
super(context, resource);
this.layoutId = resource;
this.context = context;
this.list = ssList;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if (v == null) {
v = (RelativeLayout) ((Activity)context).getLayoutInflater().inflate(this.layoutId, null); //, parent, false);
}
Ssoverview ss = this.list.get(position);
if (ss != null) {
TextView titleTV=(TextView) v.findViewById(R.id.title);
titleTV.setText(ss.getTitle());
TextView authorTV=(TextView) v.findViewById(R.id.author);
authorTV.setText(ss.getAuthor());
}
Log.d("Deb", "Fabrication de la View : "+position);
return v;
}
}
activity_main.xml :
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ole" />
<ListView
android:id="#+id/dashboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
</FrameLayout>
<ListView android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
overview.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#color/grey"
android:layout_marginTop="10sp"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Titre" />
<TextView
android:id="#+id/note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:text="Note : x/5 y votes" />
<TextView
android:id="#+id/author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="35dp"
android:text="John Doe" />
</RelativeLayout>
And the MainActity where I set the adapter
public class MainActivity extends Activity {
private ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.dashboard);
//Declaration factices des Ssoverview
ArrayList<Ssoverview> ssList = new ArrayList<Ssoverview>();
Ssoverview ss1 = new Ssoverview("Dune", "David", "4");
Ssoverview ss2 = new Ssoverview("Le petit poucet", "MLP", "2");
Ssoverview ss3 = new Ssoverview("Locked", "Hervé", "4,2");
ssList.add(ss1);
ssList.add(ss2);
ssList.add(ss3);
Log.d("D", ssList.get(0).getTitle());
//Declaration de l'adapter avec le layout et la ssList
CustomAdapter adapter = new CustomAdapter(this, R.layout.overview, ssList);
lv.setAdapter(adapter);
Log.d("D","Fin de onCreate");
}
}
change
public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
super(context, resource);
this.layoutId = resource;
this.context = context;
this.list = ssList;
}
to
public CustomAdapter(Context context, int resource, ArrayList<Ssoverview> ssList) {
super(context, resource , ssList); <-- change this line
this.layoutId = resource;
this.context = context;
this.list = ssList;
}
in arrayAdapter class super in constructor handle size of your list, as you don't pass your data this set to 0 in default so getView not called at all
Related
I want to custom each line of List view is Button with the text view inside it .
But it's not work!
It's only show a button that i have created in the first layout .
What can i do for fix ? Many thanks
Here's my code :
public class MainActivity extends AppCompatActivity {
Button button;
ListView lv ;
ListViewAdapter listViewAdapter;
ArrayList<String> al ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.lstView);
button = (Button)findViewById(R.id.button);
al = new ArrayList();
al.add("cho");
al.add("phuong");
al.add("hello");
al.add("bye");
al.add("love");
listViewAdapter= new ListViewAdapter(this , R.layout.button , al);
lv.setAdapter(listViewAdapter);
This is adapter :
public class ListViewAdapter extends ArrayAdapter {
/**
* Constructor
*
* #param context The current context.
* #param resource The resource ID for a layout file containing a TextView to use when
*/
Activity context;
int layResID;
ArrayList<String> object;
public ListViewAdapter(Activity context, int resource , ArrayList<String> object) {
super(context, resource);
this.context=context;
this.layResID = resource;
this.object = object;
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
LayoutInflater inflater = context.getLayoutInflater();
convertView = inflater.inflate(layResID, null);
}
Button btn = (Button)convertView.findViewById(R.id.view);
String string = object.get(position);
btn.setText(string);
return convertView;
}
}
This is main xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.macbook.note.MainActivity"
android:background="#1A237E"
android:orientation="vertical"
android:weightSum="10"
>
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_marginTop="30dp"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:textSize="10dp"
android:background="#drawable/button"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/button" />
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="9"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ListView
android:id="#+id/lstView"
android:divider="#1A237E"
android:dividerHeight="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</ScrollView>
</LinearLayout>
</LinearLayout>
This is custom 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="match_parent">
<Button
android:id="#+id/view"
android:layout_width="300dp"
android:layout_height="100dp"
android:background="#drawable/btn_list"
android:textSize="10dp"/>
</LinearLayout>
Change your ArrayAdapter's super call to the following
super(context, object);
I guess the problem is that you do not have specified the current count of your items which should be created inside of list view. You have two options:
1. Call other constructor of ArrayAdapter so ArrayAdapter class will handle it for you:
public ListViewAdapter(Activity context, int resource , ArrayList<String> object) {
super(context, resource, object);
}
2. Override ArrayAdapater.getCount() inside your adapter class:
#Override
public int getCount() {
return this.object.size();
}
I am new to android . I have an application working with listview and i want to number each list view item.
Like,
1 list item
2 list item
3 list item
MainActivity.java
public class MainActivity extends AppCompatActivity {
private OrderDbManager orderDbManager;
private ListView listView;
private SimpleCursorAdapter adapter;
final String[] from = new String[] { OrderDbHelper.FOOD_NAME,
OrderDbHelper.QUANTITY, OrderDbHelper.PRICE };
final int[] to = new int[] {R.id.tvFoodName, R.id.tvItemQuantity, R.id.tvItemPrice };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
orderDbManager = new OrderDbManager(this);
orderDbManager.open();
final Cursor cursor = orderDbManager.fetch();
listView = (ListView) findViewById(R.id.listView);
listView.setEmptyView(findViewById(R.id.empty));
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, from, to, 0);
adapter.notifyDataSetChanged();
LayoutInflater layoutInflater = getLayoutInflater();
listView.addHeaderView(layoutInflater.inflate(R.layout.cart_header,listView, false));
listView.setAdapter(adapter);
}
public class MyAdapter extends SimpleCursorAdapter {
public MyAdapter(Context context, int layout, Cursor c,
String[] from,int[] to) {
super(context, layout, c, from, to);
}
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_item, null, true);
TextView txt= (TextView) convertView.findViewById(R.id.tvSlNo);
txt.setText(position + 1);
return convertView;
}
}
}
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/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
list_item.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:layout_margin="5dp"
android:minHeight="50dp"
android:orientation="horizontal"
android:weightSum="1">
<TextView
android:id="#+id/tvSlNo"
android:layout_width="0dip"
android:layout_height="match_parent"
android:gravity="center"
android:textStyle="normal"
android:textColor="#android:color/black"
android:layout_weight="0.1" />
<TextView
android:id="#+id/tvFoodName"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:textStyle="normal"
android:textColor="#android:color/black"
android:text=""/>
<TextView
android:id="#+id/tvItemQuantity"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.15"
android:textStyle="normal"
android:textColor="#android:color/black"
android:gravity="center"/>
<TextView
android:id="#+id/tvItemPrice"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.25"
android:gravity="center"
android:textStyle="normal"
android:textColor="#android:color/black"
android:text=""/>
</LinearLayout>
i want to set numbering in tvSlNo.
please help
In your getView() method of your listViewAdapter simply use the position variable and set it on your TextView. You may need to add 1 to the position since it starts with 0. So use (position+1) and then set it to your TextView
Though this question may be too late but for other reference purposes.
Check where you have this code: txt.setText(position + 1);
you are close to your solution. You just have to typecast position + 1 to String since setText() only take String alone.
I am new in android. I want to show the data that I get from Database into ListView.
For now I can show only a single data.
How to show multiple data into custom ListView?
Here's my MainActivity.class
DatabaseHandler db = new DatabaseHandler(this);
/**
* CRUD Operations
* */
Log.d("Reading: ", "Reading all contacts..");
List <AllItem> allItems = new ArrayList<AllItem>();
allItems = db.getAllAccommodations();
ArrayList <String> allItems2 = new ArrayList<String>();
for (AllItem cn : allItems) {
allItems2.add(cn.getItem_name());
allItems2.add(cn.getAreaNAme());
}
ArrayAdapter <String> adapter = new ArrayAdapter <String> (this, android.R.layout.simple_list_item_1,allItems2);
listview.setAdapter(adapter);
I have my own custom ListView like this
Acco.xml
<ListView
android:id="#+id/listAccommodation"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="8"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:background="#color/white"
android:divider="#color/black90"
android:dividerHeight="5.0sp"
android:listSelector="#color/black30" >
</ListView>
AccoLayout.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_below="#+id/item_name" />
</RelativeLayout>
Is there anyone can help me with this?
You need to create CustomAdapter class for this.
Use your listview as it is.
<ListView
android:id="#+id/listAccommodation"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="8"
android:layout_marginLeft="7dip"
android:layout_marginRight="7dip"
android:background="#color/white"
android:divider="#color/black90"
android:dividerHeight="5.0sp"
android:listSelector="#color/black30" >
</ListView>
And your custom Listview also.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dip"
android:paddingTop="10dip"
android:paddingBottom="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_below="#+id/item_name" />
</RelativeLayout>
Create your custom adapter class and pass data to this class.
adapter = new ListAdapter(this, allItems2);
adapter.notifyDataSetChanged();
listview.setAdapter(adapter);
Here is CustomAdapter class.
public class ListAdapter extends BaseAdapter {
public ListAdapter(Activity a, List <AllItem> allItems) {
activity = a;
data = d;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.AccoLayout, null);
TextView itemName = (TextView)vi.findviewById(R.id.item_name);
TextView areaName = (TextView)vi.findviewById(R.id.area_name);
// Set your data here
itenName.setText(data.get(position));//like this
return vi;
}
}
I assume that your allItems2 list has a pattern like itemName0, areaName0, itemName1, areaName1, itemName2.. etc. Then, you can write a custom adapter like this.
public class CustomAdapter extends ArrayAdapter<String> {
private final Activity context;
private final List<String> items;
public CustomAdapter (Activity context, List<String> items) {
super(context, R.layout.AccoLayout, items);
this.context = context;
this.items= items;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.AccoLayout, null, true);
TextView itemName = (TextView) rowView.findViewById(R.id.item_name);
TextView areaName= (TextView) rowView.findViewById(R.id.area_name);
itemName.setText(items.get(2*position));
areaName.setText(items.get(2*position + 1));
return rowView;
}
}
Then call it from your main activity;
CustopAdapter adapter = new CustomAdapter (MainActivity.this, allItems2);
listview.setAdapter(adapter);
Also, by Android's convention, try not to use uppercase letters in XML file names to prevent any complication. You may change to acco_layout.xml.
Consider this to be your Cursor: after you fetch from DB
Cursor mCur = db.getAllAccommodations();
in onCreate:
CurAdapter Cur = new CurAdapter(getActivity(), mCur,0);
final ListView lv = (ListView)findViewById(R.id.listAccommodation);
lv.setFastScrollEnabled(true);
lv.setAdapter(Cur);
Then Make a Subclass:
private class CurAdapter extends CursorAdapter{
public CurAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tv = (TextView) view.findViewById(R.id.item_name);
TextView tv1 = (TextView) view.findViewById(R.id.area_name);
String item = (cursor.getString(cursor.getColumnIndexOrThrow("ColumnName1")));
String area = dateConvert(cursor.getString(cursor.getColumnIndexOrThrow("ColumnName2")));
tv1.setText(item);
tv.setText(area);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.AccoLayout, null);
return view;
}
}
Where R.layout.AccoLayout is your "listview row" layout, for example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:paddingLeft="10dip"
android:paddingTop="10dip" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="#+id/area_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/item_name"
android:textSize="14sp" />
</RelativeLayout>
I'm trying to get data from sqlite to listview so i followed the example in the answer in this url
Make listview from SQLite database
the CustomCursorAdapter is going like that
public class CustomCursorAdapter extends SimpleCursorAdapter {
private int layout;
private LayoutInflater inflater;
private Context context;
public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.layout = layout;
this.context = context;
inflater = LayoutInflater.from(context);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = inflater.inflate(R.layout.topics, parent, false);
return v;
}
#Override
public void bindView(View v, Context context, Cursor c) {
//1 is the column where you're getting your data from
String name = c.getString(1);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.listLabel);
if (name_text != null) {
name_text.setText(name);
}
}
}
but onCreate i have this code
ListView listView = (ListView)findViewById(R.layout.topics);
connectToDB();
from = new String[] { "topicTitle" };
to = new int[] { R.id.listLabel };
CustomCursorAdapter adapter = new CustomCursorAdapter(this,
R.layout.topics, cursor, from, to);
listView.setAdapter(adapter);
with xml as follows
<?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" >
<ImageView
android:id="#+id/listLogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
>
</ImageView>
<TextView
android:id="#+id/listLabel"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="15sp" >
</TextView>
</LinearLayout>
the problem is .. listView is Null!! .. which i don't know why!!
You are refering to your layout topics. You should have a listview defined in xml with a id.
In your activity onCreate() after setCOntentView(R.layout.topics), fins the id of listview and set adapter to your list.
In your xml say topics.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"
android:background="#0095FF">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:id="#+id/lv">
</ListView>
// other ui elements
</LinearLayout>
Then in your activity onCreate()
setContentview(R.layout.topics);
ListView listView = (ListView)findViewById(R.id.lv);
connectToDB();
from = new String[] { "topicTitle" };
to = new int[] { R.id.listLabel };
CustomCursorAdapter adapter = new CustomCursorAdapter(this,
R.layout.topics, cursor, from, to);
listView.setAdapter(adapter);
From the discussion in chat
EDIT:
Define main.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"
android:background="#0095FF">
<ListView android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:focusableInTouchMode="false"
android:listSelector="#android:color/transparent"
android:layout_weight="2"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:dividerHeight="8dp"
android:divider="#000000"
android:cacheColorHint="#000000"
android:drawSelectorOnTop="false">
</ListView>
</LinearLayout>
In your activivity
public class MainActivity extends Activity {
ListView lv;
CustomCursorAdapter cus;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) findViewById(R.id.list);
cus= new CustomCursorAdapter(parameters);
lv.setAdapter(cus);
}
}
Define customCursor adapter inflate a custom layout. Set image for image view and text for textview.
Try to edit your xml
<?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"
android:id="#+id/main"
>
<ImageView
android:id="#+id/listLogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
>
</ImageView>
<TextView
android:id="#+id/listLabel"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#+id/label"
android:textSize="15sp" >
</TextView>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/topics"
></ListView>
</LinearLayout>
In Activity onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.layout.topics);
connectToDB();
from = new String[] { "topicTitle" };
to = new int[] { R.id.listLabel };
CustomCursorAdapter adapter = new CustomCursorAdapter(this,
R.layout.topics, cursor, from, to);
listView.setAdapter(adapter);
}
I have a listview which contains custom rows. When I show the listview with it's custom rows, all the rows only have a height of about 2px. I tried it on an Android 4.0 device, and on an emulator which has Android 2.x. I added a screenshot of the problem.
Prepare, here comes quite a lot of code:
This is my code for my custom row:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="105px"
android:background="#fff"
android:orientation="vertical"
android:paddingLeft="15dp" >
<TextView
android:id="#+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginTop="8px"
android:gravity="top"
android:text="name"
android:textColor="#322a37"
android:textSize="11pt" />
</RelativeLayout>
The code in my activity:
public class ListActivity extends BaseActivity {
private ArrayList<User> m_items;
private ObjectAdapter m_adapter;
private EditText textSearch;
private ListView listView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
textSearch = (EditText) findViewById(R.id.textSearch);
listView = (ListView) findViewById(R.id.listView);
m_items = new ArrayList<User>();
//(fill m_items, goes well)
this.m_adapter = new ObjectAdapter(this, R.layout.row_text, m_items);
listView.setAdapter(this.m_adapter);
}
private class ObjectAdapter extends ArrayAdapter<User> {
private ArrayList<User> items;
#SuppressWarnings("unchecked")
public ObjectAdapter(Context context, int textViewResourceId,
ArrayList<User> items) {
super(context, textViewResourceId, items);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row_text, null);
}
User u = items.get(position);
if (u != null) {
TextView top = (TextView) v.findViewById(R.id.toptext);
if (top != null) {
top.setText(u.getName());
}
}
return v;
}
}
}
Anyone any clue why this is happening?
The line:
android:layout_height="0dip"
does not strike me as being particularly clever.
Edit your custom row xml and correct the hight of the TextView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="105px"
android:background="#fff"
android:orientation="vertical"
android:paddingLeft="15dp" >
<TextView
android:id="#+id/toptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginTop="8px"
android:gravity="top"
android:text="name"
android:textColor="#322a37"
android:textSize="11pt" />