Exception in GridView - android

I was trying a sample program to understand the behavior of the dynamic GridView. When I tried to execute the program I got a run-time exception at texts.setText(str[position]); in the .java file.
.java
package GridView_dynamic.grid;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;
public class GridView_dynamic extends Activity {
/** Called when the activity is first created. */
TextView select;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
select = (TextView)findViewById(R.id.text);
GridView grid = (GridView)findViewById(R.id.grid);
grid.setAdapter(new my_Adapter(this));
}
public class my_Adapter extends BaseAdapter{
String[] str = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N"};
public Context context;
public my_Adapter(Context context) {
// TODO Auto-generated constructor stub
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return str.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
Toast.makeText(context,"convertView is NULL", Toast.LENGTH_LONG).show();
}
TextView texts;
texts = (TextView)convertView;
//texts = (TextView)findViewById(R.id.text);
texts.setText(str[position]);
return texts;
}
}
}
main.xml
<?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"
>
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#0000aa"
android:textColor="#00aa00"
/>
<GridView
android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#aaaaaa"
/>
</LinearLayout>

You get a NullPointerException because when you get a null convertView(the list is first built) you don't create a new TextView. Your getView() method should be:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
Toast.makeText(context,"convertView is NULL", Toast.LENGTH_LONG).show();
//if convertView is null is your job to make a new View
convertView = new TextView(context);
}
((TextView)convertView).setText(str[position]);
return convertView;
}

By the looks of it you never actually instantiate the TextView in getView. The following code will create a new textView if convertView is null.
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null)
convertView = new TextView(GridView_dynamic.this);
((TextView)convertView).setText(str[position]);
return convertView;
}
Hope this helps!

Related

How to add a new EditText with separate row in a ListView

I have 24 EditTexts and I want to have one EditText in each row.
Note: this code is working properly, but I want to add one EditText in one row.
I want to add one EditText per row.
Means I want to set 24 EditTexts in 24 separate rows, not 24 EditText in one row.
This is lyt_listview_activity.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" >
<ListView
android:id="#+id/listViewMain"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
And this is-lyt_listview_list.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="#android:color/white"
android:orientation="horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:textColor="#android:color/black"
android:layout_height="wrap_content"
android:text="15dp" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
android:ems="10" >
<requestFocus />
</EditText>
</LinearLayout>
This is -ListviewActivity.java
package com.example.testlistview;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class ListviewActivity extends Activity {
private String[] arrText =
new String[]{"Text1","Text2","Text3","Text4"
,"Text5","Text6","Text7","Text8","Text9","Text10"
,"Text11","Text12","Text13","Text14","Text15"
,"Text16","Text17","Text18","Text19","Text20"
,"Text21","Text22","Text23","Text24"};
private String[] arrTemp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.lyt_listview_activity) ;
arrTemp = new String[arrText.length];
MyListAdapter myListAdapter = new MyListAdapter();
ListView listView = (ListView) findViewById(R.id.listViewMain);
listView.setAdapter(myListAdapter);
}
private class MyListAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(arrText != null && arrText.length != 0){
return arrText.length;
}
return 0;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arrText[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//ViewHolder holder = null;
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = ListviewActivity.this.getLayoutInflater();
convertView = inflater.inflate(R.layout.lyt_listview_list, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.editText1 = (EditText) convertView.findViewById(R.id.editText1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.ref = position;
holder.textView1.setText(arrText[position]);
holder.editText1.setText(arrTemp[position]);
holder.editText1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
arrTemp[holder.ref] = arg0.toString();
}
});
return convertView;
}
private class ViewHolder {
TextView textView1;
EditText editText1;
int ref;
}
}
}
In lyt_listview_list.xml you only have one EditText.
Those are your rows, and therefore, there's only one EditText (and a TextView) in each row
How many rows you see is determined by getCount(), and that's returning arrText.length, or 24.
As far as "adding another row". You can't add anything to an array. You need to use an Arraylist in your adapter. In which case, you'd likely want ArrayAdapter and not BaseAdapter

