so I have this huge problem with applying a strikethrough on a ListView item while populating it from the List of my objects.
private void setListItems() {
DatabaseHandler db = new DatabaseHandler(this);
List<ToDoTask> taskList = db.getAllTasks();
for (ToDoTask tt : taskList) {
this.listAdapter.add(tt.getName());
}
this.listAdapter.notifyDataSetChanged();
}
This part of the code populates my ListView. Now i want to add a strikethrough to item if
tt.getActive() == 0
I don't know how can i get a TextView item from my ListView. I tried in my for loop to:
TextView textView = (TextView) this.listView.getChildAt(this.listView.getCount() -1);
but the app crashes.
The only solution that didn't crash my app was:
TextView textView = this.listAdapter.getView(this.listView.getCount() -1, null, null)
but it didn't actually change anything in the ListView. I think it's not referencing the object itself.
Here's some code that instantiates my variables:
this.listView = (ListView) findViewById(R.id.listViewItem);
this.listItems = new ArrayList<String>();
this.listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, this.listItems);
this.listView.setAdapter(this.listAdapter);
The list view is not in charge of instantiating or modifying the underlying state of the views it contains. That's the job of the adapter. When you scroll a ListView, it calls the adapter method getView() and the adapter will hand it a row (possibly a recycled view from the rows that scrolled off-screen).
Inside the getView() method, the adapter should consult the underlying data set and modify the row as needed - that would include crossing out text, or setting any visual properties of the row content.
Related
How to add two columns in the listview and get the value of the first column.
I have Adapter that receives the data from a select and this data is added in the listview.
I would like the listview to be two columns and whenever I click on a row it takes the value of the first column
My listview is being populated like this:
adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItemMultipleChoice, observacao);
lv.Adapter = adapter;
lv.ChoiceMode = ChoiceMode.Multiple;
you should use RecycleView and set Recycleview as GridView by creating GridLayoutManger reference and also you can specify column number in it.
recyclerView.setLayoutManager(new GridLayoutManager(getContext(),2));
adapter = new FoodAdapter( context,filteredList);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
There is official document about Creating Custom Row Layouts for Xamarin Android.
Please follow that official tutorial to create a custom layout for the item of ListView and use your custom adapter for it.
In the GetView method of your custom adapter, you can inflate your row layout:
View view = convertView;
if (view == null)
view = context.LayoutInflater.Inflate(Android.Resource.Layout.YourCustomLayout, null);
I would like the listview to be two columns and whenever I click on a row it takes the value of the first column
In the ItemClick event of your ListView, you can get the position of the selected item:
private void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var position = e.Position;
}
The position can be used for matching the relative item of your List, which is added into the ListView, then you can find your data there.
I have a list of items: List<SomeObject> items that is mapped to an adapter to show on a ListView. When I set a new field for a specific item in that list and call adapter.notifyDataSetChanged() then list doesn't update. For example, I want to show a date TextView in a specific row in the ListView by setting
item.get(position).showDate(true);
adapter.notifyDataSetChanged();
The view doesn't show until I scroll away from that row and back to it (I'm assuming because of recycling).
Doing list.setAdapter(adapter); works but the entire view flashes and there shouldn't be a reason to re-set the adapter.
How can I get the list to update without scrolling?
You could try to change the List that is referenced in the adapter. Add something like this to your adapter:
public void updateData(List<SomeObject> dataItems) {
this.mDataItems = dataItems;
notifyDataSetChanged();
}
And then:
items.get(position).showDate(true);
adapter.updateData(items);
you can use adapter.getitem(postion) to get SomeObject and refresh it.
SomeObject sobj = (SomeObject)adapter.getItem(position);
sobj .showDate(true);
adapter.notifyDataSetChanged();
I have a problem with my custom list adapter.
My main activity consist of a ListView, Button and a EditText field.
When the user inputs something in the EditText field, and presses the button. The text should
appear in the ListView. For the ListView i have made a custom adapter. That at the moment only consist of a TextView, but i will later add more stuff so i can not use a standard adapter.
The problem is that when the user types something, and presses the button, the string is placed in the ListView, (great!), but the problem is when the user presses again, that the old text is replaced by the new (the list has 2 items, both contain same text). When the user adds a third item to the list, this item replaces the old ones, and i now have 3 of the newest in the list?
So the problem is that the newest item replaces the old ones?
In my mainActivity i have this:
private ListView list_messages;
private Button send_button;
private TextView input_message;
public Custome_list_item adapter;
public ArrayList<String> message_holder;
message_holder = new ArrayList<String>();
adapter = new Custome_list_item(this,message_holder);
list_messages.setAdapter(adapter);
The custom list adapter only has a getView, like this:
LayoutInflater inflater = context.getLayoutInflater();
View rowView= inflater.inflate(R.layout.list_row, null, true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.message_in_list_row);
ImageView imageView = (ImageView) rowView.findViewById(R.id.user_profile);
TextView timeView = (TextView) rowView.findViewById(R.id.date_and_time);
txtTitle.setText(message_holder.get(message_holder.size()-1));
//imageView.setImageResource(imageId);
timeView.setText("DD:MM:YY");
return rowView;
when the user clicks the button in the main activity, i do this:
mainActivity.message_holder.add(item);
mainActivity.adapter.notifyDataSetChanged();
Any help is appreciated
Your getview is using size in the .get function, so it will always return the last string in the array as the text. It should be using the position passed in to the call.
Try Changing
txtTitle.setText(message_holder.get(message_holder.size()-1));
to
txtTitle.setText(message_holder.get(position));
Because position will replace the view's position to the item,i.e. On first item the txtTitle from first adapter position will be displayed.On second listItem the second Adapter View will be displayed and so on.
I am populating listView using webservice; I want to display a message when there is no result. I used following code on activity but it is working fine; But when data is being loaded it still shows empty message in listview.
#Override
public void onContentChanged() {
super.onContentChanged();
View empty = findViewById(R.id.empty);
TextView empty1 = (TextView) findViewById(R.id.empty);
empty1.setText("No Channels Found!");
ListView list = (ListView) findViewById(R.id.listView1);
list.setEmptyView(empty);
}
Try to set the visibility of the empty view to Invisible after list is loaded. empty.setVisibility(View.INVISIBLE);
The empty view will be shown whenever your adapter returns a count of 0. That will include the time when your data is loading. If you don't want that behavior then don't set an empty view until after your data has loaded.
I'm using a MergedAdapter to group two custom adapters (ArrayAdapter derived) along with a section header for each one into a single ListView. It is working fine, but now I need to display a TextView saying "No Data" for the section that has no items, e.g. ArrayAdapter is empty.
What's the best approach for this? The code that sets the ListView binding is like this:
ArrayList<ItemOne> firstItems = getFirstGroupItems();
ArrayList<ItemTwo> secondItems = getSecondGroupItems();
ItemOneAdapter firstAdapter = new ItemOneAdapter(this, this.firstItems);
ItemTwoAdapter secondAdapter = new ItemTwoAdapter(this, this.secondItems);
MergeAdapter adapter = new MergeAdapter();
adapter.addView(createGroupSeparator(R.string.first_section_header)); //Just creates a TextView
adapter.addAdapter(firstAdapter);
adapter.addView(createGroupSeparator(R.string.second_section_header)); //Just creates a TextView
adapter.addAdapter(secondAdapter);
listView.setAdapter(adapter);
In case of an empty list, you could make the first item read "No Data", but this usually makes the code unmanageable. I would suggest you add an invisible TextView to your screen with the message; this is only displayed when the list is empty.