i don't know why but when i add items to listview i get only the last item. E.G. if i write apple and than pear i get only pear and not apple and pear.
Why?
CODE:
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("IDDD:"+iddd);
System.out.println("IDDD:"+video2.getPic());
//holder.lw.setA
String commento= (holder.tw.getText().toString());
// holder.lw.setAdapter(adapter);
ArrayList<String> arrayList= new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, arrayList);
arrayList.add(commento);
// next thing you have to do is check if your adapter has changed
holder.lw.setAdapter(adapter);
adapter.notifyDataSetChanged();
System.out.println("LISTVIEW:"+arrayList);
}
});
in every click you decalre new list ArrayList<String> arrayList= new ArrayList<String>(); and it's false
you need to declate it out of the listener and in your listener just add the new items and use adapter.notifyDataSetChanged();
You are creating new list and adapter during every click so move the declaration and initialization of both, outside that function
Declare them outside getView(if Base or ArrayAdaoter) or createViewHolder (RecyclerAdapter)
initialize them inside constructor
add data to list and notify adapter
Better use view holder pattern in here where you use Existing instance of listview rather than recreating the new one all the time.
You don't need to do anything special, inside of onClick you need to get a reference to your ArrayAdapter and call the method add(T...) on it.
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("IDDD:"+iddd);
System.out.println("IDDD:"+video2.getPic());
String commento= (holder.tw.getText().toString());
ArrayAdapter adapter = (ArrayAdapter) holder.lw.getAdapter();
adapter.add(commento);
}
});
No need to even call notifyDataSetChanged because add already does that internally.
if you use this you can solve your problem
yourAdapter.(getCount()-1-position);
and this if you want to see the last items in front
public yourPoJo getItem(int position) {
return super.getItem(getCount()-1-position);
}
Related
Using API through I was adding data in spinner that are added but that are not displayed in spinner at a time, so how can I refresh activity and newly added data to show at a time? i can used this link ....https://github.com/jaredrummler/MaterialSpinner/blob/master/README.md
(Schoolschool:userDetails.getSchools()) {
schoolList.add(school.getSchoolName());}
schoolDropDown.setItems(schoolList);
schoolDropDown.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener<String>() {
#Override
public void onItemSelected(MaterialSpinner view, int position, long id, String item) {
}
});
Use this method
adapter.notifyDataSetChanged();
You need to define the Adapter using setAdapter(...), for the Spinner, too. This defines the layout to be used for each individual item. i.e.
schoolDropDown.setAdapter(new ArrayAdapter(getContext(), android.R.layout.simple_dropdown_item_1line, schoolList));
You only need to call setAdapter() once and you call adapter.notifyDataSetChanged() to update the data.
You need to set adapter to the spinner for displaying data
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, schoolList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
schoolDropDown.setAdapter(dataAdapter);
After data change you need to call
dataAdapter.notifyDataSetChanged();
Try like this:
cityAdapter = new ArrayAdapter<>(frgmActivity, android.R.layout.simple_spinner_dropdown_item, listCityNames);
cityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCitys.setAdapter(cityAdapter);
Or
Try below two lines :
String[] schoolsArr = schoolList.toArray(new String[schoolList.size()]);
schoolDropDown.setItems(schoolsArr);
Instead of this line: schoolDropDown.setItems(schoolList);
I have two classes , the first one is for the GUI , where I declared my listview and the adapter , and the setters , to call them from my second class .
public class AndroidGUIModifier implements IMyComponentGUIModifier, IFragmentEvents {
private transient ListView lv;
List<String> mydeviceslist;
ArrayAdapter<String> adapter ;
public void setAdapter(ArrayAdapter<String> adapter) {
this.adapter = adapter;
adapter.notifyDataSetChanged();
}
public void setMydeviceslist(List<String> mydeviceslist) {
this.mydeviceslist = mydeviceslist;
}
#Override
public void onCreateView() {
lv=(ListView) fragment.findViewById("xdevices") ;
mydeviceslist = new ArrayList<String>();
adapter = new ArrayAdapter<String>(fragment.getContext(),android.R.layout.simple_list_item_1,mydeviceslist);
lv.setAdapter(adapter);
In my second class I'll wait an event to receive the list that I want to load it in my listview , then I'll call the list setter to set the new received list and the adapter setter to update it , but it didn't work , nothing was displayed despite I receieved the list of devices in my log .
public class triprincipal extends BCModel {
public List<String> mydevices ;
BCEvent bcEvent;
final ArrayAdapter<String> adapter =guiModifier.getAdapter();
while (isRunning()) {
bcEvent = waitForBCEvent();
if (bcEvent.getID() == checkevent) {
mydevices = bcCommandSenderPlugin.getDevicesNames(); // here I get a list of my devices
Log.i("devices", mydevices.toString());
guiModifier.getFragment().getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
guiModifier.setMydeviceslist(mydevices);
guiModifier.setAdapter(adapter);
}
} );
In setMydeviceslist() do it like:
this.mydeviceslist.addAll(mydeviceslist);
adapter.notifyDataSetChanged();
Hope it will help you out.
adapter.notifyDataSetChanged() will not work in this case, as the value of the reference you have passed to the adapter doesn't actually change.
You will need to create a new Adapter and set it to the ListView to make it work. Change your setAdapter() to this :
public void setAdapter() {
this.adapter = new ArrayAdapter<String>(fragment.getContext(), android.R.layout.simple_list_item_1, mydeviceslist);
lv.setAdapter(adapter);
}
try to update the list in same fragment/activity and after update call notifyDataSetChanged() both in same activity/fragment ....donot set adapter on list repetedly......hope it helps
I want to fill the first listview line with an int from another method in my databasehelper, and the fill the rest of the view.
All I can find googleing this is how to do it with an ArrayAdapter
this is how i fill the view now:
new Handler().post(new Runnable(){
#Override
public void run(){
cursorAdapter = new CustomCursorAdapter(MainActivity.this, contactDBHelper.listAll());
listView.setAdapter(cursorAdapter);
}
});
what I want to do is to fill the first line with the coming int from this method.
contactDBHelper.countContact();
Any ideas?
try using ListVew.addHeaderView(View view)
ListView lv=(ListView) findViewById(R.id.listView1);
TextView header=new TextView(getApplicationContext());
header.setText("Header");
lv.addHeaderView(header);
You can override getItemViewType and getViewTypeCount methods if you want to display different views at the ListView. Here is a short example.
I have a public method in DatabaseHelper.java class like below:
public List<Presentation> getAllPresentations() {
List<Presentation> presentations = new ArrayList<Presentation>();
//
//
// some code
//
//
return presentations;
}
In my MainActivity.java I have added this lines:
btnLoad.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
List list = db.getAllPresentations();
ListView l;
l=(ListView)findViewById(R.id.list);
l.setAdapter(new ArrayAdapter<List>(this,R.layout.view_presentation, list));
}
});
BUT, something is wrong on the line:
l.setAdapter(new ArrayAdapter<List>(this,R.layout.view_presentation, list));
Can someone help me?
There are at least two problems:
In the context of an anonymous inner class (like new View.OnClickListener()), this refers to the instance of the inner class. The ArrayAdapter constructor needs a Context, so you must use MainActivity.this instead.
The type parameter of ArrayAdapter<T> must be the item type. So in this case, it should be ArrayAdapter<Presentation>.
So:
List<Presentation> list = db.getAllPresentations();
ListView l = (ListView)findViewById(R.id.list);
l.setAdapter(new ArrayAdapter<Presentation>(MainActivity.this, R.layout.view_presentation, list));
I have a listview. In it, each row had a text saying 0:00. But now, I added a button on my actionbar but then I got stuck. I don't know how to make the button create a new row, displaying 0:00
This is my code for the data in a row.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RowData rowdata_data[] = new RowData[]
{
new RowData("0:00")
};
RowdataAdapter adapter = new RowdataAdapter(this,
R.layout.listview_item_row, rowdata_data);
listView1.setAdapter(adapter);
listView1 = (ListView)findViewById(R.id.listView1);
}
this is my RowData class:
public class RowData {
String title;
public RowData(String title) {
this.title = title;
}
}
So how should I implement a button click to add another row?
Under addtionbutton: should be the method.
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle presses on the action bar items
switch (item.getItemId())
{
case R.id.additionbutton:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I don't know how to make the button create a new row
With your current solution it's not possible (in efficient way do it) because you're using static array of objects - it means array has fixed size (it means that you're assuming that size won't be changed at runtime).
But your goal is "slightly" different. You want after each click on Button increase number of objects by one. It means you don't know exactly how many rows you can have (or can be changed at runtime).
Due to things mentioned above you should (have to) use dynamic array with changeable size. For this reason you need to use as source of data List that is representation of dynamic array.
Basic algorithm:
Create new List with zero (or you can have by default one row at
start).
Then create public method in your adapter that will add new item to
collection and send request that Adapter should be refreshed (after you added new row into collection).
Assign OnClickListener to your Button and in onClick() method you'll use created method for adding new row into ListView.
Solution:
How to initialise ListAdapter:
// Activity-level variable scope
private List<RowData> items = new ArrayList<RowData>();
private RowdataAdapter adapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView1 = (ListView)findViewById(R.id.listView1);
// adding first item to List, it's optional step
items.add(new RowData("0:00"));
adapter = new RowdataAdapter(this, R.layout.listview_item_row, items);
listView1.setAdapter(adapter);
}
Method for adding new row into ListAdapter:
public void addRow(RowData newRow) {
// items represents List<RowData> in your Adapter class
this.items.add(newRow);
// sends request to update ListAdapter
notifyDataSetChanged();
}
How to update Adapter after click on Button:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// add new row to Adapter
adapter.addRow(new RowData("0:00"));
}
});
Hope i helped you to solve your problem.
you need to create a dataset that you can change so you need to make your array a class wide variable so you can add to it when you need to
when you add the new row you need to notify the adapter that something changed so you do this
adapter.notifyDatasetChanged();
and that should update your list