Hey guys I am pretty new to android programming and I have a few questions. I hope somebody can help.
I am using the code:
new ArrayAdapter<Customer> (this, android.R.layout.simple_list_item_multiple_choice,emptyListForInitialization) {...
But I want to use my own layout I created ("R.layout.custom_customer_layout").
I can use it, if I CHANGE the code-line with:
new ArrayAdapter<Customer> (this, android.R.layout.simple_list_item_multiple_choice,emptyListForInitialization)
with my layout ("R.layout.custom_customer_layout")
AND REMOVE THE CODE
View view = super.getView(position, convertView, parent);
And UNCOMMENT the section (code between /* ... */ ) below it.
But I don't get the getView-Method.
WHY DO I HAVE TO REFER TWICE TO MY LAYOUT?
First time during creating the new ArrayList
Second time in the getView-Method???
This is the important part of my code:
private ListView myTestListView;
private void initializeCustomerListView() {
myTestListView = (ListView) findViewById(R.id.listview_customers);
List<Customer> emptyListForInitialization = new ArrayList<>();
ArrayAdapter<Customer> customerArrayAdapter = new ArrayAdapter<Customer> (
this, android.R.layout.simple_list_item_multiple_choice,
emptyListForInitialization) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
/*
View view = getLayoutInflater().inflate(R.layout.custom_customer_layout,null);
TextView textViewName = (TextView)convertView.findViewById(R.id.textView20);
TextView textViewContact = (TextView)convertView.findViewById(R.id.textView21);
TextView textViewMobile = (TextView)convertView.findViewById(R.id.textView25);
textViewName.setText(this.getItem(position).getCustomerName());
textViewContact.setText(this.getItem(position).getCounterpart());
textViewMobile.setText(this.getItem(position).getMobilePhoneNumber());
*/
return view;
}
};
myTestListView.setAdapter(customerArrayAdapter);
}
Related
I want to add single row to my ListView. When I run the code below, there are 2 lines added, one with the text I want to add, and an empty one. Why is the empty one added? (lines[0] is a file with (for now) 1 line in it)
listAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.listviewrow, R.id.txt_listviewItem, lines){
#Override
public View getView(int position, View convertView, ViewGroup parent){
View myView = convertView;
if (myView == null) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = li.inflate(R.layout.listviewrow, null);
} else {
TextView txt_listviewItem = (TextView)myView.findViewById(R.id.txt_listviewItem);
txt_listviewItem.setText(lines[0]);
}
return myView;
};
...
listView.setAdapter(listAdapter);
When I debug, listview.setAdapter(listAdapter) is called before AND after the getView code runs. Why? I only call it once.
you have use Viewholder to avoid duplication.
Just a wild guess, but does your text file include a "new line" character on the second row? Or, should I say, does your text file have, indeed, one line or two lines out of which the second is just a \n character?
Try this
listAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.listviewrow, R.id.txt_listviewItem, lines){
#Override
public View getView(int position, View convertView, ViewGroup parent){
View myView = convertView;
if(myView==null){
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
myView = li.inflate(R.layout.listviewrow, null);
TextView txt_listviewItem = (TextView)myView.findViewById(R.id.txt_listviewItem);
txt_listviewItem.setText(lines[0]);
}
return myView;
};
listView.setAdapter(listAdapter);
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);
So in my listview I have a bunch of strings, but I'd like to display the values with £ in front of the strings, but NOT actually edit the values themselves in the ArrayList.
Anyone know how I can do this?
I'm already overwriting the view anyway, I'm just not sure how to alter the display of the items values themselves.
Some Code:
adapter = (new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
if (position % 2 != 0)
view.setBackgroundColor(Color.rgb(135,206,250)); // Pale blue
return view;
}
});
foodListView = (ListView) findViewById(R.id.foodListView);
foodListView.setAdapter(adapter);
In you getView method you can use:
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setText("£" + listItems.get(position));
So, I have a spinner with two different options thus far. What I am trying to accomplish is, if "First Spinner Option" is chosen, then I setContentView to a specific layout and execute code corresponding to that layout. The same goes if "Second Spinner Option" is chosen. I know that I need to use setOnItemSelectedListener to a certain extent, but I am not sure how this would work exactly. Below is a quick mock up of what I'm trying to do in coding terms
spinner.setonItemSelectedListener(this);
if(spinner = first spinner option){
setContentView(R.layout.lay1);
//other code here
}elseif(spinner = second spinner option){
setContentView(R.layout.lay2);
//other code here
}
I know the syntax is bad here, Im just trying to get a general idea of how this could be done.
EDIT: #CodeMagic
This is how my code is setup thus far. 'items' is just an array of strings with 2 elements.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.my_spinner_style, items) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Exo-Bold.otf");
((TextView) v).setTypeface(tf);
return v;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
View v =super.getDropDownView(position, convertView, parent);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Exo-Bold.otf");
((TextView) v).setTypeface(tf);
//v.setBackgroundColor(Color.GREEN);
return v;
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gasChoice.setAdapter(adapter);
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
gasChoice = (Spinner) findViewById(R.id.gasChoice);
gasChoice.setOnItemSelectedListener(new OnItemSelected());
}
I don't know exactly what the problem is you are having but its pretty close. You just need to add the method
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
TextView tv = (TextView)arg1; // get the TextView selected
String text = tv.getText().toString();
if(text.equals("FirstText")){ // compare the text in the box
setContentView(R.layout.lay1);
//other code here
}elseif(text.equals("FirstText")){
setContentView(R.layout.lay2);
//other code here
}
}
There are different ways of doing this such as getting the position (arg2) and comparing that to what's in your adapter but since I don't know how you are doing any of that, this is the easiest way to get you started.
I want to override the text colour in the android adapter, how can I do that. The values of the string will be loaded from backend service.
Plz help me out.
lv = (ListView) findViewById(R.id.report_symptomlv);
mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, mSymptoms);
lv.setAdapter(mAdapter);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setOnItemClickListener(this);
//--------------
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
mName = input.getText().toString().toUpperCase();
//----I need to make this text as Orange----
Geocoder mGeoCoder = new Geocoder(favorite.this, Locale.getDefault());
Try this...
- I believe a Code is better than thousand Words, but still some guys need more than that, and so i would like to explain it....
- You will need to use getView() method that will be in used to defining the attributes of the inflated views in the ListView.
ArrayAdapter<String> adpt = new ArrayAdapter<String>(ReferralsActivity.this, android.R.layout.simple_list_item_1,numList){
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,20);
((TextView) v).setTextColor(Color.BLACK);
return v;
}
};
Use a custom Listview. You will have more flexibility in inflating the views and styling them. For custom listview have a look at this link. http://www.androidpeople.com/android-custom-listview-tutorial-part-1.