custom listview error: java.lang.unsupportedoperationexception: addview - android

I'm really new to android and I know you shouldnt paste whole codes but I honestly have no idea where the problem is.
Im trying to create a somewhat custom list view, with a text and an image, but keep geting this exception and have no idea how to solve it
02-13 13:23:06.477: E/AndroidRuntime(1834): java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
so was hoping someone here could help me out. this is what my launcher activity looks like
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ListView list=(ListView) this.findViewById(R.id.list_view);
int[] stringIds=new int[2];
stringIds[0]=R.string.text1;
stringIds[1]=R.string.text2;
int[] imgIds=new int[2];
imgIds[0]=R.drawable.ic_launcher;
imgIds[1]=R.drawable.ic_launcher;
CustomAdapter custom=new CustomAdapter(this, stringIds, imgIds);
list.setAdapter(custom);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
My CustomAdapter:
public class CustomAdapter extends BaseAdapter {
Context context;
int[] stringIds;
int[] imgIds;
public CustomAdapter(Context context, int[]stringIds, int[] imgIds) {
this.stringIds=stringIds;
this.imgIds=imgIds;
this.context=context;
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View finalView=convertView;
if(finalView==null){
LayoutInflater inflater=((Activity)context).getLayoutInflater();
finalView= inflater.inflate(R.layout.list_view_row, parent);
}
TextView txt=(TextView)finalView.findViewById(R.id.list_view_row_text_view);
ImageView img=(ImageView)finalView.findViewById(R.id.imageView1);
img.setImageResource(imgIds[position]);
return finalView;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return this.stringIds.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
}
activity_main.xml 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"
tools:context=".MainActivity" >
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
list_view_row.xml layout
<?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" >
<TextView
android:id="#+id/list_view_row_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>

Here:
finalView= inflater.inflate(R.layout.list_view_row, parent);
remove parent
finalView= inflater.inflate(R.layout.list_view_row, null);
or
finalView= inflater.inflate(R.layout.list_view_row, parent, false);
also use
setContentView(R.layout.activity_main);
before getting
ListView

Related

Issue in setting Images with dynamic columns in grid view

I want to set images in grid view with multiple columns. Images can be vary in columns.How can i achieve it. Thanks in advance
Images in GridView
create 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="3" >
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text=" Computer Languages..." />
</RelativeLayout>][1]][1]
Create MainActivity.
public class MainActivity extends Activity {
GridView gv;
Context context;
ArrayList prgmName;
public static String [] prgmNameList={"Let Us C","c++","JAVA","Jsp","Microsoft .Net","Android","PHP","Jquery","JavaScript"};
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gv=(GridView) findViewById(R.id.gridView1);
gv.setAdapter(new CustomAdapter(this, prgmNameList,prgmImages));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Create a folder by name drawable in res directory. Insert the images into drawable folder by name as below.
public static int [] prgmImages={R.drawable.images,R.drawable.images1,R.drawable.images2,R.drawable.images3,R.drawable.images4,R.drawable.images5,R.drawable.images6,R.drawable.images7,R.drawable.images8};
Create the layout as programlist.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:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
Create Customclass ie CustomAdapter.
public class CustomAdapter extends BaseAdapter{
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;
public CustomAdapter(MainActivity mainActivity, String[] prgmNameList, int[] prgmImages) {
// TODO Auto-generated constructor stub
result=prgmNameList;
context=mainActivity;
imageId=prgmImages;
inflater = ( LayoutInflater )context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public class Holder
{
TextView tv;
ImageView img;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.program_list, null);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
}
});
return rowView;
}
}
You will get the Output like..

Putting a Textview on top of a List Fragment

