ListView widget android - 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);

Related

add subitems in listview in android

I have a listvew and I add subitem into the listview like this code
public class MyCustomListView extends Activity {
/** Called when the activity is first created. */
ListView lv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
lv=(ListView) this.findViewById(R.id.lvvv);
SimpleAdapter adapter = new SimpleAdapter(this,list, R.layout.custom_row_view,new String[] {"pen","color"},
new int[] {R.id.text1, R.id.text3}
);
populateList();
lv.setAdapter(adapter);
}
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
private void populateList() {
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("pen","MONT Blanc");
temp.put("color", "Black");
list.add(temp);
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("pen","Gucci");
temp1.put("color", "Red");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("pen","Parker");
temp2.put("color", "Blue");
}
}
but in the color key, if I have a lot of colors for a pen, and I want to arrange the colors into a list and this color list is under the pen.
ex:
-pen: Parker.
+color: blue.
+color: red.
how should I do that? please help me!
thank alot.
You need to use an ExpanadableListView. Check out this -
ExpanadaleListView Tutorial

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.

How to add Image to the Custom List View in Android?

I have a custom list view with image view and text view.I am loading text view data from SQLite Database and I want to add image from my drawable folder to the image view contain in list based on some condition
I am using cursor to load data to the list view.Please any one guide me how to do this
Very simple solution for that,using a simple adapter.
public class TestList extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.list);
ArrayList<Map<String, String>> arrlist = new ArrayList<Map<String, String>>();
Map<String, String> m = null;
for (int i = 0; i <=50; i++) {
m = new HashMap<String, String>();
m.put("key", String.valueOf(R.drawable.ic_launcher));
m.put("title", "Title-" + i);
m.put("subtitle", "SubTitle-" + i);
arrlist.add(m);
}
SimpleAdapter adapter = new SimpleAdapter(this, arrlist,
R.layout.sample, new String[] { "key", "title", "subtitle" },
new int[] { R.id.imageView1, R.id.title, R.id.subtitle });
list.setAdapter(adapter);
}
}
Xml layouts: main xml layout has a listview with id-list,and sample.xml is your inflating layout,it has one image,two textviews.Modify the code as your requirement.

repeating ListView

Hey guys I am a working on displaying a list of items on listview, the code I am using is
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
ListView lv= (ListView)findViewById(R.id.listview);
rtrnList = new ArrayList<HashMap<String,String>>();
getmyLocation();
listclass = new listClass(offersobj);
listclass.populate();
rtrnList = listclass.getListArray();
adapter = new SimpleAdapter(
this,
rtrnList,
R.layout.custom_row_view,
new String[] {"Name","Msg","time"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
lv.setAdapter(adapter);
}
problem is say I am displaying three names Avinash, Arun, Rajesh. When application starts these three names are displayed on list. When I close and again start the application the values are repeating Avinash, Arun, Rajesh,Avinash, Arun, Rajesh. I am not able to figure out how to solve this.
The code you show seems fine. My guess is that listclass.populate() modifies offersobj and that offersobj is reused over several creations of your activity. So, whenever the activity is created, additional data is populated.
public class ListViewA extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv= (ListView)findViewById(R.id.listview);
// create the grid item mapping
String[] from = new String[] {"rowid", "col_1", "col_2", "col_3"};
int[] to = new int[] { R.id.item1, R.id.item2, R.id.item3, R.id.item4 };
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < 10; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("rowid", "" + i);
map.put("col_1", "col_1_item_" + i);
map.put("col_2", "col_2_item_" + i);
map.put("col_3", "col_3_item_" + i);
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.grid_item, from, to);
lv.setAdapter(adapter);
}
}
for more example see this linkthis alsolistview about adapter, for create a hashmap, why bitmap type imported cannot show image in listview?What adapter shall I use to use HashMap in a ListView

calling listactiviy from tabactivity in 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
}
});

Categories

Resources