Saving all EditText texts displayed in a GridView

My idea is to create a table where I can store my players scores in different activities.
I thought creating a GridView which contains an EditText so I can change values dynamically. Looking for in the web I found some source codes which I modified. I got my purpose (create a GridView with an EditText inside) however now I'm not able to retrieve the data (numbers in this case) stored in the EditTexts.
Do you know how could I do it?
Thanks in advance,
This is my Adapter:
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import
android.widget.EditText;
public class AdaptadorResultados extends ArrayAdapter<String> {
Activity context;
AdaptadorResultados(Activity context,String[] datos) {
super(context, R.layout.resultados, datos);
this.context = context;
}
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View item = inflater.inflate(R.layout.resultados, null);
final EditText txtCelda =(EditText)item.findViewById(R.id.txtCelda2);
txtCelda.setText(this.getItem(position).toString());
return(item);
}
This the xml file:
<?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" >
<EditText
android:id="#+id/txtCelda2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
></EditText>
</LinearLayout>
Solved
Finally I got it changing the source code of the adaptor. I post it here in case someone has the same problem:
public class AdaptadorEditText extends BaseAdapter{
private Context myContext;
private String[]nombres;
private EditText[] myCells;
public AdaptadorEditText(Context c, String[] nombres, int num){
myContext=c;
this.nombres=nombres;
myCells=new EditText[nombres.length*num];
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return nombres.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return myCells[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
EditText cell;
if (myCells[position] == null)
{
cell = myCells[position] = new EditText(myContext);
cell.setText(nombres[position]);
}
else
{
cell = myCells[position];
}
return cell;
}

Items in HListView are not clickable using HorizontalListView

What I need is a Horizontal scrollable ListView that serves as a horizontally scrollable menu.
I searched for a solution and came up with the this library.
I am trying to implement it.sephiroth.android.library.widget.AdapterView.OnItemClickListener on it.sephiroth.android.library.widget.HListView object in a DialogFragment.
I can get the list to populate but I can't seem to be able to attach listeners to the item.
I have been trying for 2 days to figure this out, but no game. This feature is still not working. So I turn to the old WWW for salvation..
This is my DialogFragment XML fragment_layout.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"
android:background="#800000"
android:descendantFocusability="blocksDescendants" >
<it.sephiroth.android.library.widget.HListView
android:id="#+id/hlvPlacesListScrollMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:scrollbars="none"
android:divider="#android:color/transparent"
/>
this is my viewitem.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:background="#800000"
android:clickable="false"
android:focusable="false"
android:orientation="vertical" >
<ImageButton
android:id="#+id/ibScrollMenuImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#800000"
android:clickable="false"
android:focusable="false"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/tvScrollMenuTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false"
android:gravity="center_horizontal"
android:textColor="#f4f4f4" />
</LinearLayout>
This is my main_activity_layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/llDialogFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#34f34f"
android:orientation="vertical"
tools:context=".MainActivity" >
</LinearLayout>
Pretty basic.
My MainActicity is :
package com.example.hscrollviewtest;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
LifeStatsDialogFragment menuFragment = new LifeStatsDialogFragment();
ft.add(R.id.llDialogFragment, menuFragment).commit();
}
#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;
}
}
the Dialogfrgment .java :
package com.example.hscrollviewtest;
import it.sephiroth.android.library.widget.AdapterView;
import it.sephiroth.android.library.widget.AdapterView.OnItemClickListener;
import it.sephiroth.android.library.widget.HListView;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LifeStatsDialogFragment extends DialogFragment implements
OnItemClickListener {
private HListView scroll;
private View rootView;
private HorizontalScrollMenuAdapter mAdapter;
final String[] IMAGE_TITLE = new String[] { "Home", "Work", "School",
"Sport" };
final int[] MENU_IMAGES = new int[] { R.drawable.ic_circle_home,
R.drawable.ic_circle_work, R.drawable.ic_circle_school,
R.drawable.ic_circle_gym };
public LifeStatsDialogFragment newInstance() {
return new LifeStatsDialogFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
rootView = inflater.inflate(R.layout.fragment_layout, container, false);
mAdapter = new HorizontalScrollMenuAdapter(getActivity(),
R.layout.fragment_layout, R.id.tvScrollMenuTitle, IMAGE_TITLE,
MENU_IMAGES);
scroll = (HListView) rootView
.findViewById(R.id.hlvPlacesListScrollMenu);
scroll.setAdapter(mAdapter);
scroll.invalidate();
scroll.setOnItemClickListener(this);
for (int i = 0; i < scroll.getAdapter().getCount(); i++) {
Log.i(this.getClass().getSimpleName(), "first item in scroll : "
+ scroll.getChildAt(i) + "and its clickable?? "
+ scroll.getAdapter().getItemViewType(i) + "\n");
}
Log.i(this.getClass().getSimpleName(),
"The number of children for HlistView is: "
+ scroll.getParent().toString());
return rootView;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
}
}
and this is the adapter(which works when I use it in the HorizontalVariableListViewDemo):
package com.example.hscrollviewtest;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
public class HorizontalScrollMenuAdapter extends ArrayAdapter<String>{
private String[] mButtonText;
private int[] mIconId;
private final String TAG = this.getClass().getSimpleName();
//Constructor
public HorizontalScrollMenuAdapter(Context context, int resource,
int textViewResourceId, String[] menuItemName, int[] menuItemImage) {
super(context, resource, textViewResourceId, menuItemName);
// TODO Auto-generated constructor stub
mButtonText = menuItemName;
mIconId = menuItemImage;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return mIconId.length;
}
#Override
public String getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater mInflater = (LayoutInflater) parent.getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.viewitem, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.tvScrollMenuTitle);
holder.icon=(ImageButton) convertView.findViewById(R.id.ibScrollMenuImage);
//holder.icon.setBackgroundResource(android.R.color.transparent);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(mButtonText[position]);
holder.icon.setImageResource(mIconId[position]);
holder.icon.setTag(mIconId[position]);
Log.d(TAG,"returned view to fragment");
return convertView;
}
static class ViewHolder{
TextView name;
ImageButton icon;
}
}
I hope one of you can see my blindspot.
Thaks
Probably you are implementing the wrong OnItemClickListener.
Try to use
public class LifeStatsDialogFragment extends DialogFragment implements
it.sephiroth.android.library.widget.AdapterView.OnItemClickListener {
//...
}
I would try 2 things:
Put the fragment in the xml layout in the first place, and avoid add in the onCreate.
What happens in the onItemClick? - its currently empty. Try using an independent onItemClickListener:
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "clicked", Toast.LENGTH_SHORT);
}
});

