android studio: Android scrollable gridview with clickable buttons leading to different activities - android

am creating an android application whereby am required to create a gridview with clickable image buttons each leading to a different activities.Also the gridview should be scrollable since i have many buttons to add .Can anyone help on how to do this? thank you.
Here is a sample of what am trying to do:
sample clickable gridview:

activitymain.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>
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>
In MainActivity.java
package com.gnetspace.simplegridview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.GridView;
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.
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 CustomAdapter and paste the following code.
package com.gnetspace.simplegridview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
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;
}
}
the source link is caveofprograming

Related

How to display the spinner with transparent background for selected value in android?

package com.android.example;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
private ICSAdapter mAdapter;
private String[] mNameList = {"one","two","three" };
Context context=this;
Spinner room_type;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_activity_main);
room_type = (Spinner) findViewById(R.id.ics_spinner);
mAdapter = new ICSAdapter(context, mNameList);
room_type.setAdapter(mAdapter);
room_type.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> Adapter, View view,
int position, long id) {
room_type.setSelection(position);
View selectedText = (View) Adapter.getChildAt(position);
if (selectedText != null) {
RelativeLayout real = (RelativeLayout) selectedText.findViewById(R.id.real);
TextView selected = (TextView) selectedText.findViewById(R.id.name_view);
selected.setText(mNameList[position].toUpperCase());
((View) selected.getParent()).setBackgroundColor(Color.TRANSPARENT);
// real.setBackgroundColor(Color.TRANSPARENT);
}
}
#Override
public void onNothingSelected(AdapterView<?> Adapter) {
// TODO Auto-generated method stub
}
});
}
public class ICSAdapter extends BaseAdapter {
private LayoutInflater inflater;
private Context context;
String[] room_type;
public ICSAdapter(Context context, String[] _accidentList) {
this.room_type = _accidentList;
this.context = context;
inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return room_type.length;
}
#Override
public Object getItem(int position) {
return room_type[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
convertView = inflater.inflate(R.layout.drop_down_view, null);
holder = new ViewHolder();
holder.spinnerText = (TextView) convertView.findViewById(R.id.name_view);
holder.spinnerText.setText(room_type[position]);
return convertView;
}
public class ViewHolder {
TextView spinnerText;
}
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg"
android:layout_gravity="center"
>
<Spinner
android:id="#+id/ics_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_alignParentTop="true"
android:background="#drawable/text3"
/>
</RelativeLayout>
dropdown.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/real"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#2D333C" >
<TextView
android:id="#+id/name_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="5dp"
android:gravity="center"
android:text="324"
android:textColor="#color/android:white"
android:textSize="18sp" />
</RelativeLayout>
Here i am using the default spinner with some background image. I have one issue, when i click the spinner,the dropdown list will display in white color background.But the orignal color background will not displayed(android:background="#2D333C")
I am using the transparent color for backgound change.How to resolve this issue. I want to display the given background color,when i click the spinner in dropdown list.After i select the selected value,the background will transaparent.Can anyone help me to solve this issue?
Make one custom file in drawable folder.
mybackground.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false" android:drawable="#color/yourcolorName" />
<item android:drawable="#android:color/transparent" />
</selector>
Now set this file to your Spinner.
<Spinner
android:id="#+id/ics_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_alignParentTop="true"
android:background="#drawable/mybackground"
/>

Display Image and text with gridview or galary in 1 row and multiple column with horizontal scrollbar

I am working on a application which require image and text group together in horizontal scroll bar.
I have tried few things but i am unable to get this, can anyone guide me with this guys.
Here is what i a have done,
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">
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="horizontal" >
<GridView
android:layout_height="wrap_content"
android:id="#+id/gridView1"
android:layout_width="wrap_content"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:scrollbars="horizontal"
android:verticalSpacing="10dp">
</GridView>
</HorizontalScrollView>
</LinearLayout>
gridview_row.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/border"
android:padding="5dp">
<ImageView
android:layout_height="64dp"
android:id="#+id/imageView1"
android:layout_width="64dp"
android:src="#drawable/icon"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
</ImageView>
<TextView
android:text="TextView"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_marginTop="2dp"
android:layout_centerHorizontal="true"
android:textSize="18sp"></TextView>
</RelativeLayout>
GridviewExampleActivity.java
package com.paresh.gridviewexample;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class GridViewExampleActivity extends Activity {
/** Called when the activity is first created. */
private GridviewAdapter mAdapter;
private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;
private GridView gridView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
prepareList();
// prepared arraylist and passed it to the Adapter class
mAdapter = new GridviewAdapter(this,listCountry, listFlag);
// Set custom adapter to gridview
gridView = (GridView) findViewById(R.id.gridView1);
gridView.setAdapter(mAdapter);
// Implement On Item click listener
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(GridViewExampleActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
}
public void prepareList()
{
listCountry = new ArrayList<String>();
listCountry.add("india");
listCountry.add("Brazil");
listCountry.add("Canada");
listCountry.add("China");
listCountry.add("France");
listCountry.add("Germany");
listCountry.add("Iran");
listCountry.add("Italy");
listCountry.add("Japan");
listCountry.add("Korea");
listCountry.add("Mexico");
listCountry.add("Netherlands");
listCountry.add("Portugal");
listCountry.add("Russia");
listCountry.add("Saudi Arabia");
listCountry.add("Spain");
listCountry.add("Turkey");
listCountry.add("United Kingdom");
listCountry.add("United States");
listFlag = new ArrayList<Integer>();
listFlag.add(R.drawable.india);
listFlag.add(R.drawable.brazil);
listFlag.add(R.drawable.canada);
listFlag.add(R.drawable.china);
listFlag.add(R.drawable.france);
listFlag.add(R.drawable.germany);
listFlag.add(R.drawable.iran);
listFlag.add(R.drawable.italy);
listFlag.add(R.drawable.japan);
listFlag.add(R.drawable.korea);
listFlag.add(R.drawable.mexico);
listFlag.add(R.drawable.netherlands);
listFlag.add(R.drawable.portugal);
listFlag.add(R.drawable.russia);
listFlag.add(R.drawable.saudi_arabia);
listFlag.add(R.drawable.spain);
listFlag.add(R.drawable.turkey);
listFlag.add(R.drawable.united_kingdom);
listFlag.add(R.drawable.united_states);
}
}
GridviewAdapter.java
package com.paresh.gridviewexample;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridviewAdapter extends BaseAdapter
{
private ArrayList<String> listCountry;
private ArrayList<Integer> listFlag;
private Activity activity;
public GridviewAdapter(Activity activity,ArrayList<String> listCountry, ArrayList<Integer> listFlag) {
super();
this.listCountry = listCountry;
this.listFlag = listFlag;
this.activity = activity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return listCountry.size();
}
#Override
public String getItem(int position) {
// TODO Auto-generated method stub
return listCountry.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public static class ViewHolder
{
public ImageView imgViewFlag;
public TextView txtViewTitle;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
LayoutInflater inflator = activity.getLayoutInflater();
if(convertView==null)
{
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridview_row, null);
view.txtViewTitle = (TextView) convertView.findViewById(R.id.textView1);
view.imgViewFlag = (ImageView) convertView.findViewById(R.id.imageView1);
convertView.setTag(view);
}
else
{
view = (ViewHolder) convertView.getTag();
}
view.txtViewTitle.setText(listCountry.get(position));
view.imgViewFlag.setImageResource(listFlag.get(position));
return convertView;
}
}
My ouput should be
------------ ---------- ------------ ----
Image Image Image Image Image Image Image Image Image Image Image
Text Text Text Text Text Text Text Text Text Text Text
------------ ---------- ------------ --
Please Help me guys
Hi there just have a look at CustomArrayAdapter used for ListViews and Gridviews especially point 10 at this Tutoiral there you get to know how to customize your GridView/ListView (handled same way)
This might also help
If you have problems with the hotrizontal way, try this project: https://github.com/jess-anders/two-way-gridview

Android listview behaving randomly

I am facing problem with listview i-e i am inflating some view on ListViewItem in runtime which results in increase in the ListView Row,i-e some rows in listview have different heights and others have different which is giving wrong getView position.
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.app.Activity;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
TestAdapter adapter = new TestAdapter(this);
listView.setAdapter(adapter);
}
public void onClickAction(View v){
v.findViewById(R.id.l1).setVisibility(View.VISIBLE);
}
}
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class TestAdapter extends BaseAdapter {
private String[] items = {"a","b","c","d","e","f","g","h","i"};
private Context context;
public TestAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items[position];
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
ViewHolder holder;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.list_view_item, null);
holder = new ViewHolder();
holder.textView = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText("value = "+position);
return convertView;
}
static class ViewHolder{
TextView textView;
}
}
<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/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
<?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"
android:id="#+id/mainLayout"
android:onClick="onClickAction"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:id="#+id/l1"
android:layout_width="fill_parent"
android:layout_height="250dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:visibility="gone"
android:background="#android:color/darker_gray"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>

