How can we give custom adapter for Material Exposed Dropdown? - java

I tried to give custom array Adapter in Material Exposed dropdown with image and text value. DropDown is shown perfectly but when I click on those dropdown item, the selected item value is set to something like #71232. And when I set it manually like this, Second time onwards the dropdown doesn't show up.
autocompleteTextView.setOnItemClickListener(...
...
#Override
public void onItemClick{
setText(clickedItem.getText());
}
CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(getContext(), list);
autoCompleteTextView.setAdapter(customArrayAdapter);
This is my CustomArrayAdapter
public class CustomArrayAdapter extends ArrayAdapter<CustomArrayAdapter.AdapterItem> {
public CustomArrayAdapter(Context context,
ArrayList<AdapterItem> itemList) {
super(context, R.layout.item_dropdown, R.id.tv_text, itemList);
}
private View initView(int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_dropdown, parent, false);
}
TextView tvText = convertView.findViewById(R.id.tv_text);
ImageView ivImage = convertView.findViewById(R.id.iv_image);
AdapterItem currentItem = getItem(position);
if (currentItem != null) {
tvText.setText(currentItem.text);
ivImage.setImageDrawable(getContext().getDrawable(currentItem.image));
}
return convertView;
}
}

Related

Java Android Data Binding ListView

I have problem with list view in data binding because I don't know how to set custom ArrayAdapter in view model, and how to add onItemClick listener in view model. Someone can show how to do this? Internet have really little information about this.
Simply set the adapter for the ListView. R.id.listview must refer to your ListView defined in the Layout obviously and R.layout.listviewrow to a layout that a row should have. Furthermore add a new Instance of onItemClickListener to the ListView.
MyCustomArrayAdapter adapter = new MyCustomArrayAdapter(getActivity(), R.layout.listviewrow);
ListView lv = (ListView) getActivity().findViewById(R.id.listview);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//perform desired action here
}
} );
The Adapter itself should look something like this:
public class MyCustomArrayAdapter extends ArrayAdapter<Item> {
public MyCustomArrayAdapter(Context context, int resource) {
super(context, resource);
}
public MyCustomArrayAdapter(Context context, int resource, List<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(resource, null);
}
Item item = getItem(position);
if (item != null) {
TextView tvFirstName = (TextView) v.findViewById(R.id.firstName);
TextView tvLastName = (TextView) v.findViewById(R.id.lastName);
if (tvFirstName != null) {
tvFirstName.setText(item.getFirstName());
}
if (tvLastName != null) {
tvLastName.setText(item.getLastName);
}
}
return v;
}

how do i change the image for each row in this list view

im new to android and java and i created a my own adapter and list view from a tutorial I was following online. I want to know how do i the image for each individual row because my adapter (from the tutorial) only has 1 image for all the rows. here is the code for my own adapter.
class HangarAdapter extends ArrayAdapter<String> {
public HangarAdapter(Context context, String[] values) {
super(context, R.layout.hangar_layout, values);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater theInflater = LayoutInflater.from(getContext());
View theView = theInflater.inflate(R.layout.hangar_layout, parent, false);
String ship = getItem(position);
TextView theTextView = (TextView) theView.findViewById(R.id.textView1);
theTextView.setText(ship);
ImageView theImageView = (ImageView) theView.findViewById(R.id.imageView);
theImageView.setImageResource(R.drawable.dot);
return theView;
}
}
theImageView.setImageResource(R.drawable.dot);
This line above is what you have to change in order to use a different images;
you could potentially do something like this;
if (position == 0){
theImageView.setImageResource(R.drawable.dot);
} else if (position == 1){
theImageView.setImageResource(R.drawable.circle);
}
It also depends on the backing data.

Android Listview clickable textview

I have made an Listview populated with list_row_layout.xml(which is populated with json serializable class), i have clickable textview and onclick changing text from "Accept" to "Accepted". But when i click it on first listview item, another textview listview items below are changing.
Here's some photos to descibe you better
this is the adapter class
public class CustomListAdapter extends BaseAdapter {
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
private Context mContext;
public CustomListAdapter(Context context, ArrayList<FeedItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
mContext = context;
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView)convertView.findViewById(R.id.sex);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.confid);
holder.approve = (TextView) convertView.findViewById(R.id.approveTV);
holder.approve.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View argView)
{
holder.approve.setText("Accepted");
}
}
);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
return convertView;
}
static class ViewHolder {
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
}
}
Remember that views can be recycled via convertView.
In your onClick method you set the approve text to "Accepted" but when the view is recycled, you never set it back to "Accept"
Actually you need to update (something in) the list in response to an click and have the Accept/Accepted value toggle based on that value rather than simply updating what is currently visible on the screen.
-- to answer the "how" question (asked below)--
Add a new field to ViewHolder
static class ViewHolder {
TextView approve;
TextView headlineView;
TextView reportedDateView;
ImageView imageView;
FeedItem newsItem;
}
Change the onClick method:
public void onClick(View argView)
{
// note that holder no longer needs to be final in the parent class
// because it is not used here.
View parent = (View)argView.getParent();
ViewHolder clickedHolder = (ViewHolder)parent.getTag();
clickedHolder .newsItem.setAccepted(true); /// a new method
clickedHolder .approve.setText ("Accepted");
Log.d(TAG, "Accepted item #" + position);
}
After you have convertView created (if necessary)
FeedItem newsItem = (FeedItem) listData.get(position);
holder.newsItem = newsItem; // populate the new field.
holder.headlineView.setText(Html.fromHtml(newsItem.getTitle()));
holder.reportedDateView.setText(Html.fromHtml(newsItem.getContent()));
if(newsItem.isAccepted ()){ // another new method!
holder.approve.setText ("Accepted");
Log.d(TAG, "Set text to Accepted for item #" + position);
}else{
holder.approve.setText("Accept");
Log.d(TAG, "Set text to Accept for item #" + position);
}
Once it is working you should consider removing the Log.d() lines to cut down on the noise in LogCat.

