Android onClickListener strange behavior - java

i'm trying to set a child click listener on my ExpandedListView, and i'm getting some strange results:
theList.setOnChildClickListener(new OnChildClickListener() {
#Override
public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id) {
Log.i(LOG_TAG, "clicked");
ExpandableListAdapter adapter = parent.getExpandableListAdapter();
View v = (View)adapter.getChild(groupPosition, childPosition);
Toast toast = Toast.makeText(getApplicationContext(),
"item: " + new Integer(groupPosition).toString() + ":" + new Integer(childPosition).toString(),
Toast.LENGTH_SHORT);
toast.show();
if (!((CheckedTextView)view.findViewById(R.id.check)).isChecked()){
((CheckedTextView)view.findViewById(R.id.check)).setChecked(true);
} else {
((CheckedTextView)view.findViewById(R.id.check)).setChecked(false);
}
return true;
}
});
it sort of works. when i check one of the items, however, another, additional item will also be selected, seemingly at random. am i not doin this correctly?

Solved the issue myself. i ended up extending the adapter class and overriding getChildView and getGroupView and added a list of booleans to the adapter to keep up with the checkmarks. probably not the most sophisticated solution, but works perfectly.

Related

Android Multiple Spinner

I'm sure I'm missing something simple, but I am having some logic issues with my code. I am working on a converter app. I will be using two spinners to select between to do conversions. Ex. inches to feet. I am using two simple methods to test before fleshing out all of the code. Right now if I select the value for SpinnerA in the app first, and then select the value for SpinnerB, it doesn't calculate. If I select SpinnerB first and then SpinnerA, it works. What am I missing?
spinnerA = (Spinner)getView().findViewById(R.id.spinnerA);
spinnerB = (Spinner)getView().findViewById(R.id.spinnerB);
adapterA = ArrayAdapter.createFromResource(getContext(),
R.array.conversions, android.R.layout.simple_spinner_item);
adapterA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterB = ArrayAdapter.createFromResource(getContext(),
R.array.conversions, android.R.layout.simple_spinner_item);
adapterB.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerA.setAdapter(adapterA);
spinnerB.setAdapter(adapterB);
spinnerA.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tempA = parent.getItemAtPosition(position).toString();
if (tempA.equals("Inches") && tempB.equals("Centimeters")){
textView.setText(String.valueOf(halfMyNum(100)));
}
else if (tempA.equals("Centimeters")){
if (tempB.equals("Inches")){
textView.setText(String.valueOf(doubleMyNum(12)));
}
}
else{
textView.setText("Please select a valid option");
}
//Toast.makeText(getContext(), parent.getItemAtPosition(position)+ " Selected"
//, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinnerB.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
tempB = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
When you are selecting an item from spinnerA first, tempA is initialized and item listener for spinnerA is called but tempB is not yet initialized.
Then, when you select item from spinnerB, tempB is initialized and item listener for spinnerB is called. In your case, you only called the method in item listener method for spinnerA, so when you select item from spinnerB nothing actually executes. One possible solution is to call the desired method in item listener for spinnerB as well.

deleted Item place in ArrayAdapter in my ListView left empty

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

setOnLongClickListener not working with PinchImageView

