Simran
Simran

Reputation: 25

listview item cannot be click in android

i have created Custom listview and i add a onclick listener to it but it was not working.the below is my code

This is my MainActivity Class:

public class MainActivity extends Activity  {
    public boolean[] status;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_main);
    // Attach the adapter to a ListView
                listView = (ListView) findViewById(R.id.lvUsers);
    populateUsersList();

    listView.setOnItemClickListener(new OnItemClickListener() {
         public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id) {
              Toast.makeText(getBaseContext(),"Hello", Toast.LENGTH_SHORT).show();
         }
         });

    }

    private void populateUsersList() {
        // Construct the data source
        ArrayList<User> arrayOfUsers = User.getUsers();
        // Create the adapter to convert the array to views
        CustomUsersAdapter adapter = new CustomUsersAdapter(this,  arrayOfUsers);


        listView.setAdapter(adapter);
    }


   }

this is my adapter class:

public class CustomUsersAdapter extends ArrayAdapter<User> {
        public CustomUsersAdapter(Context context, ArrayList<User> users) {
            super(context, 0, users);
        }

 @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    User user = getItem(position);    
    // Check if an existing view is being reused, otherwise inflate the view
    if (convertView == null) {
       convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
    }
    // Lookup view for data population
    TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
    TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
    ToggleButton tgbtn = (ToggleButton)     convertView.findViewById(R.id.toggleButton1);
    // Populate the data into the template view using the data object
    tvName.setText(user.name);
    tvHome.setText(user.hometown);

    // Return the completed view to render on screen
    return convertView;
        }
    }

i want to get the value from listview when the appropriate toggle button is checked .and want to save the value of toggle button and the appropriate text in arraylist.

this my xml :

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <ListView
    android:id="@+id/lvUsers"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
  />

 </LinearLayout>

This is my second xml :

  <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="10dp"
    >

   <TextView
    android:id="@+id/tvName"
    android:layout_alignBaseline="@+id/ivUserIcon"
    android:layout_toRightOf="@+id/ivUserIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:paddingRight="6dp"
    android:text="Sarah"
    android:textAppearance="?android:attr/textAppearanceLarge" />

  <TextView
    android:id="@+id/tvHometown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/tvName"
    android:layout_below="@+id/tvName"
    android:text="San Marco"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ToggleButton
    android:id="@+id/toggleButton1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/tvName"
    android:layout_marginRight="15dp"
    android:text="ToggleButton" />

   </RelativeLayout>

Upvotes: 0

Views: 219

Answers (3)

Agi Maulana
Agi Maulana

Reputation: 497

below of your code that there is a ListView and you wanna able clicked function to the list item, write this code.

 public void onListItemClick(ListView parent, View v, int position, long id){
        // do something here
        // Example below make a toast message
        Toast.makeText(getApplicationContext(), "I clicked item of list number " + position,Toast.LENGTH_SHORT).show();
 }

Upvotes: 0

Robin Chander
Robin Chander

Reputation: 7415

Change your RelativeLayout to

<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="10dp"
 android:descendantFocusability="blocksDescendants"
>

This view will block any of its descendants from getting focus, even if they are focusable.

Upvotes: 1

tobias s
tobias s

Reputation: 120

You must set the button on clicklistener.

tgbtn.setOnClickListener(new OnClickListener(position));

Dont know it it works but try anything like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);    
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
   convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
ToggleButton tgbtn = (ToggleButton)     convertView.findViewById(R.id.toggleButton1);
// Populate the data into the template view using the data object
tvName.setText(user.name);
tvHome.setText(user.hometown);
tgbtn.setOnClickListener(new OnButtonClickListener(position));
// Return the completed view to render on screen
return convertView;
    }

then a class for the ButtonClick:

private class OnButtonClickListener implements OnClickListener {

    int _postion;

    // constructor
    public OnButtonClickListener(int position) {
        this._postion = position;
    }

    @Override
    public void onClick(View v) {

    }

}

Upvotes: 0

Related Questions