listview getChildAt() doesn't work for imageview? - android

I want to use imageview like a radio button in custom listview so i used a for loop and getChildAt(position) but it doesn't work well when i scroll down it doesn't change imageview image src.
This is my code :
lvChooseReader.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, long id) {
for (int i = 0; lvChooseReader.getLastVisiblePosition() - lvChooseReader.getFirstVisiblePosition() > i; i++) {
View childView = lvChooseReader.getChildAt(i);
ImageView myImage= (ImageView) childView.findViewById(R.id.ivTest);
if (i == position) {
rr.setImageDrawable(getResources().getDrawable(R.drawable.check_mark_default));
}else {
rr.setImageDrawable(getResources().getDrawable(R.drawable.check_mark_selected));
}
}
lvChooseReader.invalidateViews();
}
});
or could any one give me a link to make radio button on item click not the radio button onclick...

lvChooseReader.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, long id) {
for (int i = lvChooseReader.getFirstVisiblePosition(); lvChooseReader.getLastVisiblePosition() > i; i++) {
View childView = lvChooseReader.getChildAt(i);
ImageView myImage= (ImageView) childView.findViewById(R.id.ivTest);
if (i == position) {
rr.setImageDrawable(getResources().getDrawable(R.drawable.check_mark_default));
}else {
rr.setImageDrawable(getResources().getDrawable(R.drawable.check_mark_selected));
}
}
lvChooseReader.invalidateViews();
The deference is:
In this code the value of i will be between first visible item to last visible item and in your case value of i was between on and total visible items.
For example:
If item 2-3-4-5 are visible i should be 1-2-3-4(position of these items in listview).
and in your case it was 0-1-2-3

i solved my problem by using static values :
i add this static variable in the dialogfragment which contain my listview
static int selectedReaderId;
and for onItemClick in listview i add this :
ChooseReader reader = chooseReaderArrayList.get(position);
selectedReaderId=reader.getReaderID();
lvChooseReader.invalidateViews();
then in the custom adapter inside my getview i add this
final ChooseReader chooseReader = readers.get(position);
if (chooseReader.getReaderID()==ChooseReaderDialog.selectedReaderId){
holder.ivTest.setImageDrawable(context.getResources().getDrawable(R.drawable.check_mark_default));
}else { holder.ivTest.setImageDrawable(context.getResources().getDrawable(R.drawable.check_mark_selected));
}

Related

Expand all list items in custom ListView (android)

I am using a normal ListView (mItemsList) with expandable animation (from this tutorial). It works, when I click on list item it expands and shows details for this item.
mItemsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
View details = view.findViewById(R.id.details);
// Creating the expand animation for the item
ExpandAnimation expandAni = new ExpandAnimation(details, 500);
// Start the animation on the toolbar
details.startAnimation(expandAni);
}
});
I created a showDetails button and I want to expand all list items after clicking the button, but I am completely lost. Code below doesn't work
mShowDetailsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < mItemsList.getAdapter().getCount(); i++) {
View details= v.findViewById(R.id.details);
details.startAnimation(new ExpandAnimation(details, 500));
}
}
});
Could you help me?
Here's my list_item xml file
Maybe do the similar approach like the following to automatically click item one by one to open all listciew.
mShowDetailsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < mItemsList.getAdapter().getCount(); i++) {
listView.performItemClick(
getViewByPosition(i),
i,
listView.getAdapter().getItemId(i));
}
}
});
//the listview getChildAt only return the view(item) that is visible,
//therefore add a function to get invisible view together
public View getViewByPosition(int position) {
int firstItemPosition = listView.getFirstVisiblePosition();
int lastItemPosition = firstItemPosition + listView.getChildCount() - 1;
if (position < firstItemPosition || position > lastItemPosition ) {//is invisible
return listView.getAdapter().getView(position, null, listView);
} else {
int childIndex = position - firstItemPosition;//is visible
return listView.getChildAt(childIndex);
}
}
The one you do is not work as the view is refering the mShowDetailsButton ,but not mItemsList. Thus, you cannot findViewof id R.id.details.
mShowDetailsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {//<-- this view not referring to mItemsList
}
});

Android ListView Items select and reset

