user3597639
user3597639

Reputation: 57

Listview onItemClick doesn't work as expected

My log when the intent actually worksthis is a weird question that I'm extremely curious about. I have a listview of profiles and when i click on a particular one i want to view that particular profile by itself. When i try to select an item in the listview it generally doesn't work but it works the sometimes(about 1 in 10) for some odd reason. Which makes it more frustrating. Has anyone ever had this problem? Thank you. ( I posted a question similar to this thinking it was not working at all so please don't duplicate, thank you).

public class CowListView extends ListActivity {

    RegisterCowAdapter cowAdapter;
    private DataBaseHelper databaseHelper;
    public ListView listView;
    TextView student_Id;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cow_list_item);
        cowAdapter = new RegisterCowAdapter(this);

        cowAdapter.open();
        updateCowUI();
        listView = (ListView) findViewById(android.R.id.list);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                student_Id = (TextView) view.findViewById(R.id.cowtagnolist);
                String studentId = student_Id.getText().toString();
                Intent i = new Intent(view.getContext(), CowProfile.class);
                Toast.makeText(CowListView.this, "Clicked", Toast.LENGTH_SHORT).show();
                i.putExtra("student_Id", Integer.parseInt(studentId));
                Log.d("StartingCow", studentId);
                startActivityForResult(i, 0);

            }

        });

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants"
    android:longClickable="true"
    android:focusableInTouchMode="false"
    android:clickable="false"
    android:focusable="false">

    <ListView
        android:id="@android:id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </ListView>

    <TextView
        android:id="@+id/cowtagnolist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/black"
        android:clickable="true">
    </TextView>

    <TextView
        android:id="@+id/cowdoblist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"    
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="17dp"
        android:clickable="true"
        android:textColor="@color/black">    
    </TextView>
</LinearLayout>

Upvotes: 0

Views: 190

Answers (3)

user3597639
user3597639

Reputation: 57

I figured out what was wrong, I didn't define my child objects and my root item correctly. Hope this helps anyone out there with a similar issue. The child items should have been implemented as seen below:

android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false"

In the root item:

android:clickable="false"
android:descendantFocusability="blocksDescendants"
android:focusable="false"
android:focusableInTouchMode="false"

Upvotes: 1

Pete Trikhin
Pete Trikhin

Reputation: 51

maybe the problem is in xml??

I see the next strings

android:longClickable="true" android:clickable="false"

looks like 1 of 10 successfull clicks happen when you make a long click instead a usual. To solve the problem change clickable to true

android:clickable="true"

If i understood the question right - it should help

Upvotes: 0

D Agrawal
D Agrawal

Reputation: 471

You should keep Listview width as Match_parent , if u wrap_content it, it will not get click event beyond its child size or width. For what i think this might be the problem. If ur Listview single row layout too contains childviews with width wrap_content, then the problem might increase.

Upvotes: 0

Related Questions