i want to inflate a expandable list with some different text and different images on each row if i use albumCovers.get(i) instead of getResources().getDrawable(R.drawable.icon)then it throws an error, any one help us out? thanks ;p.
public class ExpandActivity extends ExpandableListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Construct Expandable List
final String NAME = "name";
final String IMAGE = "image";
final LayoutInflater layoutInflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final ArrayList<HashMap<String, String>> headerData = new ArrayList<HashMap<String, String>>();
final HashMap<String, String> group1 = new HashMap<String, String>();
group1.put(NAME, "Group 1");
headerData.add(group1);
final ArrayList<ArrayList<HashMap<String, Object>>> childData = new ArrayList<ArrayList<HashMap<String, Object>>>();
final ArrayList<HashMap<String, Object>> group1data = new ArrayList<HashMap<String, Object>>();
childData.add(group1data);
Resources res = this.getResources();
Drawable photo = (Drawable) res.getDrawable(R.drawable.icon);
ArrayList<Drawable> albumCovers = new ArrayList<Drawable>();
albumCovers.add(photo);
// Set up some sample data in both groups
for (int i = 0; i < 10; ++i) {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put(NAME, "Child " + i);
map.put(IMAGE, getResources().getDrawable(R.drawable.icon));
(group1data).add(map);
}
setListAdapter(new SimpleExpandableListAdapter(this, headerData,
android.R.layout.simple_expandable_list_item_1,
new String[] { NAME }, // the name of the field data
new int[] { android.R.id.text1 }, // the text field to populate
// with the field data
childData, 0, null, new int[] {}) {
#Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final View v = super.getChildView(groupPosition, childPosition,
isLastChild, convertView, parent);
// Populate your custom view here
((TextView) v.findViewById(R.id.name))
.setText((String) ((Map<String, Object>) getChild(
groupPosition, childPosition)).get(NAME));
((ImageView) v.findViewById(R.id.image))
.setImageDrawable((Drawable) ((Map<String, Object>) getChild(
groupPosition, childPosition)).get(IMAGE));
return v;
}
#Override
public View newChildView(boolean isLastChild, ViewGroup parent) {
return layoutInflater.inflate(
R.layout.expandable_list_item_with_image, null, false);
}
});
}
}
You have only one drawable in albumCovers but loop it ten times in the for loop and try to access it by the index. Change albumCovers.get(i) to albumCovers.get(0) for it to work.
Here is the key location:
Drawable photo = (Drawable) res.getDrawable(R.drawable.icon);
ArrayList<Drawable> albumCovers = new ArrayList<Drawable>();
albumCovers.add(photo); // HERE: photo added only once!
for (int i = 0; i < 10; ++i) {
final HashMap<String, Object> map = new HashMap<String, Object>();
map.put(NAME, "Child " + i);
map.put(IMAGE, getResources().getDrawable(R.drawable.icon)); // HERE: you wanted to use albumCovers.get(i) here. There is only 1 photo in albumCovers but you try to get images 0...9 from it using index i. So, change this to albumCovers.get(0) OR add 10x photo into albumCovers.
(group1data).add(map);
}
Related
I would like to use Picasso library to download and show images that are in URL's inside JSON data from my server.
I've lost all hope, I saw something about custom adapter but to no avail.
I need help here.
Here's how I do stuff now to show data from json inside onPostExecute:
protected void onPostExecute(JSONObject objdanejson){
pDialog.dismiss();
try {
android = objdanejson.getJSONArray(TAG_ZAWARTOSC);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
final String akt_tytul = c.getString(TAG_TYTUL);
final String akt_skrot = c.getString(TAG_SKROT);
final String akt_tresc = c.getString(TAG_TRESC);
final String akt_id = c.getString(TAG_ID);
final String akt_IMG = c.getString(TAG_IMG);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TYTUL, akt_tytul);
map.put(TAG_SKROT, akt_skrot);
map.put(TAG_ID, akt_id);
map.put(TAG_TRESC, akt_tresc);
map.put("http://www.apirest.poligon.webimpuls.pl/"+TAG_IMG, akt_IMG);
oslist.add(map);
lista_aktualnosci = (ListView)findViewById(R.id.lista_aktualnosci);
final ListAdapter adapter = new SimpleAdapter(aktualnosci.this, oslist,
R.layout.aktualnosc_item,
new String[]{TAG_TYTUL, TAG_SKROT, TAG_IMG}, new int[]{R.id.aktTytul, R.id.aktSkrot, R.id.aktIMG});
lista_aktualnosci.setAdapter(adapter);
lista_aktualnosci.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(aktualnosci.this, "Kliknąłeś na " + oslist.get(+position).get("akt_tytul"),Toast.LENGTH_SHORT).show();
Intent czytaj = new Intent(aktualnosci.this, aktualnosc_czytaj.class);
czytaj.putExtra("tytuł",oslist.get(+position).get("akt_tytul"));
czytaj.putExtra("tresc",oslist.get(+position).get("akt_tresc"));
czytaj.putExtra("id",oslist.get(+position).get("akt_id"));
startActivity(czytaj);
}
});
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
EDIT:
How my custom controler looks:
oslist.add(map);
lista_aktualnosci = (ListView)findViewById(R.id.lista_aktualnosci);
/* final ListAdapter adapter = new SimpleAdapter(aktualnosci.this, oslist,
R.layout.aktualnosc_item,
new String[]{TAG_TYTUL, TAG_SKROT}, new int[]{R.id.aktTytul, R.id.aktSkrot});
*/
class ListAdapter extends ArrayAdapter<ClipData.Item> {
public ListAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
}
public ListAdapter(Context context, int resource, List<ClipData.Item> items) {
super(context, resource, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi;
vi = LayoutInflater.from(getContext());
v = vi.inflate(R.layout.aktualnosc_item, null);
}
ClipData.Item p = getItem(position);
if (p != null) {
aktTytul = (TextView)findViewById(R.id.aktTytul);
aktSkrot = (TextView)findViewById(R.id.aktSkrot);
aktIMG = (ImageView)findViewById(R.id.aktIMG);
}
return v;
}
}
lista_aktualnosci.setAdapter(adapter);
Create a class that extends Simple Adapter or Base Adapter. In getView() the adapter will ask you to create a list item when this is needed. As you create the item you can use Picasso to display the image in whatever image view you've selected for this purpose.
That being said you should ditch ListView and use RecyclerView instead - for which you can find all the information you need here
Other stuff that may improve your code are things like Retrofit and Gson.
Does that help?
you can get your image from listview. You can this code your activity after set adapter then you can set your imageview.
for(int i = 0; i < this.listView.getChildCount(); i ++) {
View view = this.listView.getChildAt(i);
ImageView img= (ImageView)view.findViewById(R.id.yourimage);
//do something your image
}
I am using BaseAdapter to bind Custom ListView. And I am adding Items dynamically. Each Item contains TextView, EditText, Spinner. It is working fine. But now issue is that if user selects any value from Spinner, add some data in EditText and then if he/she adds new item then it clears all values of filled up data.
So, I want to prevent this refresh of already added Items when add new Item. How to do this ?
My Code :
ArrayList<HashMap<String, String>> aaName = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = null;
map = new HashMap<String, String>();
map.put("Name", Name value);
aaName.add(map);
NameAdapter nameAdapter = new NameAdapter(getActivity(), aaName);
lvName.setAdapter(nameAdapter);
public class NameAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.chiefcomment_view, null);
TextView tvName = (TextView) vi.findViewById(R.id.tvName);
Spinner spnTime = (Spinner) vi.findViewById(R.id.spnTime);
EditText etSpecification = (EditText) vi.findViewById(R.id.etSpecification);
HashMap<String, String> name = new HashMap<String, String>();
name = data.get(position);
tvName.setText(name.get("Name"));
// Time Spinner...
ArrayList<String> Time = new ArrayList<String>();
for (int i = 1; i <= 100; i++) {
Time.add(String.valueOf(i));
}
aaTime = new ArrayAdapter<String>(activity, R.layout.small_spinner_view, Time);
spnTime.setAdapter(aaTime);
return vi;
}
}
I'm creating a ListView that contains two TextViews and an ImageView.
I'm passing data to my SimpleAdapter using a Hashmap, so I convert the ids of the images to a string and then later in the SimpleAdapter I convert them back to integers to set the image resource.
However that doesn't seem to work.
The relevant code is:
The oncreate of my activity
clubImages = new Integer[] {
R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5
};
ListView lv = (ListView)findViewById(android.R.id.list);
// create the grid item mapping
String[] from = new String[] {"image"};
int[] to = new int[] {R.id.club_image};
// prepare the list of all records
List<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, String> map = new HashMap<String, String>();
map.put("image", getString(clubImages[i]));
fillMaps.add(map);
}
// fill in the grid_item layout
SimpleAdapter adapter = new MySimpleAdapter(this, fillMaps, R.layout.eventview_row, from, to);
lv.setAdapter(adapter);
the simpleadapter class
public class MySimpleAdapter extends SimpleAdapter {
private List<HashMap<String, String>> results;
public MySimpleAdapter(Context context, List<HashMap<String, String>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
this.results = data;
}
public View getView(int position, View view, ViewGroup parent){
Typeface type = Typeface.createFromAsset(getAssets(), "fonts/aircruiser.ttf");
View v = view;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.eventview_row, null);
}
mClubImageImageView = (ImageView) v.findViewById(R.id.club_image);
int ClubImageHelper = Integer.parseInt(results.get(position).get("image").toString());
mClubImageImageView.setImageResource(ClubImageHelper);
return v;
}
}
The error in my logcat
E/AndroidRuntime(19406): java.lang.NumberFormatException: unable to parse 'res/drawable-hdpi/pic1.png' as integer
You can't pass R.drawable.pic1 as a String and then parse it back as an Integer. This will not work, instead simply pass the initial Integer drawable ids directly in the HashMap:
List<HashMap<String, Integer>> fillMaps = new ArrayList<HashMap<String, Integer>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, Integer> map = new HashMap<String, Integer>();
map.put("image", clubImages[i]);
fillMaps.add(map);
}
I hope I understand what you want. You could make a class to hold the data that you want to pass in to the adapter, for example:
class DataObject {
int imageId;
String firstString;
String secondString; // add here any string that you want to put in your list
public DataObject(String firstString, String secondString, int imageId) {
this.imageId = imageId;
this.firstString = firstString;
this.secondString = secondString;
}
}
Then construct the list for the adapter:
List<HashMap<String, DataObject>> fillMaps = new ArrayList<HashMap<String, DataObject>>();
for(int i = 0; i < eventTitles.length; i++){
HashMap<String, DataObject> map = new HashMap<String, DataObject>();
map.put("image", new DataObject("First String", "second String", clubImages[i]));
fillMaps.add(map);
}
In the getView() method retrieve the DataObject and use its content data as you please.
here is an good example that may help you
Hey I designed a help activity and I have kind of parents (groupItems) which have each one specific child.. So far there is no problem. But the childItems of the ExpandableListView are clickable by default so that they blink if you perform a click on them. And exactly that is what I want to avoid! This is the layout I use for the childs
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/grp_child"
android:paddingLeft="20dp"
android:textSize="15dp"
android:textStyle="normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Here is my source code how all that gets filled with content (this works):
public class HelpActivity extends ExpandableListActivity {
private static final String TAG = "HelpActivity";
private static final String PARENT = "parent";
private static final String CHILD = "child";
private List<HashMap<String,String>> parents;
private List<List<HashMap<String, String>>> childs;
public void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Instantiated new " + this.getClass());
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
parents = createParentList();
childs = createChildList();
SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter(
this,
parents, // Describing data of group List
R.layout.help_group_item, // Group item layout XML.
new String[] {PARENT}, // the key of group item.
new int[] {R.id.row_name}, // ID of each group item.-Data under the key goes into this TextView.
childs, // childData describes second-level entries.
R.layout.help_child_item, // Layout for sub-level entries(second level).
new String[] {CHILD}, // Keys in childData maps to display.
new int[] {R.id.grp_child} // Data under the keys above go into these TextViews.
);
setListAdapter(expListAdapter); // setting the adapter in the list.
}
/* Creating the Hashmap for the row */
private List<HashMap<String,String>> createParentList() {
ArrayList<HashMap<String,String>> result = new ArrayList<HashMap<String,String>>();
HashMap<String,String> m = new HashMap<String,String>();
m.put(PARENT,getResources().getString(R.string.help_parent_1));
result.add(m);
m = new HashMap<String,String>();
m.put(PARENT,getResources().getString(R.string.help_parent_2));
result.add(m);
m = new HashMap<String,String>();
m.put(PARENT,getResources().getString(R.string.help_parent_3));
result.add(m);
return result;
}
/* creatin the HashMap for the children */
private List<List<HashMap<String, String>>> createChildList() {
ArrayList<List<HashMap<String, String>>> result = new ArrayList<List<HashMap<String, String>>>();
for (int i=0;i<parents.size();i++){
HashMap<String, String> sec = parents.get(i);
ArrayList<HashMap<String, String>> secChild = new ArrayList<HashMap<String, String>>();
HashMap<String,String> child = new HashMap<String,String>();
if(sec.containsValue(getResources().getString(R.string.help_parent_1))){
child.put(CHILD, getResources().getString(R.string.help_child_1));
}else if(sec.containsValue(getResources().getString(R.string.help_parent_2))){
child.put(CHILD, getResources().getString(R.string.help_child_2));
}else if(sec.containsValue(getResources().getString(R.string.help_parent_3))){
child.put(CHILD, getResources().getString(R.string.help_child_3));
}else if(sec.containsValue(getResources().getString(R.string.help_parent_4))){
child.put(CHILD, getResources().getString(R.string.help_child_4));
}
secChild.add(child);
result.add(secChild);
}
return result;
}
}
what I wanna to do is to make the textview the childs unclickable: I already tried the textview attributes but no one had an effect
android:clickable="false"
android:focusable="false"
android:enabled="false"
Thanks in advance to anybody who might have an idea!
You need to customise your own ExpandableListAdapter, for example by extending SimpleExpandableListAdapter, and override the .newChildView() and .getChildView() methods. In those overrided methods, you customise by setting the child view's OnClickListener and OnLongClickListener both to null.
#Override
public View newChildView(boolean isLastChild, ViewGroup parent) {
View v = super.newChildView(isLastChild, parent);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
return v;
}
#Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = super.getChildView(groupPosition, childPosition, isLastChild, convertView, parent);
v.setOnClickListener(null);
v.setOnLongClickListener(null);
return v;
}
A View's Clickable property only determines whether or not it's state will change to 'pressed' when clicked, it does not play a role in whether the View can get clicked or not. An ExpandableListAdapter sets default Listeners to its group and child Views, so you must remove them to remove any "reaction" to clicks, focus, etc.
Try this:
getExpandableListView().setOnChildClickListener(null);
In my code the assigned value comes through the server when assigned value is 1.
The checkbox value in listview should be checked while unchecked ...
and after load of listview if I try to check on already checked or change that state it shows an alert box
When I check rest of checkbox while scrolling its automatically unchecked..
boolean checked[];
ArrayList<HashMap<String, Object>> Arraymanagemember = new ArrayList<HashMap<String, Object>>();
ListView managelist = (ListView) findViewById(R.id.managememberlist);
final CheckBox cbox = (CheckBox) managelist.findViewById(R.id.usercheck);
for (int i = 0; i < array.length(); i++) {
HashMap<String, Object> membermap = new HashMap<String, Object>();
memberinfo[i] = array.getJSONObject(i).getString("username").toString();
assingedinf[i] = array.getJSONObject(i).getString("assigned").toString();
if (assingedinf[i].equals("1")) {
membermap.put("assign", true);
} else {
membermap.put("assign", isTaskRoot());
}
membermap.put("member", memberinfo[i]);
Arraymanagemember.add(i, membermap);
}
SimpleAdapter memberadp = new SimpleAdapter(
getApplicationContext(),
Arraymanagemember,
R.layout.managememberrow,
new String[]{"member", "assign"}, new int[]{R.id.manageuserinfo, R.id.usercheck}
);
managelist.setAdapter(memberadp);
managelist.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(
AdapterView<?> parent,
View arg1, int postion,
long arg3) {
if (assingedinf[postion].equals("1")) {
Toast.makeText(getApplicationContext(), "not run", Toast.LENGTH_SHORT).show();
}
}
}
});