I am trying to dynamically add information to a ListView. The information I am adding consists of a "Device Name" (the main item) and "MAC Address" (the sub item). An example from online is below. Note: I want to replace Item 1 with a device 1's name, sub item 1 with device 1's MAC address, and so on. This MUST be done dynamically because the list is being populated as devices are scanned for.
.
Before this is marked as a repeat, I have looked at the following questions and they have not helped me: Adding ListView Sub Item Text in Android, How to add subitems in a ListView, Adding Items and Subitems to a ListView
The conclusion I have come to through reading these questions is that I need to implement a custom ArrayAdapter and override the getView() method. I have created a custom layout with two text views in it:
cyan_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/main_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/cyan"/>
<TextView
android:id="#+id/sub_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/dark_cyan"/>
</LinearLayout>
I then try to create a custom ArrayAdapter in my Activity class, but I am lost as to what to put in my public View getView(final int position, View convertView, ViewGroup parent) method. Additionally, is creating a custom ArrayAdapter necessary if all I am trying to do is add a textview sub item?
The answer to your question is: NO, you don't need to create a custom ArrayAdapter if you just want to add items. I recommend, however, creating it if your layout is customized, as you'll gain so much control over the items you're displaying. You didn't add your code where you create your ArrayAdapter, but in your case I'd use this constructor. The important part is the third parameter: In your activity, you should store an ArrayList with the initial items you're adding to your ArrayAdapter, then, if you want to add a new item, you simply add it to the ArrayAdapter and call notifyDataSetChanged() on your adapter. Simply doing that, your item will be added to the layout and displayed. If you need to override the GetView method for your own ArrayAdapter, I recommend this link, it helped me understanding the whole thing.
are you searching some listview example in google like those tutorials :
http://www.vogella.com/tutorials/AndroidListView/article.html
http://www.mkyong.com/android/android-listview-example/
I think they explain step by step how to create a list adapter
You need to add getter method into your Adapter
YourAdapter ...{
List<Device> items = new ArrayList<Device>;
public List<Device> getItems(){
return items;
}
}
then change item that you need
...{
//for 1s item
Device item = getItems().get(0);
item.setTitle(macAdress)
}
and call notifyDataSetChanged for your adapter
...
yourListView.getAdapter().notifyDataSetChanged();
}
Thats it. Now you are able to change your list data.
And for your question, i think yes. Is better to create your own adapter in order to have simple possibility to exentd it later. And in your case (if you dont want to change your adapter after each title change) you deffinetly need custom one. Cheers
Related
I have an app that's getting information from an API request and then displaying a list of devices. After several hours of combing through documentation, I cannot figure out how to format the View that is created from the ArrayAdapter. Essentially, if the device has an error, I want to display a red circle to the right of the button and display a green button if there is no error.
deviceList is the name of a ListView that I am trying to display my list of buttons inside of. deviceNames is an array of strings that contains the names of the devices.
The TextViews that are created are also clickable, which is what the onItemClickListener is handling. This section works, but I wanted to leave it in because I do need the buttons to start an activity that displays device-specific information.
Ideally I would like to essentially create a template that I can just change the values of the text and the color of the indicator for
Below is my code:
// List of device names
val listView: ListView = findViewById(R.id.deviceList)
val arrayAdapter1: ArrayAdapter<*>
arrayAdapter1 = ArrayAdapter(
this#Homepage,
R.layout.device_button,
deviceNames
)
listView.setAdapter(arrayAdapter1)
listView.onItemClickListener =
AdapterView.OnItemClickListener { parent, view, position, id ->
val pos = position
println(pos)
val device = jsonArray.getJSONObject(pos)
val ID = device.get("id") as String
println(ID)
goToDeviceDetail(ID)
}
Below is the XML file for device_button. I tried to add formatting here and essentially create a template for a button that would allow me to change the text and the color of the indicator, but it got mad that it wasn't just a TextView.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:textColor="#25383C"
/>
Below is the button that I would like for it to look like. I'm likely going to just make the background a solid color rather than the image that is in the below picture:
I would say the biggest problem is your using a simple API for a more complex problem. It is entirely possible do it with a ListView and ArrayAdapter. But I would highly recommend looking into RecyclerView/RecyclerView.Adapter
The way it works out is...
RecyclerView.Adapter binds your list of data ie Devices to the individual RecyclerView.ViewHolder
The ViewHolder would inflate your xml layout that contains the button. You then have access to all View contained in that layout easily.
You then can put listeners on the button.
The Adapter then can be setup to receive new data, when received it can rebind the data that has changed.
Say the user clicks one of the device buttons it does a task. When it gets back it will say hey Adapter I have a new List for you.(The list now contains the "fixed" device).
ViewModel(contains observable data)->Fragment/Activity(Observers the data)->Adapter(Receives the data)->ViewHolder(Displays the data)->Activity("Fixes the data")->ViewModel->...loops
Here is a very good example.
https://medium.com/#atifmukhtar/recycler-view-with-mvvm-livedata-a1fd062d2280
If you really want to keep using the ListView and ArrayAdapter you are receiving the clicked view here.
OnItemClickListener {
/*Parent of the view*/ parent,
/*The view clicked*/ view,
/*position of data*/position,
/*id of the view clicked*/ id
->{
view.findById(R.id.text_view);
//onClick
}
}
With that you know what has been clicked so you know what has to be changed later when you get back from your other Activity.
When we build a ListView in android studio, we need to use an ArrayAdapter.
What is the task of second argument in constructor of ArrayAdapter ?
I cannot understand what is android.R.layout.simple_list_item_1 used for ?
This layout describe how the item list looks like.Maybe you want every item contain a text view and image.You should specify these details in this layout.
android.R.layout.simple_list_item_1 is a layout in which the data from your ArrayAdapter gets populated(added).
This is the id of the layout that you need to use for populating each of the item in the list. If it says android.R.layout that means you are going to use one of the standard android layouts. simple_list_item_1 This is the name of the file which will populate each row of the list. try changing this to simple_list_item_2 to see how the layout in list changes.
You can also use your custom adaptors and custom layouts(which would be in majority of the cases in day to day apps).
For full list of standard layouts available Go here
This argument defines how list items would appear in ListView, There are many layouts you can also try them out or you can make your own custom layout to modify apperance of listitems in Listview by using CustomAdapter.
1.create custom layout name custom_layout.xml & paste bellow code
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="16sp" >
</TextView>
Use like R.layout.custom_layout instead of android.R.layout.simple_list_item_1
I'm inexperienced with Android and working on an app that simply displays a school bell schedule in the form of a table and hopefully highlight/apply a background color to a ListView item representative of the current period.
Currently I have attempted to do so via setting a listSelector color:
From activity_bell_schedule.xml:
<ListView android:layout_height="wrap_content"
android:layout_width="0dp"
android:id="#+id/lstPeriods"
android:layout_weight=".4"
android:listSelector="#color/colorAccent"
android:divider="#color/colorPrimaryDark"
android:dividerHeight="1dp"/>
However, I then need to be able to lock/force the selection to remain as the ListView item of the current period. I tried by setting it in a function called through the onCreate function of my BellScheduleActivity class and this(I used 2 for the position for testing purposes):
From BellScheduleActivity.java:
lvp.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println(position);
lvp.setSelection(2);
lvp.setItemChecked(2, true);
lvt.setSelection(2);
}
});
I have tried a variety of methods involving selection highlighting and selecting. If anyone has any idea what I am doing wrong or has another approach that might work, I would really appreciate it.
You can do this by setting a custom adapter to your listview. In that adapter you can define custom behavior.
In your case it would be to override the following methods in the adapter-
getItemViewType()
getViewTypeCount()
getView()
In that adapter you can define your two view types. And in getView() you can call getItemViewType() and define custom behavior.
Example for getItemViewType() is here
Another example which is similar to your question
This should solve your issue. Let me know if it works. This is like the default pattern in displaying custom listViews.
P.S. I would recommend you use RecyclerView as it is the newer(and better)ListView and is the new standard in Material Design.
i'am using a list in my main activity
and i don't want to use android list , i want to create my own list ,already i created my custom Adapter that enables me to use images and text in any row of list but i want to disable the divider of the list and the hover mode
it's mean that i don't want to have something like this pic
it is not my project screenshot
thanks in advance,
related with Avoid ImageView hover state inside Listview if List Item is pressed
you can use :
set empty background
android:cacheColorHint="#android:color/transparent"
or
android:cacheColorHint = "#00000000"
to remove divider between items in the list use :
myListview.setDivider(null);
or XML:
android:divider="#null"
android:dividerHeight="0dp"
in your adapter, override the method isEnabled(int position) and have it return false for this item.
Since you already have your own CustomListAdapter, do the following in your onCreate() method:
ListView list=(ListView)findViewById(R.id.list); //or whatever the id of your listview is
// Getting adapter by passing xml data ArrayList
YourCustomAdapter adapter = new YourCustomAdapter(this, listDataToBeDisplayed);
list.setAdapter(adapter);
Try code below to disable the highlight, you may change the text color as well
ListView.setSelector(new ColorDrawable(Color.TRANSPARENT));
This is my list i want My problem is when i scroll list view then the check boxes(which are the items of this List ) are automatically checked
ex - if i checked first then 4 automatically being checked.
My first goal:
1. want to stretch my list to full i will wrap it into Scrollview how
2. i can prevent it to automatically checked
.
<ListView
android:id="#+id/ListViewProducts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_span="2"
android:clickable="true"
android:isScrollContainer="true"
android:saveEnabled="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarSize="10sp"
android:scrollbars="vertical" >
</ListView>
Create your own BaseAdapter.
Keep in mind, that ALL views in your listview you see are temporary. They will be recycled when you'll scroll away. The reason is - you can have >9000 elements in your list. So, the way you create views must depend on some kind of (!) data.
Here is nince tutorial on how to create your own list.
Make X-th checkbox depend on X-th boolean in the list. A bit confusing first time I know, but this is the best way.
class MyAdapter extends BaseAdapter{
List<boolean> myCheckBoxes;
boolean getItem(int arg0){
return myCheckBoxes.get(arg0);
}
View getView(int arg0, View arg1, ViewGroup arg2){
...
...//See article
myView.setChecked(getItem(arg0));
...
return myView;
}
And in your activity
ListView myListView;
...
myListView.setAdapter(new MyAdapger(...));
You can't put a listview into a scrollview, two views scrolling in the same direction will not work nicely. Just put the listview in your non scrolling layout (frame- , list-, relativelayout).
Use an Adapter that sets every listview's row's views according to the data to be displayed.
ListView already extends ScrollView and doesn't need to have another one to surround it.
try looking at this post on creating custom listView items. you can implement a checkBox in them and make is have android:checked="false"
ListVew already extends ScrollView no need to implement it on ListView
for AutoCheck follow this link:
Check box checked Automatically in listview when scrolling the list.
There's no need to implement scrollview in listview becoz it is already extends to scrollview.
I think your listview is not able to handle the recycling of items properly.So to solve this problem go through the below link.
Getting an issue while checking the dynamically generated checkbox through list view