Hello. Help to understand the reason for the failure of setOnItemClickListener. The code is the following: Layout file for Activation in which we will add Item

<?xml version="1.0" encoding="utf-8"?> 

 <FrameLayout android:id="@+id/frameLayout2" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right|center_vertical" android:text="Add" /> <TextView android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="left|center_vertical" android:text="Список Матеріалів" android:textAppearance="?android:attr/textAppearanceLarge" /> </FrameLayout> <ListView android:id="@+id/listView2" android:layout_width="wrap_content" android:layout_height="300dp" android:layout_alignParentBottom="true" android:layout_below="@+id/frameLayout2" /> 

Layout of the Item

 <?xml version="1.0" encoding="utf-8"?> 

 <TableLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="75dp" android:layout_height="75dp" android:id="@+id/imageView" android:layout_column="0" android:layout_weight="0.2" /> <TableLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.7" android:layout_gravity="center_vertical"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="MAt" android:id="@+id/textView9" android:textSize="12dp" android:gravity="center_vertical|left" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" android:id="@+id/textView12" android:textSize="12dp" android:gravity="center_vertical|right" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="" android:id="@+id/textView10" android:textSize="12dp" android:gravity="center_vertical|left" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" android:id="@+id/textView13" android:textSize="12dp" android:gravity="center_vertical|right" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Id" android:id="@+id/textView11" android:textSize="12dp" android:gravity="center_vertical|left" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Small Text" android:id="@+id/textView14" android:textSize="12dp" android:gravity="center_vertical|right" /> </TableRow> </TableLayout> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Load" android:id="@+id/button5" android:layout_gravity="center_vertical|right" android:layout_weight="0.1" /> </TableRow> </TableLayout> 

And the activity code itself

 public class materialListofDraft extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener { ListView listView; Button btnAddNewMaterial; ArrayList<Integer> idMaterials; long orderPosition; private static final int CM_DELETE_ID = 1; SimpleAdapter simpleAdapter; //MyTask task; ArrayList<String> materialimageFilenames = new ArrayList<String>(); ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>( LocalInfo.getOrders().size()); Map<String, Object> m; final String ATTRIBUTE_NAME_MATERIAL_NAME = "MATERIALNAME"; final String ATTRIBUTE_NAME_COUNT_DETAIL = "COUNT"; final String ATTRIBUTE_NAME_MATERIAL_ID = "ID"; final String ATTRIBUTE_NAME_IMAGE_NAME = "IMAGE"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_material_listof_draft); listView = (ListView) findViewById(R.id.listView2); btnAddNewMaterial = (Button) findViewById(R.id.button4); btnAddNewMaterial.setOnClickListener(this); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); for (int i = 0; i < LocalInfo.getMaterialsStructureOfOrders().size(); i++) { materialimageFilenames.add(LocalInfo.getMaterialsStructureOfOrders().get(i).getMaterail().getImageLarge()); m = new HashMap<String, Object>(); m.put(ATTRIBUTE_NAME_MATERIAL_NAME, LocalInfo.getMaterialsStructureOfOrders().get(i).getMaterail().getName()); m.put(ATTRIBUTE_NAME_COUNT_DETAIL, LocalInfo.getMaterialsStructureOfOrders().get(i).getIdDetails().size()); m.put(ATTRIBUTE_NAME_MATERIAL_ID, LocalInfo.getMaterialsStructureOfOrders().get(i).getMaterail().getId()); m.put(ATTRIBUTE_NAME_IMAGE_NAME, R.mipmap.page404); data.add(m); } String[] from = {ATTRIBUTE_NAME_MATERIAL_NAME, ATTRIBUTE_NAME_COUNT_DETAIL, ATTRIBUTE_NAME_MATERIAL_ID, ATTRIBUTE_NAME_IMAGE_NAME}; int[] to = {R.id.textView12, R.id.textView13, R.id.textView14, R.id.imageView}; simpleAdapter = new SimpleAdapter(this, data, R.layout.unitlist_material, from, to); listView.setAdapter(simpleAdapter); listView.setOnItemClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case (R.id.button4): { Intent intent = new Intent(this, materialtemplates.class); startActivityForResult(intent, 1); break; } } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data1) { // запишем в лог значения requestCode и resultCode Log.d("myLogs", "requestCode = " + requestCode + ", resultCode = " + resultCode); // если пришло ОК if (resultCode == RESULT_OK) { Toast.makeText(this, "Ok result", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "Wrong result", Toast.LENGTH_SHORT).show(); } } @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Log.v("asdfasdf", "asdfasdf"); Toast.makeText(this, "Tatta", Toast.LENGTH_SHORT).show(); } 

}

At the same time, I sin in the direction of the Layout files, since onItemClick () in other Activities worked fine, but not here :(

  • Since the OnClickListener is set exclusively on the button, it makes no sense to check the id of the pressed View. Take this check and MB earns - P. Ilyin
  • With OnClickListener everything is fine The point is in setOnItemClickListener for ListView, more precisely for Item'ov which I throw to it - dimchuk
  • Try to declare the anonymous class AdapterView.OnItemClickListener directly in the listView.setOnItemClickListener method rather than the class interface, will it work like that? - Werder
  • Anonymously - it didn't work :(. First of all I tried. I’ll clarify: button4 - it’s not a mess here, it has another task. And button in the Item itself - it’s not implemented - but that's another story :) - dimchuk

1 answer 1

The active button on the list item is in the way, set attributes to it like this:

 <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:focusable="false" android:focusableInTouchMode="false" android:text="Load" android:id="@+id/button5" android:layout_gravity="center_vertical|right" android:layout_weight="0.1" /> 
  • I'm sorry. but if you use this option with the option that instead of the button THREE TextView (EditText,Numeric) + registerForContextMenu (listView) for menus for each Item , it does not work. android:textIsSelectable="true" also not possible. - Or is there another principle? - dimchuk pm
  • I do not understand you, can create a new question? - Eugene Krivenja