Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy background images of buttons are changing for last list item's button only,when I click on any list item's button?
    primarykey
    data
    text
    <p>I have created list view with adapter class, in which in list item, i have given three buttons. I need to change background image of that button and other button also. I have 5 list items and every items has its own buttons.</p> <p>Now, as I click on first, second up to fifth list item's buttons, background of last image is changing only. Every list item's button's background image should change if I click on respective button depend on logic. It is happening for only last list item..</p> <p>Please help me if anyone knows how to do this..</p> <p>Code is : </p> <pre><code>public class MainActivity extends ListActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); init(); } private void init() { LayoutInflater inflater1 = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater1.inflate(R.layout.datelabel, null); setListAdapter(new EventListAdapter(this)); ListView listView = getListView(); listView.setOnItemLongClickListener(new OnItemLongClickListener() { public boolean onItemLongClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { Toast.makeText(MainActivity.this, "tapped on position " + position, Toast.LENGTH_SHORT).show(); return false; } }); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { ((EventListAdapter)getListAdapter()).toggle(position); } private class EventListAdapter extends BaseAdapter { private static final int VISIBLE = 0; private static final int GONE = 8; private TextView textViewForDateHeader; private TextView textViewTitle; private TextView textViewDialogue; private TextView textViewHeader; private ImageButton buttonForCheckMark; private ImageButton buttonForDelete; private View buttonForRemainder; public EventListAdapter(Context context) { mContext = context; } public int getCount() { return mTitles.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View myView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); myView = inflater.inflate(R.layout.activity_main, null); textViewTitle = (TextView) myView.findViewById(R.id.textViewTitle); textViewTitle.setText(mTitles[position]); textViewDialogue = (TextView) myView.findViewById(R.id.textViewDialog); textViewDialogue.setText(mDialogue[position]); textViewDialogue.setVisibility(mExpanded[position] ? VISIBLE : GONE); buttonForCheckMark = (ImageButton) myView.findViewById(R.id.buttonForCheckMark); buttonForCheckMark.setVisibility(mExpanded[position] ? VISIBLE : GONE); buttonForDelete = (ImageButton) myView.findViewById(R.id.buttonForDelete); buttonForDelete.setVisibility(mExpanded[position] ? VISIBLE : GONE); buttonForRemainder = (ImageButton) myView.findViewById(R.id.buttonForRemainder); buttonForRemainder.setVisibility(mExpanded[position] ? VISIBLE : GONE); buttonForRemainder.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "tapped on remainder", Toast.LENGTH_SHORT).show(); } }); buttonForCheckMark.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "tapped on checkMark", Toast.LENGTH_SHORT).show(); buttonForCheckMark.setBackgroundResource(R.drawable.ic_launcher); buttonForDelete.setBackgroundResource(R.drawable.ic_navigation_cancel); buttonForCheckMark.setClickable(false); buttonForDelete.setClickable(true); } }); buttonForDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "tapped on delete", Toast.LENGTH_SHORT).show(); buttonForCheckMark.setBackgroundResource(R.drawable.ic_navigation_accept); buttonForCheckMark.setClickable(true); buttonForDelete.setBackgroundResource(R.drawable.ic_drawer); buttonForDelete.setClickable(false); } }); return myView; } public void toggle(int position) { mExpanded[position] = !mExpanded[position]; notifyDataSetChanged(); } /** * Remember our context so we can use it when constructing views. */ private Context mContext; /** * Our data, part 1. */ // private ImageButton[] mButtons = { // // "R.drawable.remainder", // // }; private String[] mHeader = { "12 Jan, 2013", "13 Feb, 2013", "31 Mar, 2013", "15 Aug, 2013", "7 Sep, 2013" }; /** * Our data, part 1. */ private String[] mTitles = { "Event 1", "Event 2", "Event 3", "Event 4", "Event 5" }; /** * Our data, part 2. */ private String[] mDialogue = { "wuszuogwfuieffufuhuysugdueljwihadghgxdhgyhghsdgyigwuweyuqaGDHGYHGHGAdhgyhigxgxgeuyehu.", "dgusduugyujguegytgujgdugwjhiuyg7wtqUYGYYgyijyiufufjguhgdugfhgfhgfgfhgfhghfghifgyi,dgwsdtgyfytfiuwt,", "rtygygghtudggyjhgujtugdhhguyuaUUUUDJYUIDHUJHDIIDUJDHDUJHDIDIOUYhujtdugyhdgg", "gjhuwjsgudggdudgjqhasdgdhgjdhushjaguhguwegagsdgygydgfgdcgycg", "fhdgyhdfhfgdyhhwsddgyuduuufguugwugdfgugdgooduiuduiuduuduiuiuidudiiwdiou" }; /** * Our data, part 3. */ private boolean[] mExpanded = { false, false, false, false, false, false, false, false }; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload