3

I have a Custom List View. Each row of the list view contains a checkbox, 2 text views and an image view. I have been tring to open another activity on click of any row of the list view but the onItemClick is not working at all.

public class SavedAddress2 extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private Toolbar toolbar;
    private ListView listView1;
    List<list_addr> rowitems;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.saved_address2);

        toolbar = (Toolbar) findViewById(R.id.tool_bar3);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowHomeEnabled(true);

        rowitems = new ArrayList<list_addr>();

        listView1 = (ListView) findViewById(R.id.addr_list);
        list_addr_adapter adapter = new list_addr_adapter(this,
                R.layout.items_sav_addr2, SavedAddress.rowitems);
        listView1.setAdapter(adapter);
        listView1.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(Main.this,Second.class);
        startActivity(intent);
    }

    //...
}

Is it because I have a checkbox in the list? Because I have applied the same method for other list view and they work perfectly.

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
TeeKay
  • 1,025
  • 2
  • 22
  • 60

3 Answers3

0

Try seting your checkbox not focusable, like this:

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

Hope it helps.

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
  • That worked. But in this if we click the checkbox,nothing happens. Could you tell me a way to that as well? – TeeKay Jan 03 '16 at 16:07
  • [this](http://stackoverflow.com/questions/1709166/android-listview-elements-with-multiple-clickable-buttons) will help you if you want your list item controls have events. – Let'sRefactor Jan 03 '16 at 16:11
  • Thankyou so much! Worked for me :) – TeeKay Jan 03 '16 at 16:18
0

in your layout xml, add this property to root layout

android:descendantFocusability="blocksDescendants"

if the listview has button or sort the the focus is on the item for your case it is the checkbox and add this to the checkbox

android:focusable="false"
android:focusableInTouchMode="false"
Ben_Gratvol
  • 84
  • 1
  • 12
0

you are passing the wrong class context.

Intent intent = new Intent(Main.this,Second.class);

startActivity(intent);

and your class is SavedAddress2

change it to

Intent intent = new Intent(SavedAddress2.this,Second.class);

startActivity(intent);

public class SavedAddress2 extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private Toolbar toolbar;
    private ListView listView1;
    List<list_addr> rowitems;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.saved_address2);

        toolbar = (Toolbar) findViewById(R.id.tool_bar3);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowHomeEnabled(true);

        rowitems = new ArrayList<list_addr>();

        listView1 = (ListView) findViewById(R.id.addr_list);
        list_addr_adapter adapter = new list_addr_adapter(this,
                R.layout.items_sav_addr2, SavedAddress.rowitems);
        listView1.setAdapter(adapter);
        listView1.setOnItemClickListener(this);
    }

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(Main.this,Second.class);
        startActivity(intent);
    }

    //...
}

Let me know if this works for you !:)

Vivek Bhardwaj
  • 530
  • 5
  • 16