0

when I click on the button for the first click click function doesn't work but for the second time click on-click function works. I don't know what is wrong with my code. Please suggest me right way. Thanks in advance

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, final int position, long id) { ImageView deletelist = (ImageView) view.findViewById(R.id.dltconsumer); final CustomFontTextView number = (CustomFontTextView) view.findViewById(R.id.consumer_number); deletelist.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { JSONArray delete_jsonarray; JSONObject jsonObject; String phonenumber = number.getText().toString(); String jsonsetpreference_data = SessionManager.getPreferences(context,"splitfare_consumer"); Log.d("jsonsetpreference_data:::::::","" +jsonsetpreference_data); try { delete_jsonarray= new JSONArray(jsonsetpreference_data); for (int i =0; i<= delete_jsonarray.length()-1 ; i++){ jsonObject = delete_jsonarray.getJSONObject(i); String array_phonenumber = jsonObject.getString("number"); if (phonenumber.equals(array_phonenumber)){ delete_jsonarray.remove(i); } } SessionManager.setPreferences(context,"splitfare_consumer",delete_jsonarray.toString()); String data = SessionManager.getPreferences(context,"splitfare_consumer"); Log.d(TAG,""+data); ArrayList<String> details = new ArrayList<String>(); try { js = new JSONArray(data); } catch (JSONException e) { e.printStackTrace(); } adapter = new AddPassengerAdapater(context,js,details); listview.setAdapter(adapter); adapter.notifyDataSetChanged(); } catch (JSONException e) { e.printStackTrace(); } } }); } }); 
1
  • Did you got it working now? Commented Oct 20, 2017 at 10:16

2 Answers 2

1

Simply reading the code above : When onItemClick is called, you setOnClickListener on some image view. This is one click. The only thing done here is setting the listener.

Then, on further clicks, the ImageView's onClick will be called.

You should consider either using directly the onItemClick of the ListView, or adding the following attributes in your ImageView's xml :

android:clickable="true" android:onClick="yourMethodNameHere" 
Sign up to request clarification or add additional context in comments.

1 Comment

I don't know which of the two solutions you tried, but if you tired the xml one, be sure to replace "yourMethodNameHere" with an existing method in your activity.
0

why you use listview? that is because the button is inside listview I guess..

You can use Recyclerview with onItemClick, please refer to this tutorial https://antonioleiva.com/recyclerview-listener/

hope this help you

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.