Adding items to data source doesn't appear in ListView - android

I am adding items to my data source and would like it to appear in my ListView. For some reason nothing is appearing:
Adapter:
private class STCompanyAdapter extends ArrayAdapter<STCompany> {
private ArrayList<STCompany> items;
public STCompanyAdapter(Context context, int textViewResourceId,
ArrayList<STCompany> 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.addstocksrow, null);
}
STCompany q = items.get(position);
if (q != null) {
TextView symbolText = (TextView) v.findViewById(R.id.toptext);
TextView nameText = (TextView) v.findViewById(R.id.bottomtext);
if (nameText != null) {
nameText.setText(q.getSymbol());
}
if (symbolText != null) {
symbolText.setText(q.getName());
}
}
return v;
}
}
Adding items in onCreate:
listView = (ListView) findViewById(R.id.symbolsListView);
this.companiesAdapter = new STCompanyAdapter(this, R.layout.addstocksrow, companies);
listView.setAdapter(this.companiesAdapter);
STCompany myCompany = new STCompany();
myCompany.setName("La La");
myCompany.setSymbol("BP");
companiesAdapter.add(myCompany);
companiesAdapter.notifyDataSetChanged();
Here is my layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/LinearLayout">
<EditText android:id="#+id/addStocksEditText" android:text="Search company or stock" android:layout_width="200dp" android:layout_height="50dp"></EditText><Button android:layout_height="wrap_content" android:text="Search" android:id="#+id/searchButton" android:layout_width="fill_parent"></Button><ListView android:id="#+id/symbolsListView" android:layout_width="fill_parent" android:layout_height="fill_parent"></ListView>
</LinearLayout>

try companies.add(myCompany) instead of companiesAdapter.add(myCompany).

Problem was my layout. listview wasn't appearing. Code is ok.

Related

How to Hide or Kill The White Space Below The Spinner Drop-Down List