I've a problem with my simple application. It uses a listview that has to:
- open a new Activity when pressed and it's on view mode
- highlight the selected item when it's on edit mode
I'm doing the following:
ListView lv = (ListView)findViewById(R.id.categoryListView);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String entry = (String) parent.getItemAtPosition(position);
if (_editMode)
view.setBackgroundColor(Color.parseColor("#5B5B5B"));
else
{
Intent intent = new Intent(MainActivity.this, CategoryActivity.class);
intent.putExtra("CATEGORY", entry);
startActivity(intent);
}
}
});
Then I want that when I turn to view mode, all items must be deselected, and I do this:
for (int i = 0; i < lv.getAdapter().getCount(); i++)
{
lv.getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"));
}
But this is working fine only if all the items in listview are visible.
I've tried implementing this on the adapter without success...
Any suggestion?
Thanks!
EDIT
Ok after Jawad answer I just figured out how does "getView" method work. So this is the solution I've used:
I declared an arraylist containing selected items:
ArrayList<String> itemSelected = new ArrayList<String>();
This is the ListView onItemClick listener (where you can select and deselect items):
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String entry = (String) parent.getItemAtPosition(position);
if (itemSelected.contains(entry))
{
itemSelected.remove(entry);
}
else
{
itemSelected.add(entry);
}
((ArrayAdapter<String>)lv.getAdapter()).notifyDataSetChanged();
}
});
This is the ovverride of getView method:
itmList.setAdapter(new ArrayAdapter<String>(this,R.layout.list_black_text,R.id.list_content, itmStrList){
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView != null)
{
if (itemSelected.contains(itmList.getAdapter().getItem(position))) {
convertView.setBackgroundColor(Color.RED);
} else {
convertView.setBackgroundColor(Color.parseColor("#5B5B5B"));
}
}
return super.getView(position, convertView, parent);
}
});
And this is how to deselect all items:
itemSelected.clear();
((ArrayAdapter<String>)lv.getAdapter()).notifyDataSetChanged();
Thank you man :)
I think your problem is your are not paying attention to view recycling. When you are changing the color of a view's background, the view can be recycled and you will have something not desired. Check this link for details.
You should have a, lets say boolean variable, in your underlyning data called something like isSelected. And add this code in your getView() method.
if(item.isSelected()){
setBackgroundColor(Color.parseColor("#5B5B5B"));
}
else{
setBackgroundColor(Color.parseColor("#FFFFFF"));
}
Then in your onItemClick add replace view.setback... by
lv.getAdapter().getItem(position).setSelected(true);
lv.getAdapter().notifyDataSetChanged();
You may need a cast.
Finally change this
for (int i = 0; i < lv.getAdapter().getCount(); i++)
{
lv.getChildAt(i).setBackgroundColor(Color.parseColor("#FFFFFF"));
}
to this:
for (int i = 0; i < lv.getAdapter().getCount(); i++)
{
lv.getAdapter().getItem(i).setSelected(false);
}
lv.getAdapter().notifyDataSetChanged();
The reason why your portion of code works only if all the items are visible is view recycling. Moreover lv.getChildAt() gives you only the views that are visible. Your code may then crash because adapter.getcount maybe bigger then the number of listview childs.
Hope it helps.

Get all views in listview in setOnItemClickListener

I am using below code to setcolor of a selected item of a listview. The rule is only one should be colored. But with below code if I select 2 views both get colored. Can you please help me get all other views in the listview so that when I click on certain view all other views i set to different color and the selected view i set a different color(Green in this case).
Please let me know if any other solution?
lv = (ListView) view.findViewById(R.id.listf);
lv.setAdapter(text![enter image description here][1]Adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
TextView v = (TextView) view.findViewById(R.id.template_text);
view.setBackgroundColor(Color.GREEN);
}
});
I resolved the problem using the below:
I put a loop where only the selected list item is set in RED whereas all others were set in Green, in this way only only one list item will be colored on selected.
lv = (ListView) view.findViewById(R.id.listf);
lv.setAdapter(Adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
for (int i = 0; i < 3; i++)
{
if (position == i)
{
parent.getChildAt(i).setBackgroundColor(Color.RED);
}
else
{
parent.getChildAt(i).setBackgroundColor(getResources().getColor(R.color.Dark_Green);
}
}
TextView v = (TextView) view.findViewById(R.id.template_text);
view.setBackgroundColor(Color.GREEN);
}
});
As you told you are not able to change adapter code, you can prefer solution 2.
Solution 1: Create one variable int selectedPosition and method setSelected in your adapter class
int selectedPosition = -1;
public void setSelected(int position)
{
selectedPosition = position;
notifyDatasetChanged();
}
Edit getView() of the adapter class and include following code
if(selectedPosition==position)
{
templateTextView.setBackgroundColor(Color.GREEN);
}
else templateTextView.setBackgroundColor(Color.BLUE);// default textView color
Solution 2: keep reference of previously selected textView as well each time change the color of currently selected textview to green andd previous one to blue
TextView previousSelected = null;
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(previousSelected!=null)
previousSelected.setBackgroundColor(Color.BLUE);
TextView v = (TextView) view.findViewById(R.id.template_text);
view.setBackgroundColor(Color.GREEN);
previousSelected = v;
}
});

