-3

So, my onListItemClick is not working for some reason. I use the same code structure in another activity and that one is working perfectly. And when I try to implement the same method here, it just doesn't work at all. The Toast doesn't work. And the intent to detailactivity also doesn't work. I'm sure my naming and labeling is fine. The list items doesn't even feel like they are clickable. Can someone help me out here, please?

public class MainActivity extends ListActivity {

    String objectId;
    protected List<ParseObject> mInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_info);

    //**** There is a bunch of code here that takes import the list view from an adapter. I believe they are working fine. ***//
    }

    //**** this part below is not working. This is the section where if you click on the list item, it brings you to the detailactivity.
    @Override
    protected void onListItemClick(ListView l,View v, int position, long id){
        super.onListItemClick(l, v, position, id);

        ParseObject infoObject = mInfo.get(position);
        String objectId = infoObject.getObjectId();

        Toast.makeText(getApplicationContext(),objectId, Toast.LENGTH_SHORT).show();

        Intent Details = new Intent(MainActivity.this, DetailsActivity.class);
        Details.putExtra("objectId", objectId);
        startActivity(Details);
    }
galath
  • 5,717
  • 10
  • 29
  • 41
stanley santoso
  • 343
  • 1
  • 6
  • 19

3 Answers3

1

Please implement the onListItemClickListener

public class MainActivity extends ListActivity implements OnItemClickListener{

//code code code

}

Then when you set the onItemCLick use

setOnItemClickListener(this);
Sheychan
  • 2,415
  • 14
  • 32
1

İf you use a custom adapter to fill listview and involve clickable components such as Button, İmageView. onListItemClick will don't work because it won't be able to decide which component will be listened. solution is "android:focusable:"false"" keyword. thanks to this, Listener will only focus items which are populated custom adapter by you.

Like this:

<Button
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="4dp"
android:background="colorCode"
/>

I have used this button to seperate each list view items. But it caused your problem. I think if you will list items, You shouldnt use clicable components in template xml.

Happy Coding.. Vd.

0

I found an answer to solve this by adding android:focusable="false" inside of the the views in the listview/customlayout that will be imported to the activity. I got the answer from this link: onListItemClick is not working for listview?

Community
  • 1
  • 1
stanley santoso
  • 343
  • 1
  • 6
  • 19