I have successfully implemented Spinners in my project using the Spinner class which exists in this link:
How to make an Android Spinner with initial text "Select One"
Also, I customized the layout of each item and named it as spinner_entries_style.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="#ffffff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:background="#640c1c"
android:maxEms="10"
android:padding="10dp"
android:singleLine="false"
android:textColor="#d7a801"
android:textSize="18sp" />
</LinearLayout>
Also, These are the Adapter classes I used in my code..
class LoanTypeAdapter extends ArrayAdapter<String> {
Context context;
String[] items;
public LoanTypeAdapter(Context context, String[] items) {
super(context, R.layout.spinner_entries_style, R.id.textView, items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(android.R.layout.simple_spinner_item,
parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(items[position]);
holder.textView.setTextColor(Color.parseColor("#d7a801"));
holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
holder.textView.setSingleLine(true);
return holder.textView;
// ------------------------------------------
}
class ViewHolder {
final TextView textView;
ViewHolder(View view) {
textView = (TextView) view;
}
}
}
And this..
class LoanProgramAdapter extends ArrayAdapter<String> {
Context context;
String[] items;
public LoanProgramAdapter(Context context, String[] items) {
super(context, R.layout.spinner_entries_style, R.id.textView, items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder = null;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(android.R.layout.simple_spinner_item,
parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.textView.setText(items[position]);
holder.textView.setTextColor(Color.parseColor("#d7a801"));
holder.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
holder.textView.setSingleLine(true);
return holder.textView;
}
class ViewHolder {
final TextView textView;
ViewHolder(View view) {
textView = (TextView) view;
}
}
}
But, there is something strange I encountered after building the spinners. When running the app in Android versions (4.2 or less), there is a white space below the dropdown list.
Here are the screenshots of what happens..
http://www.mediafire.com/view/cqj51t8e3aju18m/01.png
http://www.mediafire.com/view/ure788yetrt00v3/02.png
That is not just on the emulator, but also in the real devices having Android 4.2 or less. Also, this white space seems a bit bigger in some devices.
Is there any idea to hide or kill these white area? Any solution for this problem?
I found the solution of this problem after reading this post.
How to wrap lengthy text in a spinner
In my layout called spinner_entries_style.xml, I should specify the height of text view like that:
android:layout_height="?android:attr/listPreferredItemHeight"
And it will be like that:
<?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="#ffffff"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_marginBottom="2dp"
android:background="#640c1c"
android:maxEms="10"
android:padding="10dp"
android:singleLine="false"
android:textColor="#d7a801"
android:textSize="18sp" />
</LinearLayout>
That solved my problem successfully..
Remove this line from spinner_entries_style.xml.
android:layout_marginBottom="2dp"

Can't find ViewByID in Custom Adapter

I created my own custom adapter that extends Array Adapter:
public class AdapterGeoArea extends ArrayAdapter<GeoArea>{
private Context _context;
private int resource;
private ArrayList<GeoArea> _myGeoArea;
public AdapterGeoArea(Context context, ArrayList<GeoArea> myGeoArea) {
super(context, 0, myGeoArea);
_context = context;
_myGeoArea = myGeoArea;
}
public int getCount() {
return _myGeoArea.size();
}
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout GeoAreaView;
if (convertView == null) {
GeoAreaView = new LinearLayout(_context);
LayoutInflater li = LayoutInflater.from(_context);
convertView = li.inflate(R.layout.layout_geo_areas, null);
}
else {
GeoAreaView = (LinearLayout) convertView;
}
GeoArea curGeoArea = _myGeoArea.get(position);
TextView name = (TextView) GeoAreaView.findViewById(R.id.txtGeoAreaName);
name.setText(curGeoArea.name);
return convertView;
}
static class ViewHolder {
TextView name;
RelativeLayout childContainer;
}
#Override
public Filter getFilter() {
return null;
}
}
And here is how I am using it in my main:
public class ActivityGeoAreas extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_geo_areas);
GeoArea.searchTerm = "Bar & Grill";
GeoArea torontoArea = new GeoArea("cityOfToronto");
ArrayList<GeoArea> testList = new ArrayList<GeoArea>();
testList.add(torontoArea);
AdapterGeoArea adapter = new AdapterGeoArea(this, testList);
ListView lv = (ListView) findViewById(R.id.textGeoArea);
lv.setAdapter(adapter);
}
}
Everything seem to be fine except for this line:
TextView name = (TextView) GeoAreaView.findViewById(R.id.textGeoArea);
And here is how I defined my layout:
<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=".ActivityGeoAreas" >
<TextView
android:id="#+id/textGeoArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Choose Area"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/LinearLayoutGeoAreas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textGeoArea"
android:layout_below="#+id/textGeoArea"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewGeoArea"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
</RelativeLayout>
It simply can't find the TextView textGeoArea and this line become null, what am I doing wrong?
Change this
TextView name = (TextView) GeoAreaView.findViewById(R.id.txtGeoAreaName);
to
TextView name = (TextView) convertView.findViewById(R.id.txtGeoAreaName);
I guess you have got confused.
Move textview to a new layout list_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="match_parent" >
<TextView
android:id="#+id/textGeoArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Choose Area"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Now change your getView as below
LayoutInflater inflater;
public AdapterGeoArea(Context context, ArrayList<GeoArea> myGeoArea) {
super(context, 0, myGeoArea);
_context = context;
_myGeoArea = myGeoArea;
inflater = LayoutInflater.from(context); // initialize inflater
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.list_row,parent,false);
holder = new ViewHolder();
holder.tv = (TextView)convertView.findViewById(R.id.textGeoArea);
holder.setTag(convertView);
}
else {
holder = (ViewHolder) convertView.getTag();
}
GeoArea curGeoArea = _myGeoArea.get(position);
holder.tv.setText(curGeoArea.name);
return convertView;
}
static class ViewHolder // use a view holder for smooth scrolling an performance
{
TextView tv;
}
On the first pass, GeoAreaView is a childless LinearLayout since you just created it. convertView is the View initialized your layout which holds the TextView. So you need to use that to find the TextView.
TextView name = (TextView) convertView.findViewById(R.id.txtGeoAreaName);
I see you have a ViewHolder but you don't appear to be using it. You may want to take a little time and go through a good tutorial on using ListView.
This guy usually seems to have good tutorials