android GridView does not work in my emulator:does not display it though no error is shown

I tried everything. I tested by copy pasting the example here from developer website but still didnt do work. it just shows a blank space whee the gridview was supposed to come. Here is my code.
EnterApp.java
package com.locationremind.app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class EnterApp extends Activity {
Context con;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
con=this;
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.enterapp);
GridView gridView=(GridView) findViewById(R.id.grid_menu);
gridView.setAdapter(new MyAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
if(position==0)
startActivity(new Intent(getBaseContext(),LocationReminderActivity.class));
else
Toast.makeText(getBaseContext(), "Item"+position+"not Allocated yet",Toast.LENGTH_SHORT).show();
}
});
}
}
MyAdapter.java
package com.locationremind.app;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
Context context;
public MyAdapter(Context c) {
context = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
#Override
public Object getItem(int pos) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView tv;
/*ImageView imageView;
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();*/
if(convertView==null)
{
tv=new TextView(context);
tv.setLayoutParams(new GridView.LayoutParams(85,85));
}
else
tv=(TextView)convertView;
tv.setBackgroundResource(thumbsID[position]);
tv.setMinimumHeight(128);
tv.setMinimumWidth(128);
tv.setLayoutParams(new GridView.LayoutParams(85,85));
tv.setText(text[position]);
return tv;
}
private Integer[] thumbsID={R.drawable.gridview_icon,R.drawable.icn};
private String[] text={"Select Location Using GPS","Select Location Using Wi-Fi",
"Select Location Using Network","Settings"};
}
enterapp.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:id="#+id/Rlayout"
>
<ImageView
android:id="#+id/img_icon"
android:src="#drawable/android"
android:maxWidth="80dp"
android:maxHeight="80dp"
android:layout_width="64px"
android:layout_height="64px"
android:contentDescription="logo-icon"/>
<GridView
android:id="#+id/grid_menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
</LinearLayout>
you mistake lies in your adapter as
#Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
saying you dont have any item to display that is why its not showing any element.
to solve this change your getCount() like this
#Override
public int getCount() {
// TODO Auto-generated method stub
return thumbsID.length;
}
getCount returns 0, that's why!
public int getCount() {
// TODO Auto-generated method stub
return text.lenght;
}
You are value is not coming because in your adapter getCount() method is returning 0. put some values on that. or if u put the list as argument in adapter, the u can take that list size as the return for the getCount().