Get index if checked rows in this customized listView?

Hi i am using the following code to generate a custom listview with checkbox.how can i get the index of the checked rows when the user clicks on the button?
package com.CustomListView;
import android.app.Activity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class CustomListViewActivity extends Activity {
ListView mListview;
Button btn;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListview=(ListView)findViewById(R.id.listview);
mListview.setAdapter(new mCustomList(this));
btn =(Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
public class mCustomList extends BaseAdapter{
private Context mContext;
public mCustomList(Context c){
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return COUNTRIES.length;
}
#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 converView, ViewGroup parent) {
View List;
if(converView==null){
List=new View(mContext);
LayoutInflater mLayoutinflater=getLayoutInflater();
List=mLayoutinflater.inflate(R.layout.mylist, parent, false);
}
else{
List = (View)converView;
}
ImageView imageView = (ImageView)List.findViewById(R.id.imgview);
TextView textView = (TextView)List.findViewById(R.id.text);
CheckBox chkbox=(CheckBox)List.findViewById(R.id.chkbox);
textView.setText(COUNTRIES[position]);
// TODO Auto-generated method stub
return List;
}
}
static final String[] COUNTRIES = new String[] {
"Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
"Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
};
}
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"
>
<ListView
android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="300dp"
android:scrollbars="none"
></ListView>
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
mylist.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"
>
<TextView android:id="#+id/text" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="#099900" />
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:scaleType="center"
android:id="#+id/imgview" android:src="#drawable/icon" android:layout_centerInParent="true" />
<CheckBox android:id="#+id/chkbox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:checked="true" android:layout_alignParentRight="true" android:layout_marginRight="10dip" />
</RelativeLayout>
first change your getView because findViewById is called for every view which is wrong.
you have a boolean array with false set for every item in the list. Than at every change in checked state of your check box, you change the corresponding boolean to true/false(checked/unchecked). Than
if(converView==null){
List=new View(mContext);
LayoutInflater mLayoutinflater=getLayoutInflater();
List=mLayoutinflater.inflate(R.layout.mylist, parent, false);
ImageView imageView = (ImageView)List.findViewById(R.id.imgview);
TextView textView = (TextView)List.findViewById(R.id.text);
CheckBox chkbox=(CheckBox)List.findViewById(R.id.chkbox);
chkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkedArray[position] = isChecked;
//process your array. do something with indexes that correspond to a true value.
}
}
}
});
} else{
List = (View)converView;
}

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