Android : Buttons and edit Text repeated in listView

My problem is that my listView repeat all items of my layout.
I need to have the edit text and the button at the bottom of this listView.
But here the edit text and button is repeated for each row of listView and i don't know why.
If someone could help me
The activity :
public class MessActivity extends ListActivity
{
public Channel chan;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(Network.getInstance().Messages);
Bundle b = getIntent().getExtras();
this.chan = (Channel) b.get("chanSelected");
//add all message of selected chan to the messAdapter
for (int i = 0 ; i < this.chan.getListMessage().size() ; i++) {
Message mess = this.chan.getListMessage().get(i);
Network.getInstance().addMessage(mess);
}
setContentView(R.layout.mess_list);
}
}
The adapter :
public class MessageAdapter extends ArrayAdapter<Message>
{
private Context context;
private int textViewResourceId;
public MessageAdapter(Context context, int textViewResourceId)
{
super(context, textViewResourceId);
this.context = context;
this.textViewResourceId = textViewResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
MessHolder holder = null;
//row null
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(textViewResourceId, parent, false);
holder = new MessHolder();
holder.texte = (TextView)row.findViewById(R.id.listMess);
row.setTag(holder);
}
else
{
holder = (MessHolder)row.getTag();
}
Message mess = getItem(position);
holder.texte.setText(mess.getText());
return row;
}
static class MessHolder
{
TextView texte;
}
}
The layout :
<?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="wrap_content"
android:orientation="vertical" >
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="#android:id/list">
</ListView>
<TextView
android:id="#+id/listMess"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="bottom" >
//edittext
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="text" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Envoyer" />
</LinearLayout>
</LinearLayout>
Thank you for your help.
Use ListView method addFooter(View v).
You can define xml layout for this view and inflate it:
ListView lv = getListView();
LayoutInflater inflater = getLayoutInflater();
View footer = (View)inflater.inflate(R.layout.footer, lv, false);
lv.addFooterView(footer, null, false);
Firstly Reason of this issue is Listview Reuses its view when scrolls , So we should have a logic in our get view to manage that
if(convertview == null)
{
//code here
}else {
//code here
}
, but if you dont want to reuse then you can use following :
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getViewTypeCount() {
return 500;
}

ListView with ImageView and TextView doesn't show anything

I'm populating a ListView which contains an ImageView and a TextView. It doesn't crash, but doesn't show anything either. I see a lot of tutorials but appear to show the same code as I am using.
This is the row.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:background="#drawable/fondo_fila_click"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dip"
android:layout_alignParentLeft="true"
android:background="#drawable/borde_foto"
android:layout_marginRight="5dip">
<ImageView
android:id="#+id/imagen_marca"
android:layout_width="50dip"
android:layout_height="50dip"/>
</LinearLayout>
<TextView
android:id="#+id/nombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail"
android:layout_toRightOf="#+id/thumbnail"
android:textColor="#040404"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
</RelativeLayout>
And the code:
public class SATActivity extends SherlockListActivity {
private IconListViewAdapter adaptador;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList m_fabricante = new ArrayList();
this.adaptador = new IconListViewAdapter(this, R.layout.fila, m_fabricante);
setListAdapter(this.adaptador);
iniciaFabricantes();
}
#SuppressWarnings("unchecked")
private void iniciaFabricantes() {
ArrayList m_fabricante = new ArrayList();
try {
Fabricante acer = new Fabricante();
acer.setNombre("Acer");
acer.setFoto(R.drawable.acer);
Fabricante apple = new Fabricante();
apple.setNombre("Apple");
apple.setFoto(R.drawable.apple);
m_fabricante.add(acer);
m_fabricante.add(apple);
} catch (Exception e) {
}
adaptador.notifyDataSetChanged();
}
public class IconListViewAdapter extends ArrayAdapter<Fabricante> {
private ArrayList<Fabricante> items;
public IconListViewAdapter(Context context, int textViewResourceId, ArrayList<Fabricante> 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, null);
}
Fabricante o = items.get(position);
if (o != null) {
//poblamos la lista de elementos
TextView tt = (TextView) v.findViewById(R.id.nombre);
ImageView im = (ImageView) v.findViewById(R.id.imagen_marca);
if (im!= null) {
im.setImageResource(o.getFoto());
}
if (tt != null) {
tt.setText(o.getNombre());
}
}
return v;
}
}
}
Does anyone know why it doesn't show the text or the images?
Thanks
Try changing your adapter class to below one:
public class IconListViewAdapter extends ArrayAdapter<Fabricante> {
private ArrayList<Fabricante> items;
LayoutInflater mInflater;
public IconListViewAdapter(Context context, int textViewResourceId, ArrayList<Fabricante> items) {
super(context, textViewResourceId, items);
this.items = items;
mInflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.mTextView=(TextView)convertView.findViewById(R.id.nombre);
holder.mImageView=(ImageView)convertView.findViewById(R.id.imagen_marca);
convertView.setTag(holder);
}
else
holder=(ViewHolder)convertView.getTag();
Fabricante o = items.get(position);
if (o != null) {
//poblamos la lista de elementos
holder.mImageView.setImageResource(o.getFoto());
holder.mTextView.setText(o.getNombre());
}
return convertView;
}
static class ViewHolder
{
TextView mTextView;
ImageView mImageView;
}
}
In your getView method, make the following change:
if (im!= null) {
Drawable d = context.getResources().getDrawable(o.getFoto());
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());//must require
if(d!=null)
im.setImageDrawable(d);
else
Log.w("listview", "drawable is not available");
}
Hopefully then it should work.
Try adding a ViewHolder as well as calling your findViewById in your if statement where you inflate the view. http://www.vogella.de/articles/AndroidListView/article.html#overview should help with your custom adapter.

