How to Do Action on ListView Item Click, without extending ListActivity
1. Add a ListView.
2. Open the MainActivity.java file and add these to the import section.
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
3. Add the below section of code to the onCreate method.
myListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id) {
// Do action item here
//.....
// Example, Display Toast of Position
Toast.makeText(getApplicationContext(), " " + position , Toast.LENGTH_LONG).show();
// Other example action items
// Remove Selected Item from ListView
}
});
4. Compile and run!
Popular Posts
- android:ellipsize - Add an ellipsis(...) in a TextView
- Save LogCat to a Text File
- android:typeface - Set the Typeface to Sans, Serif, or Monospace in a TextView/EditText
- Perform an Action after Enter keypress on EditText
- android:layerType
- How to Get Current Time in Milliseconds
- Capitalize Text in a TextView, using inputType
- Intent setType(String type) - How to Set an Explicit MIME data type
- android:alpha - Set the Opacity/Transparency of Text in TextView/EditText
- Deprecated: android:singleLine - Set a TextView to 1 Line with ellipsis(...) at end