calling listactiviy from tabactivity in android - android

Is it possible to call listactivity through tab activity? Basically, I am developing an app with 3 tabs, for which I am using tabactivity. Furthermore, in one of the tabs I want a listview, so I have derived from listactivity.
Now I want the click event to be determined in the listview. Am i missing something?
public class Tabissue extends TabActivity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TabHost host = getTabHost();
host.addTab(host.newTabSpec("Tab1").setIndicator("Tab1").setContent(new Intent(this,Tab1.class)));
host.addTab(host.newTabSpec("Tab2").setIndicator("Tab2").setContent(new Intent(this,Tab2.class)));
host.setCurrentTab(1);
}
}
Tab1 Class
public class Tab2 extends ListActivity
{
ListView list;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("Test1","####");
map.put("Test2", "India");
map.put("Time", "India time");
mylist.add(map);
map = new HashMap<String, String>();
map.put("Test1", "####");
map.put("Test2", "US");
map.put("Time","US time");
mylist.add(map);
map = new HashMap<String, String>();
map.put("Test1", "####");
map.put("Test2", "UK");
map.put("Time", "UK Time");
mylist.add(map);
ListAdapter mSchedule = new SimpleAdapter( this,
mylist,
R.layout.row,
new String[]
{
"India",
"US",
"UK"
},
new int[]
{
R.id.TRAIN_CELL,
R.id.FROM_CELL,
R.id.TO_CELL,
}
);
list.setAdapter(mSchedule);
}
}

In your ListActivity set onItemClickListener:
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> av, View v, int position,
long id) {
// Do your stuff here
}
});

Related

ListView widget android

I'm working on android application that shows number and name of persons , using ListView .But the compilers doesn't recognize "champs1" and "champs2".
public class Second extends Activity {
ListView vue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
vue=(ListView) findViewById(R.id.listV2);
String [][] rep=new String[][]{{"1","aaaaaaa" },
{"2","bbbbbbb" },
{ "3","ccccccc" } };
List<HashMap<String,String> > list= new ArrayList<HashMap<String, String>>();
HashMap<String,String> element;
for(int i=0 ;i<rep.length;i++ ){
element =new HashMap<String, String>();
element.put("chmaps1",rep[i][0]);
element.put("champs2",rep[i][1]);
list.add(element);
}
ListAdapter adapter=new SimpleAdapter(this,
list,
android.R.layout.simple_list_item_2,
new String[] {"chmaps1","champs2"},
new int[] {android.R.id.champs1, android.R.id.champs2 });
vue.setAdapter(adapter);
}
}
Error:(39, 71) error: cannot find symbol variable champs1
Error:(39, 71) error: cannot find symbol variable champs2
change android.R.id.champs1 to android.R.id.text1
change android.R.id.champs2 to android.R.id.text2
public class Second extends Activity {
ListView vue;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_searchable);
vue = (ListView) findViewById(R.id.listView1);
String[][] rep = new String[][] { { "1", "aaaaaaa" },
{ "2", "bbbbbbb" }, { "3", "ccccccc" } };
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String, String> element;
for (int i = 0; i < rep.length; i++) {
element = new HashMap<String, String>();
element.put("chmaps1", rep[i][0]);
element.put("champs2", rep[i][1]);
list.add(element);
}
ListAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, new String[] { "chmaps1",
"champs2" }, new int[] { android.R.id.text1,
android.R.id.text2 });
vue.setAdapter(adapter);
}
}
You are refferring to:
android.R.id.champs1
instead of:
R.id.champs1
Also be careful that you ar calling variables chmaps1 sometimes and champs1 some other times.
put this code in for loop
HashMap<String, String> element = new HashMap<String, String>();
and there is no problem i think android.R.id.champs1 or R.id.champs1 both are correct .
change this one instead of this vue.setAdapter(adapter)
try like this vue.setListAdapter(adapter);

setAdapter() returning null within ListFragment

So I'm trying to make a basic foreground for another app I'm making.
I'm just trying to get a listView to populate on screen within my fragment. (To create swipe-able tab views)
I keep getting a nullPointerException on this line here:
List.setAdapter(Adapter);
Here's my entire class:
public class List_View extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listviewtest, container, false);
ListView List = (ListView) getActivity().findViewById(R.id.listView1);
List<HashMap<String, String>> Data = new ArrayList<HashMap<String, String>>();
{
HashMap<String, String> temp1 = new HashMap<String, String>();
temp1.put("Item", "Item 1");
Data.add(temp1);
HashMap<String, String> temp2 = new HashMap<String, String>();
temp2.put("Item", "Item 2");
Data.add(temp2);
HashMap<String, String> temp3 = new HashMap<String, String>();
temp3.put("Item", "Item 3");
Data.add(temp3);
}
SimpleAdapter Adapter = new SimpleAdapter(this.getActivity(),
Data, R.layout.custom_row_view, new String[] {
"Item" }, new int[] { R.id.text1});
List.setAdapter(Adapter);
List.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 1:
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(
getActivity());
dlgAlert.setMessage("Set Message Text");
dlgAlert.setTitle("Set Message Title");
dlgAlert.setNeutralButton("View info",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
dialog.dismiss();
}
});
dlgAlert.setCancelable(false);
dlgAlert.create().show();
break;
}
}
});
return rootView;
}
}
So far as I can tell this shouldn't be returning null..
I'm thinking your inflate operation is returning null. Check this line
ListView List = (ListView) getActivity().findViewById(R.id.listView1);
You should be inflating your ListView from your parent view of your fragment, not from the parent activity.
Should be something like this instead
ListView List = (ListView) rootView.findViewById(R.id.listView1); and make sure that

