I am updating a custom adapter and had to change the value from a String Array to a List.
Here is code:
private final List<String> values;
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.comments_listadapter, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.comments_label);
ImageView imageView = (ImageView) rowView.findViewById(R.id.comments_menu);
textView.setText(values[position]); // Needs to Change!
String s = values[position]; // Needs to Change!
imageView.setImageResource(R.drawable.up_down);
return rowView;
}
I have marked the two lines that need to change (I am not sure if changing it to a List will need more to change?
List<String> mStringList = Arrays.asList(values);
where values is a string array.
Arrays class is in java.util package.
If I understand your question correctly, you're looking for:
String s = values.get(position);
textView.setText(s);
Try this
String s = getItem(position);
textView.setText(s);
So it sounds like you have something like String[] values which you want to be List<String> values.
Here is how you can do the conversion:
List<String> listValues = new ArrayList<String>();
for(String s : values) {
listValues.add(s);
}
Then instead of values[position], you do listValues.get(position).
What exactly is your problem?
Do you mean you have to change your references to
mList.get(position);
mList.add(position, string);
Related
I have a Problem, that I could not find in the Internet yet…
My aim is to create a little Texteditor based on the structure of given Elements. Basicly you choose an element out of 3 different Topics (Driving, Eating, Extra Costs) and provide some Information like Time, Adress, Price... These Elements can be choosen in any order and as often as you want to. No problem so far.
Problem:
What makes it really difficult for me is the desired layout: Everytime you choose one of the above Topics and saved the Information, i want a Fragment to be shown, that displays the given Information.
Is it possible somehow, to use a listview with changing Elements?
Maybe it's better to use a scrollview - but how can i create the needed fragmentcontainers programmaticly when needed, to avoid providing dozens of Dummie-containers, waiting to be filled and displayed?
Please have a look at the Pictures - I hope it makes clear what i want to describe…
https://ibb.co/Kry1mJm
https://ibb.co/26gG4K2
https://ibb.co/1Jvm1Rg
What magic word is the one to search for in the Internet?
I hope someone could help me here - thanks for your patience!
This is a more or less creativ Workaround that works fine so far. Does anybody know. if this concept will cause any Problems?
Its the Code of a Custom Adapter for a normal Listview. It chooses what Layout will be inflated, by a Keyword safed in the ArrayList
public class EintragAdapter extends ArrayAdapter<EintragDH> {
private Context mContext;
private int mResource;
public EintragAdapter(#NonNull Context context, int resource, #NonNull List<EintragDH> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
String Elementtyp = getItem(position).getElementTyp();
if(Elementtyp.equals("Add")){
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.editorelement_add,parent,false);
}
if (Elementtyp.equals("Abfahrt")){
String AbfahrtVon = getItem(position).getAbfahrtVon();
String AbfahrtUhrzeit = getItem(position).getAbfahrtUhrzeit();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.editorelement_abfahrt,parent,false);
TextView tvDatum = convertView.findViewById(R.id.tvDatum);
TextView tvAbfahrtVon = convertView.findViewById(R.id.tvAbfahrtVon);
TextView tvAbfahrtUhrzeit = convertView.findViewById(R.id.tvAbfahrtUhrzeit);
tvAbfahrtVon.setText("Abfahrt von Zuhause: " + AbfahrtVon);
tvAbfahrtUhrzeit.setText("Uhrzeit Abfahrt:\n" + AbfahrtUhrzeit);
}
if(Elementtyp.equals("Termin")){
String TerminUhrzeiten = getItem(position).getTerminUhrzeiten();
String TerminName = getItem(position).getTerminName();
String TerminAnschrift = getItem(position).getTerminAnschrift();
String TerminEinnahme = getItem(position).getTerminEinnahme();
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.editorelement_termin,parent,false);
TextView tvTerminUhrzeiten = convertView.findViewById(R.id.tvUhrzeitenTermin);
TextView tvTerminName = convertView.findViewById(R.id.tvTerminName);
TextView tvTerminAnschrift = convertView.findViewById(R.id.tvTerminAnschirft);
TextView tvTerminEinnahme = convertView.findViewById(R.id.tvTerminEinnahmen);
tvTerminUhrzeiten.setText(TerminUhrzeiten);
tvTerminName.setText(TerminName);
tvTerminAnschrift.setText(TerminAnschrift);
tvTerminEinnahme.setText("+ " + TerminEinnahme + "€");
}
return convertView;
}
}
I am running into some difficulty with populating values to a listview, which currently displays no rows.
Firstly I am retrieving values from my database table and storing them in Arrays within onPostExecute.
LikedListAdapter listAdapter = new LikedListAdapter(context, R.layout.liked_list_main, restID, restName, restAddr1, restAddr2, restImgPath);
lvPlaces.setAdapter(listAdapter);
These values are then successfully passed into my ListAdapter.
public LikedListAdapter(Context context, int resource, String[] restID, String[] restName, String[] restAddr1, String[] restAddr2, String[] restImgPath) {
super(context, resource);
this.context = context;
this.resource = resource;
this.restID = restID;
this.restName = restName;
this.restAddr1 = restAddr1;
this.restAddr2 = restAddr2;
this.restImgPath = restImgPath;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LinearLayout likedItemsView;
if(convertView==null) {
likedItemsView = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater vi;
vi = (LayoutInflater)getContext().getSystemService(inflater);
vi.inflate(resource, likedItemsView, true);
}
else {
likedItemsView = (LinearLayout) convertView;
}
ImageView restaurantImg = (ImageView)likedItemsView.findViewById(R.id.listItemThumbImg);
TextView restaurantName =(TextView)likedItemsView.findViewById(R.id.listItemTitle);
TextView restaurantDesc = (TextView)likedItemsView.findViewById(R.id.listItemSubText);
restaurantName.setText(restName[position]);
restaurantDesc.setText(restAddr1[position] + ", " + restAddr2[position]);
Picasso
.with(getContext())
.load(restImgPath[position])
.into(restaurantImg);
return likedItemsView;
}
However, when i run the app the Listview is empty. When debugging i notice that the values are successfully passed to my listAdapter (on debugging it displays the values retrieved being displayed in the constructor) however it never hits the method getView, where values are set to each listview widget.
Is there something i am misunderstanding, or do i need to call the getView method at some point? Thanks in advance.
Did you override
getCount()
method? If not then return the size of your rows. Ex-
#Override
public int getCount() {
return restName.length;
}
Hope your problem will be solved. If already populated list then use
adapter.notifyDataSetChanged()
method.
LikedListAdapter listAdapter = new LikedListAdapter(context, R.layout.liked_list_main, restID, restName, restAddr1, restAddr2, restImgPath);
lvPlaces.setAdapter(listAdapter);
listAdapater.notifyDataSetChanged(); // <-- add this
I'm creating an app for a bus station, to give the schedule. For that i'm using a custom listview. Here it is:
class custom_adapter extends ArrayAdapter<String>{
public custom_adapter(Context context, ArrayList<String> horarios) {
super(context, R.layout.costum_listview ,horarios);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater horarioInflater = LayoutInflater.from(getContext());
View costumView = horarioInflater.inflate(R.layout.costum_listview, parent, false);
String singleHorario = getItem(position);
TextView hora = (TextView) costumView.findViewById(R.id.Hora);
TextView nota = (TextView) costumView.findViewById(R.id.Nota);
hora.setText(singleHorario);
nota.setText(" ");
return costumView;
}
}
Now as you can see I have just 2 texViews yet, the "hora" is to show the timers of the bus, the "nota" is for some notes, like someday the bus don't go or something like that. And my problem is exactly on that "nota" textview. I have dozens of arrayList's passing to this custom ListView, and so dozens and dozens of timers, and there are some timers that I need to put a note and other that I don't. So, can I had another argument to this custom ListView, like a boolean or something, so I can do a if / else in that code to put a note on each one. What do I need to change in order to do that ? I've been trying, but didn't quite managed to do that.
Instead of using a String as the argument for your ArrayAdapter, create a custom class and use that instead.
That way you can pass all the information you want into the adapter and show it however you like.
public class custom_adapter extends ArrayAdapter<MyClass>{
public custom_adapter(Context context, ArrayList<MyClass> horarios) {
super(context, R.layout.costum_listview ,horarios);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater horarioInflater = LayoutInflater.from(getContext());
View costumView = horarioInflater.inflate(R.layout.costum_listview, parent, false);
MyClass singleHorario = getItem(position);
TextView hora = (TextView) costumView.findViewById(R.id.Hora);
TextView nota = (TextView) costumView.findViewById(R.id.Nota);
hora.setText(singleHorario.hora);
nota.setText(singleHorario.nota);
return costumView;
}
And the new class
public class MyClass {
public String hora;
public String nota;
}
How about making a class for holding two values 'hora' and 'nota', and with, lets say, boolean isNotaAvailable() method. Then in getView() you just make something like this:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater horarioInflater = LayoutInflater.from(getContext());
View costumView = horarioInflater.inflate(R.layout.costum_listview, parent, false);
YourClassName singleHorario = getItem(position);
TextView hora = (TextView) costumView.findViewById(R.id.Hora);
TextView nota = (TextView) costumView.findViewById(R.id.Nota);
// set hora text
hora.setText(singleHorario);
// check if nota is available, if true - set nota text
if(singleHorario.isNotaAvailable()) {
nota.setText(singleHorario.getNota())}
else nota.setVisibility(View.GONE);
return costumView;
}
It's just an idea, tell me if it helps :)
Just extend BaseAdapter, so you can define the data structure.
class custom_adapter extends BaseAdapter
change ArrayList<String> horarios to ArrayList<Data> horarios
And the Data can be
public class Data{
private String hora;
private String nota;
private boolean shouldShowNota;
//write getter and setter here
}
at last, read the data in getView
Data data = getItem(position);
if (data.getShouldShowNota) {
nota.setText(data.getNote);
}
hora.setText(data.getHora);
i have a xml parser with lazy adapter, i parse all the data to an list view.
now i would like to grabe all the data from the listivew(wiche is 1 textview per row) and put is in and string array
but i have no idea how i have to do that. because if i Google i only get results on how to use and string array to populate the list.
my parser:
TextView txt =(TextView)vi.findViewById(R.id.tv);
HashMap<String, String> song = new HashMap<String, String>();
song = data.get(position);
txt.setText(song.get(MainActivity.KEY_TXT));
the method i use (so far) for getting my string array:
private String[] txt = {
}
thanks in advance.
You just have to use the string array in getView() method of listView adapter. like this:--
give the size of strArray same as the size of listView.
String strArray[] = new String[<size of Your ListView>];
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listitem, parent, false);
textView = (TextView) convertView
.findViewById(R.id.textView);
}
strArray[position] = textView.getText().toString();
return convertView;
}
now u want to use this strArray to other class then either you have to make it static or you could pass this to other class via intent.like this:--
Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);
and then in other activity class You should get this String array by putting this code:--
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
and if the class to whom you want to pass the String Array( that is strArray) is not Activity then You should make your strArray static like this:
public static strArray[];
now i think your problem will be solved..
This one has me quite stumped. I'm sure it is just something simple I am missing, but I cant seem to find out what...
When I run the program, it opens up the dialog box and displays the AutoCompleteTextView I have initialized. When I try to type something into it, nothing drops down or is displayed other than the text I type in. I have created a similar system in another part of my program with the same mechanics, but using a regular ArrayAdapter and it works fine so the interface is not the problem.
Here is where I initialize my custom ArrayList. I have been trying to use just strings to make it simpler.
final Dialog weaponDialog = new Dialog(BattleScreen.this);
weaponDialog.setContentView(R.layout.weapon_selection_dialog);
weaponDialog.setTitle("Add a Weapon");
weaponDialog.setCancelable(true);
String[] weaponStringArrayList = ConstantEquipmentHelper.getCondensedWeaponString();
WeaponArrayAdapter weaponAdapter = new WeaponArrayAdapter(this, R.layout.weapon_list_item, weaponStringArrayList);
weaponDialogAcTextView = (AutoCompleteTextView) weaponDialog.findViewById(R.id.weaponSelectionAutoCompleteTxt);
weaponDialogAddButton = (Button) weaponDialog.findViewById(R.id.weaponSelectionAddButton);
weaponDialogWeaponInfo = (TextView) weaponDialog.findViewById(R.id.weaponSelectionInformationTxt);
...
...
...
Here is my custom ArrayAdapter Class
public class WeaponArrayAdapter extends ArrayAdapter<String> {
private Context context;
String[] objects;
public WeaponArrayAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId);
this.objects = objects;
this.context = context;
}
private class WeaponItemHolder {
TextView weaponName;
TextView weaponCat;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//return super.getView(position, convertView, parent);
final WeaponItemHolder holder;
if (convertView == null) {
//Sets up a new holder to temporaraly hold the listeners that will be assigned to the binded variables
holder = new WeaponItemHolder();
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.weapon_list_item, null);
//Find the IDs! Find them!!!!
holder.weaponName = (TextView) convertView.findViewById(R.id.weaponListItemName);
holder.weaponCat = (TextView) convertView.findViewById(R.id.weaponListItemCategory);
//"Sets the tag associated with this view. A tag can be used
//to mark a view in its hierarchy and does not have to be unique within the hierarchy."
convertView.setTag(holder);
} else {
holder = (WeaponItemHolder) convertView.getTag();
}
String spellName = objects[position];
String[] weaponInfo = spellName.split("\\:");
weaponInfo[1] = weaponInfo[1].trim();
holder.weaponName.setText(weaponInfo[0]);
holder.weaponCat.setText(weaponInfo[1]);
return convertView;
}
}
Additional Info: I have tried debugging it and it never reaches getView. This makes sense of course, as its not displaying anything.
Thanks,
-Andrew
EDIT: I have found out how to implement the above problem:
I used a SimpleAdapter with a custom layout.
However, now I can not select any of the items... onItemClick is not even called when I try to click it. It probably has to do with using the SimpleAdapter??
LINK: http://lemonbloggywog.wordpress.com/2011/02/15/customer-autocomplete-contacts-android/
ArrayList<Map<String, String>> weaponStringArrayList = ConstantEquipmentHelper.getCondensedWeaponString();
//The adapter that recieves the layout type from android and the array creatd by the above function.
SimpleAdapter simpleAdapter = new SimpleAdapter(this, weaponStringArrayList, R.layout.weapon_list_item ,new String[] {"name", "category"}, new int[] { R.id.weaponListItemName, R.id.weaponListItemCategory});
//Find the view blah blah blah...
weaponDialogAcTextView = (AutoCompleteTextView) weaponDialog.findViewById(R.id.weaponSelectionAutoCompleteTxt);
weaponDialogAddButton = (Button) weaponDialog.findViewById(R.id.weaponSelectionAddButton);
weaponDialogWeaponInfo = (TextView) weaponDialog.findViewById(R.id.weaponSelectionInformationTxt);
//Set that adapter!
weaponDialogAcTextView.setAdapter(simpleAdapter);
You have to implement getCount() and set the count of your data, i.e. objects.length.
You also have to set the adapter to the view using the method setAdapter().
Hope this helps!