ListActivity throws NullPointerException

Hey guys i have a ListActivity... a very simple at that... and it keeps throwing NullPointer Exception though i have done it exactly as the Sample List7 except that i have used the Layout inflater... below is the code... Can u plz comment the error i have done here??
import java.util.Vector;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
public class CustomList extends ListActivity implements OnItemSelectedListener{
Vector<String> VTitle;
Vector<String> VDescription;
TextView display;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
VTitle.addElement("First Title");
VTitle.addElement("Second Title");
VTitle.addElement("Third Title");
VTitle.addElement("Fourth Title");
VDescription.addElement("1 Description");
VDescription.addElement("2 Description");
VDescription.addElement("3 Description");
VDescription.addElement("4 Description");
display = (TextView)findViewById(R.id.display);
setListAdapter(new CustomAdapter(this));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
display.setText(VTitle.elementAt(position));
}
class CustomAdapter extends BaseAdapter {
protected Activity mContext;
public CustomAdapter(Activity context) {
mContext = context;
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return VTitle.size();
}
#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;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
if(row==null) {
LayoutInflater inflater = mContext.getLayoutInflater();
row = inflater.inflate(R.layout.row,null);
}
TextView title = (TextView)row.findViewById(R.id.title);
title.setText(VTitle.elementAt(position));
TextView description = (TextView)row.findViewById(R.id.description);
description.setText(VDescription.elementAt(position));
ImageView image = (ImageView)row.findViewById(R.id.image);
switch(position){
case 0:
image.setImageResource(R.drawable.check);
break;
case 1:
image.setImageResource(R.drawable.dos);
break;
case 2:
image.setImageResource(R.drawable.smily);
break;
case 3:
image.setImageResource(R.drawable.wrong);
break;
}
return(row);
}
}
#Override
public void onItemSelected(AdapterView parent, View v, int position, long id) {
// TODO Auto-generated method stub
display.setText(VTitle.elementAt(position));
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
And the xmls are like this....
"main.xml"
<?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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "#+id/display"
android:text="something"
/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:drawSelectorOnTop="false"
android:layout_height="0px">
</ListView>
</LinearLayout>
"row.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">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
<TextView
android:text="Title"
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<TextView
android:text="description"
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
VTitle and VDescription aren't initialized
Before accessing one of these attributes, you should :
VTitle = new Vector<String>();
VDescription = new Vector<String>();
Moreover in java, the first letter of an attribute name should be lowercase, and in android this first letter should be an m, to denote a member field.

Categories

Resources