deleted Item place in ArrayAdapter in my ListView left empty - java

I used animated for deleting Item in ArrayAdapter in my ListView.
but when i deleted one row, It's place remained empty and lower box was not coverd empty place.
what can i do The lower box to come up and The stick-top box?
what can I do?
listViewHome.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView<?> parent, View view, final int position, long id) {
Log.i("######", "setOnItemClickListener: " + position);
view.animate().alpha(0f).setDuration(1000).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
Log.i("######", "onAnimationEnd: " + position);
contacts.remove(position);
adapter.notifyDataSetChanged();
super.onAnimationEnd(animation);
}
});
}
});
If I don't use animate everything is ok but when I use animate for deleting Its place Item remained empty and lower box was not coverd empty place.

Seems to be ok. Another way you can try (which is actually equal with your way) is:
adapter.remove(yourArraylist.get(position));
adapter.notifyDataSetChanged();
If it still not working try this:
Object toRemove = adapter.getItem([position]);
adapter.remove(toRemove);
This might help Delete Items android ListView Tutorials

Related

set background Color of view on List and Save when scrolling

lv_monsterLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setBackgroundColor(GREEN);
}
enter image description here
This works to set the background color but once I scroll up the color goes away,any help as to how I can save the color of the item i clicked when scrolling would be great!
Add a field color in your Monster model, set color on item click and while rendering check the color value and set the view color accordingly.
In the adapter class, I did
oneMonsterLine.setBackgroundColor(p.getColor());
I then went to my Monster class
p.setColor(WHITE);
Now, I can go to item click
lv_monsterLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
adapter.getItem(position).setColor(GREEN);
Log.d(TAG, "onItemClick: position" + position);
adapter.notifyDataSetChanged();
}
});
I'm sure there's a better way but this got the job done feel free to drop any easier way of doing this.

Set visibility TableLayout based item selected on Spinner

I want to make Spinner to set Visibility of table, i have 2 Array String "cuboid and cylinder". if i select Cuboid , cubeT table is visible and cyclinderT table is Invisible. and if i select Cylinder , cylinderT table is Visible and cubeT is Invisible.
Sample code welcome. Thank you for your time.
You can set an OnItemSelectedListener to your Spinner then using the int position argument to decide what action to take.
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
switch (position) {
case Cuboid:
cubeT.setVisibility(View.VISIBLE);
cylinderT.setVisibility(View.GONE);
break;
....
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) { }
});
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String text = ((Spinner)spinner).getSelectedItem().toString();
if (Intrinsics.areEqual(text, "Cuboid")) {
//Your code here to set your "table" as cubeT if it's image in imageview
//if it's a "tableLayout" you may create 2 different layouts included and..:
setContentView(R.layout.your_cubeT_layout);
} else if (Intrinsics.areEqual(text, "Cylinder")) {
setContentView(R.layout.your_cyclinderT_layout);
}
} //when it comes to use different layouts on the same activity, generally suggestions made over fragments to make your code more dynamic but i don't know how to do that...
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
I'm not using java since years so i might code bad... So no warranty!

How to cancel the selected state of the ListItem I have clicked after I click it again?

In my app, I have a listView whose item is choice mode is singlechoice. When I click an item, its background color gets dark. When I click it again, it is still with dark background. I want it to come back to the previous background color after I click again.
Now my solution is to use the myList.setSelector(new ColorDrawable(Color.TRANSPARENT)) function every time I click the item.
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
myList.setSelector(new ColorDrawable(getResources().getColor(R.color.gray_white)));
if (position==prePosition){
if(isSelected==UNSELECTED){
isSelected=SELECTED;
if (position>lock_listItems.size())
floatingActionButton.setVisibility(View.VISIBLE);
}
else{
isSelected=UNSELECTED;
floatingActionButton.setVisibility(View.INVISIBLE);
myList.setItemChecked(position,false);
//view.setBackgroundColor(getResources().getColor(R.color.white));
myList.setSelector(new ColorDrawable(Color.TRANSPARENT));
}
}
else {
isSelected=SELECTED;
if (position>lock_listItems.size())
floatingActionButton.setVisibility(View.VISIBLE);
else
floatingActionButton.setVisibility(View.INVISIBLE);
prePosition=position;
}
myList.forceLayout();
}
I think this solution is not so effective. Any better solution?
Take an arraylist.
First check the clicked position is present in the arraylist or not by using
Collections.contains(position);
If it returns true, don't do anything. Otherwise do your stuff. You can do as below:
ArrayList<Integer> myPositionClickedList = new ArrayList<Integer>(); // this should be as global variable
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(!myPositionClickedList.contains(position)) {
myList.setSelector(new ColorDrawable(getResources().getColor(R.color.gray_white)));
//do your stuff here........
myPositionClickedList.add(position);
}
}

Runnable changes wrong cell

