for Android Studio with Java
Listview have not correctly scrollview, so mean I select first item in Listview but Selecting first item both another a item. So mean, showing me two item, one's my selected item another random a item
What can I do about this situation
for Listview, selected item not correctly work
Automatically selected item element in list view
please refer this 2 link, I hope you getting your solution
This is first link
This is second link
Map<Integer, String> Green = new HashMap<>();
Map<Integer, String> white = new HashMap<>();
checkBox_TumParca.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(checkBox_TumParca.isChecked()){
for(int i=0;i<barkodlar.size();i++){
listemiz.getChildAt(i).setBackgroundColor(Color.GREEN);
Green.put(i,"Yeşil");
white.remove(i);
}
}else{
for(int i=0;i<barkodlar.size();i++){
listemiz.getChildAt(i).setBackgroundColor(Color.WHITE);
white.put(i,"Beyaz");
Green.remove(i);
}
}
}
});
ArrayList<Integer> posit = new ArrayList<>();
ArrayList<Integer> yesil = new ArrayList<>();
ArrayList<Integer> beyaz = new ArrayList<>();
listemiz.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
try {
String a=Green.get(position);
if (a!="Yeşil"){
listemiz.getChildAt(position).setBackgroundColor(Color.GREEN);
Green.put(position,"Yeşil");
white.remove(position);
}
else if (a=="Yeşil"){
listemiz.getChildAt(position).setBackgroundColor(Color.WHITE);
white.put(position,"Beyaz");
Green.remove(position);
}
}catch (Exception ex){
dialog("",ex.getMessage(),false);
}
}
});
How do I get the first element of an ArrayList from another class and assign it to another ArrayList?
public class PaymentScreenController {
public ListView<Customer> lvCustomer1;
public ArrayList<Customer> allcustomers;
public ArrayList<Customer> cusarray;
private Table tbl = new Table();
#FXML
public void initialize() {
allcustomers = tbl.getCustomers();
// Getting first element from 'allcustomers and assigning it to cusarray?
// I have tried cusarray = allcustomers.get(0) but that doesn't work?
}
}
And then assign cus array to a listview?
Any help would be appreciated thanks
try:
cusarray.add(allcustomers.get(0));
You can initialize the cusarray and add the item to the list:
cusarray = new ArrayList<>();
cusarray.add(allcustomers.get(0);
That should work for you.
Then for the ListView, I guess you'd want something like this:
lvCustomer1 = new ListView(FXCollections.observableArrayList(cusarray));
lvCustomer1.setCellFactory(param -> new ListCell<Customer>() {
#Override
protected void updateItem(Customer item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null || item.<function to get name>() == null) {
setText(null);
} else {
setText(item.<function to get name>());
}
}
});
Though I haven't tested the ListView bit.
EDIT: Now tested the code for the ListView and it works as intended.
Basically I'm using a RecyclerView with gridAdapter to load images from server. But the problem is items of RecyclerView are blinking in a quirky manner. I've tried all the possible solutions but none of them worked so far.
I tried to update glide version from 4.4.0 to 4.8.0 but it's futile. I also tried to disable the animations of RecyclerView but that couldn't help. Can anyone please help me to solve this issue?
Code:
GridLayoutManager gridLayoutManager = new GridLayoutManager(v.getContext(),3);
gridLayoutManager.setSmoothScrollbarEnabled(true);
gridLayoutManager.setItemPrefetchEnabled(true);
gridLayoutManager.setInitialPrefetchItemCount(20);
posts_rView.setLayoutManager(gridLayoutManager);
posts_rView.setItemAnimator(null);
posts_rView.setHasFixedSize(true);
gridPostAdapter=new StarredAdapter(timelineDataList);
posts_rView.setAdapter(gridPostAdapter);
Data Updation Code:
private Emitter.Listener handlePosts = new Emitter.Listener(){
#Override
public void call(final Object... args){
try {
JSONArray jsonArray=(JSONArray)args[0];
Needle.onMainThread().execute(() -> {
timelineDataList.clear();
swipeRefreshLayout.setRefreshing(false);
for(int i=0;i<jsonArray.length();i++){
try {
//JSONArray arr=jsonArray.getJSONArray(i);
JSONObject ob=jsonArray.getJSONObject(i);
post_username=ob.getString("_pid");
post_fullname=ob.getString("owner_fullname");
if(ob.has("owner_profPic"))post_profPic=ob.getString("owner_profPic");
else post_profPic="";
post_time=ob.getString("time");
post_link=ob.getString("img_link");
likes_counter=ob.getString("likes_counter");
comments_counter=ob.getString("comments_counter");
if(ob.has("caption")) post_caption=ob.getString("caption");
else post_caption=null;
//Skipping Private Posts
if(ob.getString("private_post_stat").equals("yes")&&!post_username.equals(my_username)) {
continue;
}
else
private_post_stat = ob.getString("private_post_stat");
comment_disabled=ob.getString("comment_disabled");
share_disabled=ob.getString("share_disabled");
download_disabled=ob.getString("download_disabled");
if(ob.has("short_book_content")) short_book_content=ob.getString("short_book_content");
else short_book_content=null;
society_name_adp=ob.getString("society");
addTimelineData(post_username,post_fullname,post_profPic,post_time,post_link,post_caption,
private_post_stat,comment_disabled,share_disabled,download_disabled,likes_counter,comments_counter,short_book_content,society_name_adp);
} catch (JSONException e) {
e.printStackTrace();
}
}
RecyclerView.Adapter adapter=posts_rView.getAdapter();
posts_rView.setAdapter(null);
posts_rView.setAdapter(adapter);
});
} catch (Exception e) {
Log.e("error",e.toString());
}
}
};
private void addTimelineData(String username,String fullname,String post_profPic,String time,String img_link,String caption,
String private_post_stat,String comment_disabled,String share_disabled,String download_disabled,String likes_counter,
String comments_counter,String short_book_content,String society_name_adp){
boolean isRepeated = false;
for(TimelineData data:timelineDataList){
if (data.getTime().equals(time)) {
isRepeated = true;
}
}
if(!isRepeated){
timelineDataList.add(new TimelineData(username,fullname,post_profPic,time,img_link,caption,private_post_stat,comment_disabled,share_disabled,download_disabled,likes_counter,comments_counter,short_book_content,society_name_adp));
gridPostAdapter.notifyDataSetChanged();
// posts_rView.scrollToPosition(gridPostAdapter.getItemCount()-1);
// posts_rView.scrollToPosition(0);
}
//gridPostAdapter.notifyItemInserted(timelineDataList.size()-1);
}
Adapter Class:
#Override
public void onBindViewHolder(StarViewHolder holder, int position) {
//loading img
Glide.with( parent.getContext()).asBitmap().load(arrayList.get(position)).apply(new RequestOptions()
.override(200, 200)
.dontAnimate()
.placeholder(new ColorDrawable(Color.parseColor("#20001919"))))
.thumbnail(0.1f)
.into(holder.img);
}
#Override
public int getItemCount() {
return arrayList.size();
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public int getItemViewType(int position) {
return position;
}
I do not quite understand yet why the flickering and repositioning to the first element is occurring in your case, as I do not see the updating policy of your RecyclerView. However, I would like to suggest something which might remove the problem.
I would like to suggest you remove the setInitialPrefetchItemCount and setItemPrefetchEnabled configurations while setting GridLayoutManager. This will aid in case of you are having a nested RecyclerView which is not your case.
You are setting adapter each time you are updating your data from the server. I guess you are calling the update action code when you are scrolling the items. Which you should not. Moreover, please remove setting up the adapter of your RecyclerView each time you are updating the dataset. Just use, notifyDataSetChanged after each of your updates. Do not clear the timelineDataList and append the new items instead, and then call notifyDataSetChanged on your adapter.
You are calling notifyDataSetChanged in addTimelineData after each insert to your list. This should be done only after adding all the elements to the list.
So I would like to propose the following code for setting your adapter and updating your RecyclerView. Please note that I have not tested the code and you might have to modify some errors which might occur.
The code for setting up the layout manager and the adapter.
GridLayoutManager gridLayoutManager = new GridLayoutManager(v.getContext(),3);
posts_rView.setLayoutManager(gridLayoutManager);
posts_rView.setHasFixedSize(true);
gridPostAdapter = new StarredAdapter(timelineDataList);
posts_rView.setAdapter(gridPostAdapter);
The code for updating the list.
private Emitter.Listener handlePosts = new Emitter.Listener(){
#Override
public void call(final Object... args){
try {
JSONArray jsonArray = (JSONArray)args[0];
Needle.onMainThread().execute(() -> {
// Do not clear the list. Just append the new data in the list instead
// timelineDataList.clear();
swipeRefreshLayout.setRefreshing(false);
for(int i = 0; i < jsonArray.length(); i++){
try {
JSONObject ob = jsonArray.getJSONObject(i);
post_username = ob.getString("_pid");
post_fullname = ob.getString("owner_fullname");
if(ob.has("owner_profPic")) post_profPic = ob.getString("owner_profPic");
else post_profPic = "";
post_time = ob.getString("time");
post_link = ob.getString("img_link");
likes_counter = ob.getString("likes_counter");
comments_counter = ob.getString("comments_counter");
if(ob.has("caption")) post_caption = ob.getString("caption");
else post_caption = null;
//Skipping Private Posts
if(ob.getString("private_post_stat").equals("yes")&&!post_username.equals(my_username)) {
continue;
}
else
private_post_stat = ob.getString("private_post_stat");
comment_disabled = ob.getString("comment_disabled");
share_disabled = ob.getString("share_disabled");
download_disabled = ob.getString("download_disabled");
if (ob.has("short_book_content")) short_book_content = ob.getString("short_book_content");
else short_book_content = null;
society_name_adp = ob.getString("society");
addTimelineData(post_username, post_fullname, post_profPic, post_time, post_link,post_caption, private_post_stat, comment_disabled, share_disabled, download_disabled, likes_counter, comments_counter, short_book_content, society_name_adp);
} catch (JSONException e) {
e.printStackTrace();
}
}
gridPostAdapter.notifyDataSetChanged();
});
} catch (Exception e) {
Log.e("error",e.toString());
}
}
};
private void addTimelineData(String username, String fullname, String post_profPic, String time, String img_link, String caption, String private_post_stat, String comment_disabled, String share_disabled, String download_disabled, String likes_counter, String comments_counter, String short_book_content, String society_name_adp) {
boolean isRepeated = false;
for(TimelineData data:timelineDataList){
if (data.getTime().equals(time)) {
isRepeated = true;
}
}
if(!isRepeated){
timelineDataList.add(new TimelineData(username,fullname,post_profPic,time,img_link,caption,private_post_stat,comment_disabled,share_disabled,download_disabled,likes_counter,comments_counter,short_book_content,society_name_adp));
// Do not call notifyDataSetChanged each time you are adding an item. This will be called in the call function above. So remove this line.
// gridPostAdapter.notifyDataSetChanged();
}
}
Try to add some pagination in the server side API if it is not there already so that you might fetch the next 20 dataset instead of fetching the whole data again when you are scrolling down.
Hope that helps.
I have an custom adapter that is giving me problem. When I add or edit something it updates the listview but when I delete it doesn't.
Code when I remove something:
private ArrayList<Teams> m_orders = null;
private TeamsAdapter m_adapter;
private ListView lstv;
...
private void deleteTeam(int indexRemove){
hasKeys.remove(hasKeys.indexOf(m_orders.get(indexRemove).getTeamName()));
Menu.teams.remove(m_orders.get(indexRemove).getTeamName());
m_orders.remove(indexRemove);
m_adapter.notifyDataSetChanged();
}
I tried use a Runnable, but without success.
private Runnable returnRes = new Runnable() {
#Override
public void run() {
if(m_orders != null && m_orders.size() > 0){
m_adapter.notifyDataSetChanged();
for(int i=0;i<m_orders.size();i++)
m_adapter.add(m_orders.get(i));
}
m_adapter.notifyDataSetChanged();
}
};
My onCreate method:
lstv = findViewById(R.id.teamsList);
m_orders = new ArrayList<Teams>();
this.m_adapter = new TeamsAdapter(this, R.layout.row, m_orders);
lstv.setAdapter(this.m_adapter);
You have to remove an item from the ArrayList which is inside the Adapter class. So you need to write your delete function inside the adapter class. In this way will able to delete items and update listview by calling notifyDataSetChanged() method.
You are creating an m_adapter with m_orders dataset, but you are removing item from m_teams dataset, make no sense, they are different instances.
final List<Teams> mTeams = new ArrayList<>();
TeamsAdapter mAdapter;
onCreate() {
mAdapter = new TeamsAdapter(this, R.layout.row, mTeams);
ListView listView = (ListView) findViewById(R.id.teamsList);
listView.setAdapter(mAdapter);
}
addTeams(Collection<Teams> items) {
mTeams.addAll(items);
mAdapter.notifyDataSetChanged();
}
deleteTeam(int index) {
mTeams.remove(index);
mAdapter.notifyDataSetChanged();
}
I've created a ListView which is using FastScroll. (see pic) When the user clicks any of the below Button (viz. All Tracks, Artists, Album), everytime the following custom ArrayAdater is called
ArrayAdapter<String> adapter = new ScrollIndexListAdapter(Listing.this, elements);
//Code for ScrollIndexListAdapter is below
and the same ListView is updated.
PROBLEM: According to my investigation in Android, the getSections() method is called only once (i.e. only when the first time ScrollIndexListAdapter is called).
This time the sections are populated & the fastScrolling works perfectly.
But when I update the ListView by clicking on Artists/Album, the getSections() method is not called. So the older sections are used, and the FastScrolling still shows previews of old alphabets.
So, how can I make sections get updated everytime when the ListView is updated?
There is a setSections() method, but I'm not able to find how to use it.
Code for ScrollIndexListAdapter class:
public class ScrollIndexListAdapter extends ArrayAdapter implements
SectionIndexer {
// Variables for SectionIndexer List Fast Scrolling
HashMap<String, Integer> alphaIndexer;
String[] sections;
private static ArrayList<String> list = new ArrayList<String>();
public ScrollIndexListAdapter(Context context, ArrayList<String> list) {
super(context, android.R.layout.simple_list_item_1, android.R.id.text1,
list);
this.list.clear();
this.list.addAll(list);
/*
* Setting SectionIndexer
*/
alphaIndexer = new HashMap<String, Integer>();
int size = list.size();
for (int x = 0; x < size; x++) {
String s = (String) list.get(x);
// Get the first character of the track
String ch = s.substring(0, 1);
// convert to uppercase otherwise lowercase a -z will be sorted
// after upper A-Z
ch = ch.toUpperCase();
if (!alphaIndexer.containsKey(ch)) {
alphaIndexer.put(ch, x);
}
}
Set<String> sectionLetters = alphaIndexer.keySet();
// create a list from the set to sort
ArrayList<String> sectionList = new ArrayList<String>(
sectionLetters);
Collections.sort(sectionList);
sections = new String[sectionList.size()];
sectionList.toArray(sections);
}
/*
* Methods for AphhabelIndexer for List Fast Scrolling
*/
#Override
public int getPositionForSection(int section) {
String letter = (String) sections[section];
return alphaIndexer.get(letter);
}
#Override
public int getSectionForPosition(int position) {
String letter = (String) sections[position];
return alphaIndexer.get(letter);
}
#Override
public Object[] getSections() {
return sections;
}
}
It seems that latest FastScroll version doesn't have that problem. But, in your case, there's a turn around. When setting the adapter to the ListView, disable and enable the fast scrolling. See the code below:
adapter = new ScrollIndexListAdapter(this, data);
listView.setFastScrollEnabled(false);
listView.setAdapter(adapter);
listView.setFastScrollEnabled(true);