user3597639
user3597639

Reputation: 57

setOnItemClick isnt getting recognized(Android)

I am displaying a Listview of recorded data and when the user clicks on a specific list item, it triggers an intent and brings the user to the profile for that selected user. I worked when i first ran my project but ever since it just will not work. Thank you in advance.

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() {
            @Override
            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 objIndent = new Intent(getApplicationContext(), CowProfile.class);
                Toast.makeText(CowListView.this, "Clicked", Toast.LENGTH_SHORT).show();
                objIndent.putExtra("student_Id", Integer.parseInt(studentId));
                startActivity(objIndent);
            }
        });

    }


    private void updateCowUI() {

        Cursor cursor = cowAdapter.getAllCowData();
        startManagingCursor(cursor);
        String[] from = new String[]{RegisterCowAdapter.COWTAGNO, RegisterCowAdapter.COWDOB};
        int[] to = new int[]{R.id.cowtagnolist, R.id.cowdoblist};
        SimpleCursorAdapter reminders = new SimpleCursorAdapter(
                this,
                R.layout.cow_list_item,
                cursor,
                from,
                to
        );
        this.setListAdapter(reminders);
    }
}


    private void updateCowUI() {

         Cursor cursor =cowAdapter.getAllCowData();
        startManagingCursor(cursor);
        String[] from = new String[]{RegisterCowAdapter.COWTAGNO, RegisterCowAdapter.COWDOB};
        int[] to = new int[]{R.id.cowtagnolist, R.id.cowdoblist};
        SimpleCursorAdapter reminders  = new SimpleCursorAdapter(
                this,
                R.layout.cow_list_item,
                cursor,
                from,
                to
        );
        this.setListAdapter(reminders);
    }
   }

My XML

 <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:longClickable="true">
    <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:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"


        android:descendantFocusability="blocksDescendants"
        >
    </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:focusableInTouchMode="false"
        android:clickable="false"
        android:focusable="false"
        android:descendantFocusability="blocksDescendants"
        android:textColor="@color/black">

    </TextView>
    </LinearLayout>

Upvotes: 0

Views: 99

Answers (1)

Manny265
Manny265

Reputation: 1709

Might be one of two things. You have implemented OnClickListener but you have not set the ListView to listen to that specific class nor have you called any method in your onCreate that will set the ListView's listener to that class.

Secondly I think you might need to implement OnItemClickListener and not onCLickListener since thats what is on the ListView, an Item.

Im confused though why do you have onClick and then you set the listener inside this method? How will it know in the first place that the view has been clicked if it is not listening from the beginning i.e. in the onCreate or a method called in the onCreate ? Your logic seems abit off

Upvotes: 1

Related Questions