I have got a list view with 9 rows in it. Every row has two TextViews and a ImageButton which plays a song specific for that row. If it is playing one of the two TextViews should change color and change the text every second to get a result like '1:12 - 7:35'. And that's where my problem lies.
The first time the list view loads all elements that are on screen work fine but whenever I scroll down, tap on the playButton it highlights the wrong cell. Probably because list view's position returns the position on the screen and not the position in the list.
private MediaPlayer mp;
private Handler handler;
private int playingCellPosition = -999;
public View getView(final int position, View convertView, final ViewGroup parent) {
// Find the oefening to work with
final Oefening currentExercise = myExercises.get(position);
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View itemView = vi.inflate(R.layout.cell, null);
// Get textviews
final TextView durationTextView = (TextView) itemView.findViewById(R.id.cell_duur);
final Runnable updateDurationTextView = new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable(){
public void run(){
Oefening playingExercise = currentExercise.get(playingCellPosition);
TextView durationTextViewToUpdate = (TextView) parent.getChildAt(playingCellPosition).findViewById(R.id.cell_duur);
durationTextViewToUpdate.setText(getFormattedCurrent(mp.getCurrentPosition()) + " - " + playingExercise.getDuration());
durationTextViewToUpdate.setTextColor(Color.parseColor("#b71393"));
}
});
handler.postDelayed(this, 1000);
}
};
ImageButton playButton = (ImageButton) itemView.findViewById(R.id.cell_playButton);
playButton.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
if (mp.isPlaying()) {
// Pause it here. Not very important to this problem since it occurs when it starts playing and not when it stops
mp.pause();
v.setBackgroundResource(R.drawable.play_icon);
durationTextView.setText(currentExercise.getDuration());
durationTextView.setTextColor(Color.BLACK);
handler.removeCallbacks(updateDurationTextView);
} else {
mp = MediaPlayer.create(MainActivity.this, currentExercise.getAudioFile());
mp.start();
v.setBackgroundResource(R.drawable.pause_icon);
handler.post(updateDurationTextView);
}
}
});
}
The Oefening playingExercise = currentExercise.get(playingCellPosition); works fine though, since it shows the information of the cell whose play button I tapped on. It just shows the information on the wrong cell.
As Áron Nemmondommegavezetéknevem pointed out, the problem is in parent.getChildAt(...);
Note 1: I don't reuse views with convertView since that messed up positions a lot. This is the closest I have come to what I have to achieve.
Note 2: I left out a lot of the code for the MediaPlayer. It is constructed well, so don't worry about that.
Note 3: If someone has a better suggestion for a title, please edit this one. Couldn't come up with a better one.
The problem is obviously with
parent.getChildAt(...);
In the case of a ListView it doesn't return the view of the specific position. Actually it's quite unpredictable what it returns with.
To illustrate why it doesn't return with the view of the specified position: imagine a list view with 10000 or more items. ListView has only a few views, and doesn't have views for all the 10000 items. What could it return with if you would request the 2786. view? Nothing, it doesn't have a view for that item.
Edit:
Suggestion:
Although I don't see how your code works, you should store which item view belongs to an individual item. For example, you can call setTag(position) on convertView before you return with it. Then you can write a function which finds the appropriate view for an item, if it exists. Something similar to this:
public View findViewAtPosition(int position) {
for (int i=0; i < listView.getChildCount(); i++) {
if (listView.getChildAt(i).getTag() == position) {
return(listView.getChildAt(i));
}
}
return(null);
}
I fixed it with an answer on another topic: https://stackoverflow.com/a/2679284/1352169
Áron Nemmondommegavezetéknevem's answer looks good, but this one is a little bit better since it e.g. keeps headers in mind too.

NullPointerException when clicking ListView item

I have a ListView with set onItemClickListener:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// not important
if (!found) {
activity.addSelectedIngredient(ingred);
parent.getChildAt(position).setBackgroundColor(Color.parseColor("#ff99FE80"));
} else {
activity.removeSelectedIngredient(ingred);
parent.getChildAt(position).setBackgroundColor(Color.WHITE);
}
}
The NullPointerException is thrown when parent hasn't got child on selected position (e.g. 15). Why? How it's possible that the element might not be present if she already selected it?
Edit:
if (!found) {
activity.addSelectedIngredient(ingred);
view.setBackgroundColor(Color.parseColor("#ff99FE80"));
} else {
activity.removeSelectedIngredient(ingred);
view.setBackgroundColor(Color.WHITE);
}
getChildAt returns listView's child. getChildAt position is not same position as in your adapter. You can have 1000 items in your adapter and only several childViews in listview because views are reusing.
I think you should change
parent.getChildAt(position).setBackgroundColor(Color.WHITE);
to
view.setBackgroundColor(Color.WHITE);
Try,
if (!found) {
activity.addSelectedIngredient(ingred);
view.setBackgroundColor(Color.parseColor("#ff99FE80"));
} else {
activity.removeSelectedIngredient(ingred);
view.setBackgroundColor(Color.WHITE);
}
Explanation:
so what is happening is that the parent has that view but the indexing is reset. So you clearly don't has any idea as which index is assigned to your element, It depends on the cash the listview has cashed elements for. So instead of getting child you should use the view. This is exactly the same as the item you just clicked.

Categories

Resources