How Can I add a textview on top of a fragment. I'm using the following adapter to populate the listfragment and the xml file is each row in the list fragment. I need to add a textview ontop of listview which must be scrollable along with the list?
public class Adapter extends BaseAdapter {
Context context;
public TextView txtName;
public TextView txtTitle;
private LayoutInflater mInflater;
private Storage storage;
FontManager fontManager;
Typeface typeface;
public Adapter(Context _context,Storage _storage) {
context = _context;
mInflater = LayoutInflater.from(context);
this.storage = _storage;
fontManager = new FontManager(_context);
typeface = fontManager.getTypeFace();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return _storage.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
convertView = mInflater.inflate(R.layout.row3, null);
txtName = (TextView) convertView.findViewById(R.id.tvName);
txtName.setTypeface(typeface);
txtName.setText(storage.getName(position));
txtTitle = (TextView) convertView.findViewById(R.id.tvTitle);
txtTitle.setTypeface(typeface);
txtTitle.setText(storage.getTitle(position));
return convertView;
}
}
xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="20dp"
android:scrollbars="none"
android:id="#+id/ll1">
<TextView
android:id="#+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView"
android:textColor="#android:color/black" />
</LinearLayout>
ListFragment:
public class SampleListFragment extends ListFragment {
int number;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
number = getArguments().getInt(
"num", 0);
new SampleAsyncTask(this, getActivity(), number).execute();
return super.onCreateView(inflater, container, savedInstanceState);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
}
}
}
Following code is used in AsyncTask class to populate the list fragment
Adapter adapter = new Adapter(context, storage);
listFragment.setListAdapter(adapter);
You can do as below or add a textview as a header to your listivew
Quoting from the docs
ListActivity(ListFragment is similar) has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain aListView` object with the id "#android:id/list" (or list if it's in code)
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"
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" >
<fragment android:name="com.example.listfragment.MyFragment"
android:id="#+id/frag"
android:layout_above="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
MyFragment.java
public class MyFragment extends ListFragment {
String names[] ={"A","B","C"};
#Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myFragmentView = inflater.inflate(R.layout.list_frag, container, false);
TextView tv = (TextView) myFragmentView.findViewById(R.id.textView1);
tv.setText("My Header");
setListAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,names));
return myFragmentView;
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// on click display the item in toast
Toast.makeText(getActivity(), (String)l.getItemAtPosition(position), Toast.LENGTH_SHORT).show();
}
}
list_frag.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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="TextView" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
snap shot
Edit: if you want the textview to scroll
<?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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="TextView" />
</RelativeLayout>
Before calling setListAdapter
View view = inflater.inflate(R.layout.text, null);
TextView textinlfated = (TextView) view.findViewById(R.id.textView1);
ListView lv = getListView();
textinlfated.setText("TextView scrolls");
lv.addHeaderView(view);
change adapter's getView method and put a case on position and in case of 0, return your textview in other cases listview items.

Customlistview not inflating

I was trying to implement simple customlistview.What i tried to do is to inflate a text and a picture inside each list row in the listview.I got no output i.e. layout was not inflating..I got a blank screen.I used 2 Java class and 2 xml files.Below is my code..Please let me know where is my mistake and what should i correct?
CListView.java
package com.example.customlistview;
public class Clistview extends Activity {
int p1[]={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};
String[] Shapes1={"circle","circle","circle","circle","circle"};
ListView lv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_clistview);
lv=(ListView)findViewById(R.id.listView1);
CustomListViewAdapter clvadapter=new CustomListViewAdapter(Clistview.this,Shapes1,p1);
lv.setAdapter(clvadapter);
}
}
CustomListViewAdapter.java
package com.example.customlistview;
public class CustomListViewAdapter extends BaseAdapter {
Context context;
String shape1[];
int pic[];
public CustomListViewAdapter(Context c,String[] sh,int[] p)
{
this.context=c;
this.shape1=sh;
this.pic=p;
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
public String getItem(int pos) {
// TODO Auto-generated method stub
return shape1[pos];
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int pos, View v, ViewGroup vg) {
// TODO Auto-generated method stub
View itemview=v;
TextView tv1;
ImageView im1;
if(itemview==null)
{
LayoutInflater inflater=(LayoutInflater)((Activity)context).getLayoutInflater();
itemview=inflater.inflate(R.layout.clistview1,vg,false);
}
tv1=(TextView)itemview.findViewById(R.id.textView1);
im1=(ImageView)itemview.findViewById(R.id.imageView1);
tv1.setText(shape1[pos]);
im1.setImageResource(pic[pos]);
return itemview;
}
}
activity_clistview.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=".Clistview" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp" >
</ListView>
</RelativeLayout>
clistview1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp"
>
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="right"
/>
</LinearLayout>
Inside CustomListViewAdapter, change this
public int getCount() {
// TODO Auto-generated method stub
return shape1.length;
}
It is an important function that decides the number of items in a ListView.
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
You are returning 0 .
Return pic.length or shape1.length as per your requirement.
public int getCount() {
return pic.length;
}
Method getCount() returns the number of items in the datatset for the adapter.
getCount()

How to add ImageView, TextView and Button in ListView in android

In my application I am trying to create a ListView that contains an ImageView a TextView and a Button. I have created a separate XML file and drag all the above mentioned elements in that XML and in my main java file I have created an Object of BaseAdapter and in the getView() method I have declared these elements but when I run the application I cant see the list. I haven't used the BaseAdapter, so I am missing with some code in order to view the List. I also want to apply some operations on the button so please let me know that code also.
Code for my main.xml file:
<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/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Code for my list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="75dp"
android:layout_height="75dp"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="0dp"
android:layout_marginTop="15dp"
android:text="Medium Text"
android:textSize="15dp" />
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="25dp"
android:text="Button" />
</LinearLayout>
Code for my main.java file:
public class CustomListActivity extends Activity {
/** Called when the activity is first created. */
ListView lv;
LayoutInflater inflator;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.listView1);
BaseAdapter bs = new BaseAdapter() {
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vw = inflator.inflate(R.layout.list_items, null);
ImageView img = (ImageView)findViewById(R.id.imageView1);
TextView tv = (TextView)findViewById(R.id.textView1);
Button btn = (Button)findViewById(R.id.button1);
return vw;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public int getCount() {
// TODO Auto-generated method stub
return 2;
}
};
}
}
Thanks in advance....
ok,
public class App_Adapter extends BaseAdapter implements OnClickListener{
private Activity mActivity;
private List<App_List> mList;
private static LayoutInflater inflater=null;
private PackageManager pm;
private String appclass;
private ApplicationTask mApplicationTask;
private String link=null;
public App_Adapter (FavouriteApp favouriteApp,List<App_List> mAppList, String appclass) {
// TODO Auto-generated constructor stub
this.mActivity= favouriteApp;
this.mList= mAppList;
this.appclass = appclass;
inflater = (LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
pm=mActivity.getPackageManager();
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View mView=convertView;
if (convertView == null)
mView = inflater.inflate(R.layout.app_adapter, parent,false);
App_List mAppList= mList.get(position);
**here i am setting two textview and one button**
((TextView) mView.findViewById(R.id.textView_appName)).setText(mAppList.getApp_Name());
((TextView) mView.findViewById(R.id.textView_appDescription)).setText(mAppList.getApp_Description());
boolean status = isAppInstalled(mAppList.getApp_Pkg());
Button btn = (Button) mView.findViewById(R.id.button_appStatus);
**// register the button for clicklistener**
btn.setOnClickListener(this);
return mView;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
and call this adapter class from your activity.
You are definitely missing lv.setAdapter(bs);

Android: BaseAdapter not showing elements

The app I'm struggling to make has a simple list and a button present in the bottom no matter what. My problem is that my custom BaseAdapter doesn't show elements. I know that since my elements are only a string I could use an ArrayAdapter, but the assignment requires it. The code:
class ListaOrase extends BaseAdapter{
private Activity context;
ArrayList<String> orase;
public ListaOrase(Activity context){
this.context=context;
orase=new ArrayList<String>();
}
public void add(String string){
orase.add(string);
}
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
element = inflater.inflate(R.layout.lista, null);
}
else element = convertView;
TextView elementLista=(TextView)element.findViewById(R.id.elementLista);
elementLista.setText(orase.get(position));
return element;
}
}
public class WeatherAppActivity extends ListActivity {
Button buton;
ListaOrase lista;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lista=new ListaOrase(this);
buton=(Button)findViewById(R.id.buton);
setListAdapter(lista);
lista.add("Bucuresti");
lista.add("Sibiu");
}
}
My XML files go like this:
main.xml -- for the Activity
<?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"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#android:id/list"
android:layout_alignParentTop="true"/>
<RelativeLayout
android:id="#+id/relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/buton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="add"
android:text="Add"
android:layout_gravity=""
/>
</RelativeLayout>
</RelativeLayout>
lista.xml -- for the list
<?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" >
<TextView
android:id="#+id/elementLista"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
The button "downstairs" opens a dialog that adds items to my list. It obviously doesn't work, but I think it's the BaseAdapter (ListaOrase) itself, as the intents throw no exceptions. I would be thankful if the items I hardcoded in would show up (Bucuresti and Sibiu).
What am I doing wrong?
Thank you very much! :)
Your code almost perfect issue is return orase.size() in getcount() method,you are returning 0, add unimplemented methods in ListaOrase class.
your ListaOrase should be like this:
class ListaOrase extends BaseAdapter{
private Activity context;
ArrayList<String> orase;
public ListaOrase(Activity context){
this.context=context;
orase=new ArrayList<String>();
}
public void add(String string){
orase.add(string);
}
public View getView (int position, View convertView, ViewGroup list) {
View element;
if (convertView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
element = inflater.inflate(R.layout.lista, null);
}
else element = convertView;
TextView elementLista=(TextView)element.findViewById(R.id.elementLista);
elementLista.setText(orase.get(position));
Log.e("",orase.get(position));
return element;
}
public int getCount() {
// TODO Auto-generated method stub
return orase.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
hope this helps
getCount method was really returning 0 so consquently getView() wasn't being called. When I changed to:
getcount() {
return arrayObject.Size();
}

Categories

Resources