Android get checked ListView items

I have a Listview with a CheckBox and some Textviews and i'm trying to get all the checked items. I'm using a SimpleAdapter and could not find any helpful tutorial for it. Is there any way to do it with a SimpleAdapter?
You can display checked items of listview like this :
When a listview item is checked,it appends the item to the Textview and removes when the item is unchecked.
ArrayList<String> selectedNames = new ArrayList<String>();
//Initialise selctnts in onCreate().
selctnts=(TextView)findViewById(R.id.selectedtagcontact);
public class CheckBoxClick implements AdapterView.OnItemClickListener {
int listlength;
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {
CheckedTextView ctv = (CheckedTextView) view;
String item = adapter.getItem(position);
if (ctv.isChecked()) {
ctv.setChecked(true);
selectedNames.add(ctv.getText().toString());
selctnts.append(item+", ");
} else {
ctv.setChecked(false);
selectedNames.remove(ctv.getText().toString());
listlength=selectedNames.size();
selctnts.setText("");
for(int l=0;l<listlength;l++)
selctnts.append(selectedNames.get(l)+", ");
}
}
}

ListView item setTextColor modified other elements

I have a ListView backed by SimpleCursorAdapter and custom ViewBinder. I want to make items in this listview change their color on clicking. If I do that in the OnClickListener - it works paritally, changing the color of the item clicked, and of the items down the list, each 7th (I guess, the period depends on on the viewable area of the listview).
Can anyone suggest how to deal with this? Or, maybe point to a more elegant way of making items in the listView selectable?
Thank you.
UPD: (sorry for bad formatting - this is the first time I post a question):
Below is how I try to make an item in the ListView "selected":
private void setupListView(final ListView lv) {
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> adapterView, View view, int position, final long id) {
RelativeLayout layout = (RelativeLayout) view;
int color;
if (conditionMet) {
color = R.color.gray;
} else {
color = R.color.red;
}
for(int i = 0; i < layout.getChildCount(); i++) {
((TextView)layout.getChildAt(i)).setTextColor(getResources().getColor(color));
}
return;
}}
This is how I init the adapter:
final SimpleCursorAdapter adapter =
new SimpleCursorAdapter(
this,
itemId,
cursor,
from,
to
);
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
final TextView textView = (TextView) view;
// do necessary conversions
return true;
}
});
listView.setAdapter(adapter);
You can use the property android:listSelector to set the theme or any drawable or color of the currently selected item in a list.
Since no other answer, and, I think, I had some troubles with the suggestion below, I post how I did it:
I store ids of the items clicked in a special map
in the listview onclick I check whether the id of the just clicked item is in the map: if yes, I remove it and make the item and its children color A, otherwise I add the id to the map and set the color to B
public void onItemClick(AdapterView<?> adapterView, View view, int position, final long id) {
Context ctx = MainActivity.this;
RelativeLayout layout = (RelativeLayout) view;
try {
int color;
if (items.containsKey(id)) {
items.remove(id);
color = R.color.gray;
tempIds.remove(id);
} else {
items.put(id, sum);
color = R.color.red;
tempIds.add(id);
}
for (int i = 0; i < layout.getChildCount(); i++) {
final TextView textView = (TextView) layout.getChildAt(i);
textView.setTextColor(getResources().getColor(color));
}
} catch (ParseException e) {
Log.e(MainActivity.class.toString(), "Exception parsing", e);
}
return;
}
}

Categories

Resources