So, here we extends simple Activity not ListActivity.
Here i am giving the entire work of my Dynamic List with Button, where we get the event listener of each button from android:onClick="myClickHandler" in one of the layout file. From this event we can get the selected value and pass to other intents.
Here is the program....if you find any concerns or updates let me know will update accordingly....
package com.jit;
/*
* Vinay Guntaka
* Desc: Dynamic List with buttons and event listener of each buttons.
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListViewButtons extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.btnOptionList);
List<HashMap<String, String>> myOptList = getOptionList(EnumTypes.OPTION);
SimpleAdapter optionList = new SimpleAdapter(getBaseContext(),myOptList,R.layout.btnstablerows,
new String[]{
"optnCard"
},new int[]{
R.id.btnAddOptn
});
listView.setAdapter(optionList);
listView.setTextFilterEnabled(true);
}
/* myClickHandler() is defined in btnstablerows.xml for
* onClick event android:onClick="myClickHandler".
*/
public void myClickHandler(View v)
{
LinearLayout vwParentRow = (LinearLayout)v.getParent();
/*
* Based on the row of each field the values of <td> can be
* get based on the field type and field location
* just replace with 0,1,2,.... of getChildAt();
* get the 2nd child of our ParentRow (remember in java that arrays start with zero,
* so our 2nd child has an index of 1)
*/
Button btnChild = (Button)vwParentRow.getChildAt(0);
Toast.makeText(getBaseContext(), btnChild.getText(), Toast.LENGTH_LONG).show();
int c = Color.CYAN;
vwParentRow.setBackgroundColor(c);
vwParentRow.refreshDrawableState();
Toast.makeText(getBaseContext(), "Hey event fired", Toast.LENGTH_LONG).show();
}
private ArrayList<HashMap<String, String>> getOptionList(EnumTypes type){
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
switch (type) {
case OPTION:
map.put("optnCard", "JAVA");
mylist.add(map);
map = new HashMap<String, String>();
map.put("optnCard", "Eclipse Galileo");
mylist.add(map);
map = new HashMap<String, String>();
map.put("optnCard", "Android 2.2");
mylist.add(map);
map = new HashMap<String, String>();
map.put("optnCard", "Simulator Android");
mylist.add(map);
break;
default:
break;
}
return mylist;
}
public enum EnumTypes{
OPTION
}
}
/*
* Vinay Guntaka
* Desc: Dynamic List with buttons and event listener of each buttons.
*/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class ListViewButtons extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView)findViewById(R.id.btnOptionList);
List<HashMap<String, String>> myOptList = getOptionList(EnumTypes.OPTION);
SimpleAdapter optionList = new SimpleAdapter(getBaseContext(),myOptList,R.layout.btnstablerows,
new String[]{
"optnCard"
},new int[]{
R.id.btnAddOptn
});
listView.setAdapter(optionList);
listView.setTextFilterEnabled(true);
}
/* myClickHandler() is defined in btnstablerows.xml for
* onClick event android:onClick="myClickHandler".
*/
public void myClickHandler(View v)
{
LinearLayout vwParentRow = (LinearLayout)v.getParent();
/*
* Based on the row of each field the values of <td> can be
* get based on the field type and field location
* just replace with 0,1,2,.... of getChildAt();
* get the 2nd child of our ParentRow (remember in java that arrays start with zero,
* so our 2nd child has an index of 1)
*/
Button btnChild = (Button)vwParentRow.getChildAt(0);
Toast.makeText(getBaseContext(), btnChild.getText(), Toast.LENGTH_LONG).show();
int c = Color.CYAN;
vwParentRow.setBackgroundColor(c);
vwParentRow.refreshDrawableState();
Toast.makeText(getBaseContext(), "Hey event fired", Toast.LENGTH_LONG).show();
}
private ArrayList<HashMap<String, String>> getOptionList(EnumTypes type){
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
switch (type) {
case OPTION:
map.put("optnCard", "JAVA");
mylist.add(map);
map = new HashMap<String, String>();
map.put("optnCard", "Eclipse Galileo");
mylist.add(map);
map = new HashMap<String, String>();
map.put("optnCard", "Android 2.2");
mylist.add(map);
map = new HashMap<String, String>();
map.put("optnCard", "Simulator Android");
mylist.add(map);
break;
default:
break;
}
return mylist;
}
public enum EnumTypes{
OPTION
}
}
-----------------
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/btnOptionList"
android:layout_width="200px" android:layout_height="wrap_content"
android:drawSelectorOnTop="false" android:paddingLeft="35px"
android:paddingTop="10px" android:clickable="true" android:scrollbars="none"
>
</ListView>
</LinearLayout>
<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/btnOptionList"
android:layout_width="200px" android:layout_height="wrap_content"
android:drawSelectorOnTop="false" android:paddingLeft="35px"
android:paddingTop="10px" android:clickable="true" android:scrollbars="none"
>
</ListView>
</LinearLayout>
----------------
btnstablerows.xml
--------------
<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip" android:paddingBottom="6dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<Button android:id="@+id/btnAddOptn" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:clickable="true" />
</LinearLayout>
<!-- row.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingTop="4dip" android:paddingBottom="6dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<Button android:id="@+id/btnAddOptn" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:clickable="true" />
</LinearLayout>
Hey.., I am a newbie to Android World
So please have a look at my Program and let me know if any thing missing
Thanks
Dear sir,
ReplyDeletecan we use images instead of text with list?
Hi, just wanted to say this was exactly the kind of behavior I was looking to put in my app. Good Job!
ReplyDeleteSteve
Hi!
ReplyDeleteIf say I click the button with the text "JAVA" and I want it to go to another xml layout, how would do it?
Thanks!