I downloaded the android-pinch jar so that I can have zoom functionality. The problem I am having is that I have to do setOnLongClickListener because I want to create a DragShadow for the user to see when they are dragging an icon or image and I want to assign a setOnClickListener to the image. However the PinchImageView for some reason doesn't work with my single and long click listeners, nothing happens.
Here is the code( it's a custom adapter ):
#Override
public View getView(int position, View view, ViewGroup viewGroup)
{
try
{
if(view == null)
view = myInflater.inflate(R.layout.tools_layout, viewGroup, false);
PinchImageView img = (PinchImageView)view.findViewById(R.id.imageView);
img.setBackgroundResource(drawId[position]);
img.setTag(icons.get(position));
img.setLongClickable(true);
img.setOnLongClickListener(longListen);
}
catch(Exception ex)
{
Log.i("customException", "getView():" + ex.getMessage());
}
return view;
}
View.OnLongClickListener longListen = new View.OnLongClickListener()
{
#Override
public boolean onLongClick(View v)
{
try
{
isListItem = true; // this never gets run...
Log.i(TAG, "long click");
ClipData data = ClipData.newPlainText("", "");
DragShadow dragShadow = new DragShadow(v);
v.startDrag(data, dragShadow, v, 0);
}
catch(Exception ex)
{
Log.i("customException", "longListen: " + ex.getMessage());
}
return false;
}
};
I've even tried changing the return to true because I saw in the TouchImageView that changing the return made it work, probably not the same for PinchImageView but was worth a try. Any suggestions?
You can try entering the PinchImageView source code and change the top to:
implements OnTouchListener, View.OnLongClickListener
Then implement a callback to your code or do whatever you want.

How to disable ListView item after it has been clicked?

I have a simple array of Strings that I was displaying in a horizontal ListView with an ArrayAdapter. What I'm looking to do is: when the user selects an item from the ListView, make that item not clickable and change the background color of that item. Perhaps like a "grayed-out" look to it. I was looking into creating a custom Adapter and overriding the isEnabled(int position) method but I don't know how I would go about this. Any advice, suggestions, or help will be greatly appreciated thanks!
I was looking into creating a custom Adapter and overriding the isEnabled(int position) method but I don't know how I would go about this.
This is quite easy to do. I recommend a SparseBooleanArray to track the enabled items for efficiency:
public class MyAdapter extends ArrayAdapter<String> {
private SparseBooleanArray enabledItems = new SparseBooleanArray();
public MyAdapter(Context context, int textViewResourceId, List<String> objects) {
super(context, textViewResourceId, objects);
}
#Override
public boolean areAllItemsEnabled() {
return false;
}
#Override
public boolean isEnabled(int position) {
return enabledItems.get(position, true);
}
public void toggleItem(int position) {
boolean state = enabledItems.get(position, true);
enabledItems.put(position, !state);
}
}
The AutoComplete feature of Eclipse did must of the work, but here are some quick notes:
You must override areAllItemsEnabled() along with isEnabled()
I designed toggle() to be used by an onItemClickListener() you only need to call adapter.toggle(position)
If you want to change the row's appearance (more than what enabling and disabling does by default) simply override getView(). Don't forget to cover both cases:
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
if(!isEnabled(position)) {
/* change to disabled appearance */
}
else {
/* restore default appearance */
}
return convertView;
}
Hope that helps!
pass position to adapter class when you click on list item
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
adapter.setSelectedIndex(position);
}
add method of setSelectedIndex to adapter class
public void setSelectedIndex(int ind)
{
selectedIndex = ind;
notifyDataSetChanged();
}
Now check the postion of this listview if same then enable and disable value in getView me method
if(selectedIndex!= -1 && position == selectedIndex)
{
holder.tv.setBackgroundColor(Color.BLACK);
}
else
{
holder.tv.setBackgroundColor(selectedColor);
}
holder.tv.setText("" + (position + 1) + " " + testList.get(position).getTestText());
Reference from here
Use setEnabled(bool) property:
yourlistview.setEnabled(false);
Not sure whether it will work or not
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// your code
view.setBackgroundColor(Color.BLUE);
view.setEnabled(false);
}

How to output text result in Android?

In an android website, I found an article about a widget similar to a drop-down list for selecting items. (Following is the link to the site; and it shows all the codes).
http://developer.android.com/resources/tutorials/views/hello-spinner.html
It uses the following code to display a message once you have selected a planet.
Toast.makeText(parent.getContext(), "Planet is Selected", Toast.LENGTH_LONG).show();
But this message "Planet is Selected" is only going to display for about 3 seconds and then it disappears. Can you please tell me how can I make the message stay on the screen for a longer time. Or how can I output the "Planet is Selected" message as a text layout in to the screen(So that it will stay on the screen permanently till I select another item from the list)? How can I use addView(tv) instead of setContentView(tv) Any help would be greatly appreciated.
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if (parent.getItemAtPosition(pos).toString().equals("Mars"))
{ TextView tv = new TextView(HelloSpinner.this);
tv.setText(parent.getItemAtPosition(pos).toString() + "Planet is Selected");
setContentView(tv); //How can I use addView(tv); here?
//Toast.makeText(parent.getContext(), "Planet Selected", Toast.LENGTH_LONG).show();
}
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
} }
If you want it to stay permanently on the screen, why not use a TextView and set your value to that instead of a Toast.
If you have any problems with not being able to use TextView, ie undefined. You should take a look at the textview documentation, as it is very well described there.
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
TextView tv = new TextView(this);
tv.setText(parent.getItemAtPosition(pos).toString());
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
third parameter Toast.LENGTH_LONG is time. so you can set any integer value ( not sure second or millisecond) ,. then on specific event call toast.hide() ;
toast is good choice for show message for some times only . so use textView if possible
create TextView with activity context :
TextView tv = new TextView(ActrivityName.this)
else if not an activity
TextView tv = new TextView(parent.getContext())

Categories

Resources