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
Related
I have created a listview with externe layout like tweet and I want to set button clickable into all of the adapter like the X in photo:
Example when I click in button if I'm admin I delete the post
This is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
final ArrayList<HashMap<String, String>> listItem = new ArrayList<>();
FirebaseFirestore db = FirebaseFirestore.getInstance();
HashMap<String, String> map = new HashMap<>();
map.put("module", "Example");
map.put("years", "Year");
map.put("sections", "Section");
map.put("groupe", "Groupe");
map.put("urlImage",("Image"));
map.put("date","example");
listItem.add(map);
SimpleAdapter mSchedule = new SimpleAdapter
(getBaseContext(), listItem, R.layout.affichage,
new String[]{"avatar", "module", "years", "sections", "groupe"},
new int[]{R.id.avatar, R.id.module, R.id.years, R.id.sections, R.id.groupe});
ListView.setAdapter(mSchedule);
}
You have to implement CustomAdapter with BaseAdapter as base class.
You can check this link https://guides.codepath.com/android/Using-a-BaseAdapter-with-ListView for implementation details
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.
I am using a custom list view through the following code
public class details extends ListActivity {
/** Called when the activity is first created. */
Bundle extras;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
extras=getIntent().getExtras();
SimpleAdapter adapter = new SimpleAdapter(
this,
list,
R.layout.custom_row_view,
new String[] {"name","use"},
new int[] {R.id.text1,R.id.text2}
);
populateList();
setListAdapter(adapter);
}
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
private void populateList()
{
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("name","NAME");
temp1.put("use",extras.getString("name"));
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("name","CHANGE IN PRICE");
temp2.put("use",extras.getString("change"));
TextView txt=(TextView)findViewById(R.id.text2);
double k=Double.parseDouble(extras.getString("change"));
if(k<0)
{
txt.setTextColor(Color.RED);
}
else
{
txt.setTextColor(Color.RED);
}
list.add(temp2);
}
}
I would like to set the color of the textview to green if change in price is grater than 0 else i want it to be red in color .The following code throws a null pointer exception at the setTextColor(). How exactly do i dothis
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.
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