How to Handle the Click Event in Listview in Android

How to handle the click event in Listview in android?

I can not see where do you declare context. For the purpose of the intent creation you can use MainActivity.this

 lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(MainActivity.this, SendMessage.class);
String message = "abc";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});

To retrieve the object upon you have clicked you can use the AdapterView:

ListEntry entry = (ListEntry) parent.getItemAtPosition(position);

How to handle ListView click in Android

On your list view, use setOnItemClickListener

Android How to set onClick event in list item of ListView


list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Intent intent = new Intent(context, SendMessage.class);
String message = "abcpqr";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
});

try this way.

How can I handle a click event in Listview?

have you declared this activity in your AndroidManifest.xml?

Clicked Activity Declare in AndroidManifest file.

Handling click event in ListView items views in Android

There are actually lots of ways to do this.

  • You could create a Click adapter for each button that knew which Button it was attached to.
  • You could put a unique tag on each button that the click handler identified and acted on.
  • You could build an ArrayList linking each Button to a code telling the click handler what to do ...
     - As Alexander says, you can create a custom adapter for the ListView which handles the buttons. If you use this method, then you need to create the onClick handlers in

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
    // Create all the Views, Buttons, etc

    // Create the click handlers:
    button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
    doButtonOneClickActions(position);
    }
    });
    }

    private void doButtonOneClickActions(int rowNumber) {
    // Do the actions for Button one in row rowNumber (starts at zero)
    }

It rather depends on the rest of your code (and your preferred coding style) which solution to go for ..

How to call click event on Android ListView Header?

just try this..

itemView.setOnClickListner(new View.OnClickListner()
{
@Override
public void onClick(View v)
{
}
}

If you have TextView in buypwr.xml then try this

TextView txt = (TextView)itemView.findViewById(......);
txt.setOnClickListner(new View.OnClickListner()
{
@Override
public void onClick(View v)
{
}
}


Related Topics



Leave a reply



Submit