How to add two columns in the listview - android

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.

Related

notifiyDataSetChanged() won't update adapter with new fields

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();

Cannot display empty textview in List Fragmant when the list has no items

I am displaying a list in a listfragmant inside a ViewPager. I have looked for varoius solutions but none worked for me.
Solution 1-
if ((ArrayList) myList.size() > 0) {
setListadapter(myAdapter);
} else {
ListView lv = (ListView)findViewById(android.R.id.list);
TextView emptyText = (TextView)findViewById(android.R.id.empty);
lv.setEmptyView(emptyText);
}
Solution 2-
if ((ArrayList) myList.size() > 0) {
setListadapter(myAdapter);
} else {
setEmptyText("No Items");
}
When the list is empty it shows nothing and whole page is empty.I have defined the requisite XML too.
You still have to set the adapter. If the adapter is empty, then the ListFragment will automatically hide ListView and show the empty view. So, the proper way to do it would be just:
setEmptyText("No Items");
setListadapter(myAdapter);
you have three ways:
1) create fragment and add ListView to it's layout then behind Listview put Textview so when List was empty,TextView will appear.if list has child then TextView not appear.
2) if your array has no item , add one Item to your array with any text( like :"No Items") and set adapter for listview.
3) set header for your ListView(layout can be textview that you want)

How to strikethrough ListView item while populating ListView

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.

How to move multiple selected items from listview to another listview

I have two listviews SAMPLE IMAGE HERE.i want to move items from one listview to another.
i have two buttons,"move to right" and "move to left".
Its a multi selection listview.after select items we need to move this items to another listview.
First listview data from database.code shown below
public void fillcategory() {
Cursor cursor = dataBase
.select("SELECT * FROM t_Category ORDER BY CategoryName");
lacategory = new list(this,
android.R.layout.simple_list_item_activated_1, cursor,
new String[] { "CategoryName" }, new int[] { android.R.id.text1 },1);
lvcategory.setAdapter(lacategory);
lvcategory.setOnItemClickListener(new listclick());
lvcategory.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}
There are twio options you can do this, first by adding checkbox in each listitem and then populate the other listview with selected items. for this here is good tutorial Multiple Check
Second, you can set you listview property to multiple_select in adapter and then populated those selected item through position in other listview.
I hope atleast one will work.

Android delete a row containg 2 lists by click of an image in that row in list view

I have a list view which is populated using a custom adapter.The list view contains 2 lists and an image.Format- list list image.
I want to delete items from both the lists at the same time when that button is clicked.
I tried using onclick and onitemclick but the app crashed.
How do i delete both the lists at the same time?The list are not saved in the date base.Items are stored in an Vector.
What is use of index and view?
when custom adapter extends Base Adpater we get a getView method which contains a parameter position .Is this same as index value?
code-holder.imgBtn.setOnClickListener(new ImageView.OnClickListener() {
#Override
public void onClick(View arg0) {
Index index = (Integer) arg0.getTag();
if(index>=0){
prescriptionRemoval = ProgressDialog.show(AddToCartActivity.this, null, getResources().getString(R.string.prescriptionRemoval));
String removeDrug=drugNameList.get(index);
String removePrescription=prescriptionList.get(index);
String removeTotal=removeDrug+removePrescription;
listCheck.remove(removeTotal);
drugNameList.remove(index.intValue());
prescriptionList.remove(index.intValue());
notifyDataSetChanged();
handler.sendEmptyMessage(DIALOG_PRESCRIPTION_REMOVAL);
}
}
})
Found the answer.Get the position and remove both lists with the position value
and notifydatasetchanged()

Categories

Resources