Delete item from listview

I have listview which contains textview and buttons. When i delete listview item and i try to scroll down, i get exception on this:
BuildQueue eile = countryList.get(position);
Exception:
02-08 19:11:04.279: E/AndroidRuntime(10509): java.lang.IndexOutOfBoundsException: Invalid index 15, size is 15
Seems i do not updating something when i delete item from listview. I think i have problem with ViewHolder, but i do not know what kind of...
My ArrayAdapter code:
public class MyCustomAdapter extends ArrayAdapter<BuildQueue> {
private ArrayList<BuildQueue> countryList;
public MyCustomAdapter(Context context, int textViewResourceId,ArrayList<BuildQueue> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<BuildQueue>();
this.countryList.addAll(countryList);
}
private class ViewHolder {
TextView code;
TextView field;
Button del;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.queue_buildings, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.field = (TextView) convertView.findViewById(R.id.field_text);
holder.del = (Button) convertView.findViewById(R.id.del_button);
convertView.setTag(holder);
holder.del.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Button del_button = (Button) v;
BuildQueue building = (BuildQueue) del_button.getTag();
countryList.remove(building);
dataAdapter.notifyDataSetChanged();
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
BuildQueue eile = countryList.get(position);
holder.code.setText(" ( Level: " + eile.getOld_level() + " to "+eile.getNew_level()+")");
holder.field.setText(eile.getNameSort());
holder.field.setTag(eile);
holder.del.setText("Delete");
holder.del.setTag(eile);
return convertView;
}
}
You are using a two arrays in your Adapter, but only changing one of them.
Every Adapter uses getCount() to determine how many row should be drawn. ArrayAdapter's getCount() simply asks for the size of the array that you pass to the super constructor here: super(context, textViewResourceId, countryList);. But you are also using a second, local array and when you delete a value from this countryList getCount() has no idea this happened which results in getView() throwing an IndexOutOfBoundsException...
Either extend BaseAdapter, or use ArrrayAdapter's methods like getItem(), add(), and remove() and remove your local data set.

ListAdapter to modify the datasource (which is an arraylist)

here's a problem that i've run into lately:
I have a listview with a custom adapter class, the adapter takes in a listview and populates the listview with elements from it. Now, i'd like to have a button on each row of a listview to remove the item from it. How should i approach this problem? Is there a way to remotely trigger a method in the activity class and call notifydatachanged() method on the adapter to refresh the listview?
I've done something like that:
public class MyAdapter extends Adapter {
private final ArrayList<String> items = new ArrayList<String>();
// ...
deleteRow(int position) {
items.remove(position);
notifyDataSetChanged();
}
//
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null) {
Tag tag = new Tag();
// inflate as usual, store references to widgets in the tag
tag.button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
deleteRow(position);
}
});
}
// don't forget to set the actual position for each row
Tag tag = (Tag)convertView.getTag();
// ...
tag.position = position;
// ...
}
class Tag {
int position;
TextView text1;
// ...
Button button;
}
}
In the getView() method, can't you just setOnClickListener() on the button?
Something like this:
static final class MyAdapter extends BaseAdapter {
/** override other methods here */
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// inflate the view for row from xml file
// keep a reference to each widget on the row.
// here I only care about the button
holder = new ViewHolder();
holder.mButton = (Button)convertView.findViewById(R.id.button);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
// redefine the action for the button corresponding to the row
holder.mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// do something depending on position
performSomeAction(position);
// mark data as changed
MyAdapter.this.notifyDatasetChanged();
}
}
}
static final class ViewHolder {
// references to widgets
Button mButton;
}
}
If you're unsure about extending BaseAdapter, check out example List14 in ApiDemos. This techniques provides you with a flexible way to modify just about any aspect of your adapter, though it's quite some work.

Categories