Android - Multiple lists not showing

I have two list, that need to be processed and displayed, I have used the following technique -
I get the data from 2 sources, process it and have 2 Custom Adapters, that take the data and place it in the relvant View.
However I only can view one list (the one at the top of in the view xml) - and not the other one,
Can someone please help me fix this prob. Its driving me insane.
Main Activity code belwo:
public class TestList extends Activity {
private final static String TAG = "ListSample";
private List<String> items;
private List<String> items2;
private CustomTeamListAdapter m_adapter;
private CustomTeamListAdapterTwo t_adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
items = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
items.add(i + "|||>>");
}
ListView list = (ListView) findViewById(R.id.customlist);
this.m_adapter = new CustomTeamListAdapter(this, R.layout.listitem,
(ArrayList<String>) items);
list.setAdapter(m_adapter);
/******************************************************************************/
items2 = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
items2.add(i + ">>----");
}
ListView list_two = (ListView) findViewById(R.id.customlisttwo);
this.t_adapter = new CustomTeamListAdapterTwo(this, R.layout.listitem,
(ArrayList<String>) items2);
list_two.setAdapter(t_adapter);
}
private class CustomTeamListAdapter extends ArrayAdapter<String> {
private ArrayList<String> items;
public CustomTeamListAdapter(Context context, int textViewResourceId,
ArrayList<String> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
String o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.sampletext);
tt.setText(o + "FirstSet");
tt.setId(position);
}
return v;
}
}
private class CustomTeamListAdapterTwo extends ArrayAdapter<String> {
private ArrayList<String> items;
public CustomTeamListAdapterTwo(Context context, int textViewResourceId,
ArrayList<String> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listitem, null);
String o = items.get(position);
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.sampletext);
tt.setText(o + "SecondSet");
tt.setId(position);
}
return v;
}
}
}
Main layout XML below
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/customlisttwo"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="#+id/randomtext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#CCCCCC"
android:text="Random Text" />
<ListView android:id="#+id/customlist" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
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="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/customlisttwo"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<TextView android:id="#+id/randomtext" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:background="#CCCCCC"
android:text="Random Text" />
<ListView android:id="#+id/customlist" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Thanks in advance for your thoughts
Try using Merge Adapter by Mark Murphy https://github.com/commonsguy/cwac-merge. You cant have two list views in the same activity. You can add that text view between the two layouts.

Categories

Resources