Enable Phone Call in ListView

After lot of research over internet i could not find a answer how to enable a phone call in a list view.
here is my sample code:
Suppose i have a list of 50 hospital with there phone contact detail
public class Medical extends ListActivity {
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bus_main);
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.rowview,
new String[] {"title","address","phone"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
populateList();
setListAdapter(adapter);
}
private void populateList() {
HashMap<String,String>
map = new HashMap<String,String>();
map.put("title","UHS Hospital");
map.put("address", "Street 54");
map.put("phone", "4077000");
list.add(map);
map = new HashMap<String,String>();
map.put("title","Calvary Hospital");
map.put("address", "Street 43");
map.put("phone", "2362491");
list.add(map);
}
}
How to enable phone call to all listview entry based on individual Phone number detail (text3) of a hospital.
add this method in your activity:
public class Medical extends ListActivity {
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.rowview,
new String[] {"title","address","phone"},
new int[] {R.id.textView1,R.id.textView2, R.id.textView3}
);
populateList();
setListAdapter(adapter);
}
protected void onListItemClick (ListView l, View v, int position, long id){
super.onListItemClick(l,v,position,id);
if(position>=0 && position<list.size()) {
HashMap<String, String> tmp = list.get(position);
if(tmp.containsKey("phone")) {
String tel = tmp.get("phone");
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+tel));
startActivity(callIntent);
}
}
}
private void populateList() {
HashMap<String,String>
map = new HashMap<String,String>();
map.put("title","UHS Hospital");
map.put("address", "Street 54");
map.put("phone", "4077000");
list.add(map);
map = new HashMap<String,String>();
map.put("title","Calvary Hospital");
map.put("address", "Street 43");
map.put("phone", "2362491");
list.add(map);
}
}
and add
<uses-permission android:name="android.permission.CALL_PHONE" />
in your manifest
Attention This code does not check if the number is valid.

Extending Fragments with listactivity

I am working with fragments in android,am extending android.app.ListFragment to display listview, but it takes only one item in a list view. I want to display 2 items in a list view ,its possible with extending ListActivity class, but i want to extend both fragment and listactivty.
Refer this image
public class ListFragmentnewforel extends android.app.ListFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<HashMap<String, String>> list = buildData();
String[] from = { "purpose" , "name","desc", "num"};
int[] to = { R.id.detailsText1, R.id.detailsText2, R.id.detailsText3, };
SimpleAdapter adapter = new SimpleAdapter(this, list,R.layout.mylistview,from,to);
setListAdapter(adapter);
}
public void onListItemClick(ListView l, View v, int position, long id) {
ArrayList<String>arr=new ArrayList<String>();
String item = (String) getListAdapter().getItem(position);
DetailFragment fragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detailFragment);
if (getResources().getConfiguration().orientation ==
Configuration.ORIENTATION_LANDSCAPE) {
fragment.setText(item);
}
else {
Intent intent = new Intent(getActivity().getApplicationContext(),
DetailActivity.class);
Xmlparsingactiforele d=new Xmlparsingactiforele();
arr=d.myelarraylist(item);
// arr.get(0);
intent.putExtra("value1", arr.get(0));
intent.putExtra("value2", arr.get(1));
intent.putExtra("value3", arr.get(2));
intent.putExtra("value4", arr.get(3));
startActivity(intent);
}
}
private ArrayList<HashMap<String, String>> buildData() {
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
list.add(putData("Android", "Mobile","two", "1"));
list.add(putData("Windows7", "Windows7","one", "2"));
list.add(putData("iPhone", "iPhone","three", "3"));
return list;
}
private HashMap<String, String> putData(String name, String purpose,String a, String n) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
item.put("desc", a);
item.put("num", n);
return item;
}
}
There is nothing stopping you from showing whatever you want in the ListView item. Please read the API documentation of ListFragment.
You can create a ListAdapter that returns the View of the type you want in its getView method. There is hardly any difference between ListActivity and ListFragment in this regard.
Extend both fragment and Activity classes to single Class not possible. So, extends Activity to main class and create inner class and extends Fragment class to in it.

Using simple_list_item_2 and can't figure out how to use setOnItemClickListener

Please excuse me, I'm new:new at this. I use a simple_list_item_2 to display 11 items. These 11 items have been loaded by using HashMap and then SimpleAdapter. This works fine in displaying everything. The problem is that I cannot get setOnItemClickListener going. The code:
public class TwoLineActivity extends ListActivity
{
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(2);
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.scrolllist);
// enter valid data, these 2 are the same as the remaining 9
HashMap<String, String> maplist;
maplist = new HashMap<String, String>();
maplist.put("line1", "a11 data");
maplist.put("line2", "asd asd ad 1234569780");
list.add(maplist);
maplist = new HashMap<String, String>();
maplist.put("line1", "a12 data");
maplist.put("line2", "asd asd ad 1234569781");
list.add(maplist);
String[] from = { "line1", "line2" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list, android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);`
So up to here things are great, I get my list. Now I want to be able to select an item from the list, so I coded the next 2 lines
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener()
{ .....
I get the following errors
The method setAdapter(SimpleAdapter) is undefined for the type ArrayList<HashMap<String,String>>
and
The method setOnItemClickListener(new AdapterView.OnItemClickListener(){}) is undefined for the type ArrayList<HashMap<String,String>>
If your activity extends ListActivity, you should override
protected void onListItemClick(ListView l, View v, int position, long id) {
}

Categories

Resources