I use simple_list_item_1 to show list in my app. I want to change the list so each element in list will have an image + radio button in addition to his text which taken from Const.practisesList.
This is the code now :
ListAdapter listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Const.practisesList);
final ListView tests_list = (ListView)findViewById(R.id.tests_list);
tests_list.setAdapter(listAdapter);
tests_list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
switch(position){
case Const.TEST_TYPE1_MATH:
p = practiseCreator(1)
break;
case Const.TEST_TYPE4_INSTRUCTIONS:
p = practiseCreator(4)
break;
}
}
});
What is the way to do it?
thanks
Create custom layout for your row. Implement custom adapter extended from ArrayAdapter.
private class SchemeAdapter extends ArrayAdapter<String> {
private int layout;
public SchemeAdapter(final Context context, final List<String> objects) {
//row layout id, content view id
super(context, R.layout.list_row_practice, R.id.list_row_practice_name,
objects);
layout = R.layout.list_row_practice;
}
#Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
final View row =
(convertView == null) ? getLayoutInflater().inflate(layout, parent, false) :
convertView;
if (row != null) {
final TextView name = (TextView) row.findViewById(R.id.list_row_practice_name);
//set here your text, image view src and radio button value.
}
return row;
}
}
Related
I would like to color each item´s backround of the spinner individual.
The items are set with this code:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.duesentypen, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
The hex values are stored in an array. Whereas the index count refers to the item count. E.g.:
String[] hex = {"#886da8", "#ffc3cf","#ef9b00", "#2bb430", "#f7dc01", "#bc65a2", "#487ebf","#e00124","#b36634","#949494", "#ffffff","#000000","#4bbbd0", "#8b6d9f", "#8ccff4"};
//color for the first spinner item: hex[0]
//color for the second spinner item: hex[1]
How could I do that in the best way?
You need to create a custom Spinner adapter and override getView() to set the color and text of each item.
Here I am wrapping colors around the array of colors using the modulus operator (%) in order to avoid IndexOutOfBoundsExecption if the items are greater than the available colors.
The custom adapter
public class ColorAdapter extends BaseAdapter {
private ArrayList<Integer> mColors;
private String[] mItems;
ColorAdapter2(String[] items) {
mItems = items;
String[] hex = {"#886da8", "#ffc3cf", "#ef9b00", "#2bb430", "#f7dc01", "#bc65a2", "#487ebf", "#e00124", "#b36634", "#949494", "#ffffff", "#000000", "#4bbbd0", "#8b6d9f", "#8ccff4"};
mColors = new ArrayList<>();
for (String color : hex) {
mColors.add(Color.parseColor(color));
}
}
#Override
public int getCount() {
return mItems.length;
}
#Override
public Object getItem(int args) {
return mColors.get(args);
}
#Override
public long getItemId(int args) {
return args;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
convertView = inflater.inflate(android.R.layout.simple_spinner_dropdown_item, null);
holder = new ViewHolder();
holder.textView = convertView.findViewById(android.R.id.text1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setBackgroundColor(mColors.get(position % mColors.size()));
holder.textView.setText( mItems[position]);
return convertView;
}
/* Return spinner text of the item at particular position*/
public String getItemAtPosition(int position) {
if (position > -1 && position < mItems.length)
return mItems[position];
else
return null;
}
static class ViewHolder {
TextView textView;
}
}
Usage
Spinner spinner = findViewById(R.id.spinner);
spinner.setAdapter(new ColorAdapter(getResources().getStringArray(R.array.myItems)));
strings.xml
<resources>
<string name="app_name">Color Activity</string>
<string-array name="myItems">
<item>item1</item>
<item>item2</item>
<item>item3</item>
<item>item4</item>
<item>item5</item>
</string-array>
</resources>
Result
Edit
How can I get the text of the selected item?
spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString()
isn´t working anymore. The returned value is a big number.
Now as we created a custom adapter, then you have to customize any related data and you can't rely on the API of the default Spinner adapter; so I've added a getItemAtPosition() method above at the custom adapter that takes a position, and returns the text of this item.
Usage
String item = ((ColorAdapter)spinner.getAdapter()).getItemAtPosition(position));
And you can get it whenever an item is selected:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG, "onItemSelected: " +
((ColorAdapter)spinner.getAdapter()).getItemAtPosition(position));
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Try to change color according to Position.
final List<String> plantsList = new ArrayList<>(Arrays.asList(plants));
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.duesentypen, android.R.layout.simple_spinner_item){
#Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View view = super.getDropDownView(position, convertView, parent);
TextView tv = (TextView) view;
switch(position) {
case 0:
tv.setTextColor(Color.parseColor("#FF7C7967"));
case 1:
tv.setTextColor(Color.parseColor("#FF7C7967"));
}
return view;
}
};
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
I have a custom adapter where i display text and a spinner. I have tried to store the state of each spinner so as to display the same items when the activity is open again i have tried different things but i have not been successfull. Please how do i accomplish this thanks. This is the latest thing i have tried
CustomListAdapter(Context context, ArrayList<String> subjects) {
super(context, R.layout.custom_list_view, subjects);
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.custom_list_view, parent, false);
singleSubject = getItem(position);
TextView singleText = (TextView) customView.findViewById(R.id.listSubjectsMyCourses);
colorLayout = (LinearLayout)customView.findViewById(R.id.colorForSubjects);
relativeLayout = (RelativeLayout)
customView.findViewById(R.id.relativeForView);
parentLayout = (RelativeLayout)
customView.findViewById(R.id.parentLayout);
points = new ArrayList<>();
selected = new ArrayList<>();
selectedsttring = new ArrayList<>();
customView.findViewById(R.id.textViewForGrades);
tinyDB = new TinyDB(getContext());
spinnerForGradePoints = (Spinner)customView.findViewById(R.id.spinnerForGrades);
final ArrayAdapter<String> gradePointAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_dropdown_item_1line, UserCourseSelection2.userSubjectGradePoint);
spinnerForGradePoints.setAdapter(gradePointAdapter);
spinnerForGradePoints.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedItempos = adapterView.getSelectedItemPosition();
String getSelectedItem = adapterView.getItemAtPosition(i).toString();
tinyDB.putInt("selected", selectedItempos);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
singleText.setText(singleSubject);
colorLayout.setBackgroundColor(UserCourseSelection2.userSubjectsListColor.get(position));
int getSelected = tinyDB.getInt("selected");
spinnerForGradePoints.setSelection(getSelected);
return customView;
}
SharedPreferences is the way if you dont want to use dependency injection like dagger or some kind of custom local cache on the application level.
For SharedPreferences in spinner check this answer:
https://stackoverflow.com/a/13431729/5577679
I'm trying to implement an Android app for sudoku game, and i created a customized adapter for that. i want make edit text for cells the user is allowed to modify, and textview for cell filled by the program, the number of ediTexts and textViews will be random. how do specify that in the adapter ?
This is my adapter :
public class SodukuAdapter extends BaseAdapter {
ArrayList<String> items;
static Activity mActivity;
private static LayoutInflater inflater = null;
public SodukuAdapter (Activity activity, ArrayList<String> tempTitle,) {
mActivity = activity;
items = tempTitle;
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public final int getCount() {
return items.size();
}
#Override
public final Object getItem(int position) {
return items.get(position);
}
#Override
public final long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = null;
v = inflater.inflate(R.layout.item, null);
EditText et = (EditText) v.findViewById(R.id.et);
et.setText(items.get(position));
return v;
}
}
You can place both views in the same parent (e. g. parent is a RelativeLayout or a FrameLayout) so they are on top of each other. Than you simply hide one of them and show the other using setVisibility() in your getView() method of the adapter.
Of course you would need a datasource object which keeps track of whether a view should show the TextView or the EditText:
class SodokuItem {
public boolean isStatic;
public String text;
}
...
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = inflater.inflate(R.layout.item, null);
SodokuItem item = items.get(position);
EditText et = (EditText) v.findViewById(R.id.et);
TextView tv = (TextView) v.findViewById(R.id.tv);
if(item.isStatic){
et.setVisibility(GONE);
tv.setText(item.text);
}else{
tv.setVisibility(GONE);
et.setText(item.text);
}
return v;
}
I have a listview of objects in my android application I have made it by using my custom ArrayAdapter and I want to get the fields of any object that now is a listItem by clicking the listItem but I haven't any idea to do this
my Content class is:
public class Content {
public String title;
public String text;
public int id;
public Date date;
}
and my ContentAdapter class:
public class ContentAdapter extends ArrayAdapter<Content> {
private ArrayList<Content> objects;
public ContentAdapter(Context context, int textViewResourceId,
ArrayList<Content> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.content_list_item, null);
}
Content i = objects.get(position);
if (i != null) {
TextView tt = (TextView) v.findViewById(R.id.toptext);
TextView ttd = (TextView) v.findViewById(R.id.toptextdata);
TextView mt = (TextView) v.findViewById(R.id.middletext);
TextView mtd = (TextView) v.findViewById(R.id.middletextdata);
if (tt != null) {
tt.setText("title");
}
if (ttd != null) {
ttd.setText(i.title);
}
if (mt != null) {
mt.setText("text:");
}
if (mtd != null) {
mtd.setText(i.text);
}
}
return v;
}
}
now I want to get date and id by clicking a list item but not show them in the list view
should I add id and date fields to my custom arrayAdapter class to do this?
Assuming that your custom adapter holds a list of Content object, you will have to add an OnItemClickListener to your list view as below and obtain the object clicked and retrieve the attributes.
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view,
int position, long arg3) {
Content content = (Content ) adapterView
.getItemAtPosition(position);
//from the content object retrieve the attributes you require.
}
});
I have a ListView that is contained in a fragment. When an item is clicked on the listview I want it to execute some code but the code that is executed depends on which item on the List was clicked. Here is my code for the fragment that contains the ListView:
public class ListViewer extends Fragment {
private ListView list;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInsanceState) {
View v = inflater.inflate(R.layout.activity_list_view, container, false);
Player player_data[] = new Player[] {
new Player(R.drawable.bschenn1, "Brayden Schenn", "Age: 21, Drafted 5th overall by LA King in 2009", "Center - #10"),
new Player(R.drawable.giroux, "Claude Giroux", "Age: 25, Drafted 22nd overall by Philadelphia Flyers in 2006", "Center - #28"),
new Player(R.drawable.hartnell, "Scott Hartnell", "Age: 30, Drafted 6th overall by Nashville Predators in 2000", "Left Wing - #19")
};
PlayerAdapter adapter = new PlayerAdapter(getActivity(), R.layout.list_row, player_data);
list = (ListView)v.findViewById(R.id.list);
View header = (View)inflater.inflate(R.layout.listview_header_row, null);
list.addHeaderView(header);
list.setAdapter(adapter);
return v;
}
}
Here is the code to the Adapter:
public class PlayerAdapter extends ArrayAdapter<Player> {
Context context;
int layoutResourceId;
Player data[] = null;
public PlayerAdapter(Context context, int layoutResourceId, Player[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
PlayerHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new PlayerHolder();
holder.imageIcon = (ImageView)row.findViewById(R.id.list_image);
holder.txtplayerName = (TextView)row.findViewById(R.id.title);
holder.txtageDraft = (TextView)row.findViewById(R.id.artist);
holder.txtpositionSNum = (TextView)row.findViewById(R.id.duration);
row.setTag(holder);
} else {
holder = (PlayerHolder)row.getTag();
}
Player player = data[position];
holder.imageIcon.setImageResource(player.image);
holder.txtplayerName.setText(player.playerName);
holder.txtageDraft.setText(player.ageDraft);
holder.txtpositionSNum.setText(player.positionSweaterNum);
return row;
}
static class PlayerHolder {
ImageView imageIcon;
TextView txtplayerName;
TextView txtageDraft;
TextView txtpositionSNum;
}
}
Any help is much appreciated.
You should add a listener to the listview to detect touch events.
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView parentView, View childView, int position, long id)
{
//here you have the position so you can do whatever you want with this item
}
});
You need to add OnItemClickListener to your list object:
list.setOnItemClickListener( new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
}
});
}