Reputation: 673
I have this class and XML
public class MyClass extends SherlockListActivity implements OnTouchListener
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:smoothScrollbar="true"
android:cacheColorHint="#fff"
android:fastScrollEnabled="false"
android:clickable="true"
android:layout_weight="1"
android:dividerHeight="1sp" />
I'm not able to detect a click. The only way to register a click is to drag. I don't understand what the problem is.
getListView().setOnTouchListener(this);
Here is my onTouch:
public boolean onTouch(View v, MotionEvent event)
{
Log.i("Click", "MAIN");
switch (event.getAction() & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
...
The only way to get any log is (or action) is by dragging. I tried click and long click and none works. Any idea how to fix this and keep onTouch?
Edit: The list view uses a different ArrayAdapter implementation (my own):
public class ContentDisplayListAdapter extends ArrayAdapter<DataContentsInfo> {
private List<DataContentsInfo> items;
private DataContentsInfo o;
public ContentDisplayListAdapter(Context context, int textViewResourceId, List<DataContentsInfo> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.content_display_row, null);
}
o = items.get(position);
if(o != null) {
TextView content = (TextView) v.findViewById(R.id.content);
TextView DataNo = (TextView) v.findViewById(R.id.title_no);
ContentDisplayAdapter.updateTitles(getContext(), o, content, DataNo, items.size());
}
return v;
}
}
Upvotes: 0
Views: 1090
Reputation: 1691
If you are not concerned with single clicks on any element in the listview, or the listview itself, then you can use the click listener for this. Something like the following...
First we need a subclass of OnItemClickListener
that only triggers on double clicks:
public abstract class DoubleClickListener implements OnItemClickListener {
// time between clicks before it is no longer considered a double click
private static final long DOUBLE_CLICK_TIME_DELTA = 200;
private long mLastClickTime = 0;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// check if this click occurred shortly after a previous click
long clickTime = System.currentTimeMillis();
if (clickTime - mLastClickTime < DOUBLE_CLICK_TIME_DELTA) {
onDoubleClick(view);
}
// save the time of this click for comparing against future clicks
mLastClickTime = clickTime;
}
// override this function with desired behavior
public abstract void onDoubleClick(View view);
}
Now set an instance of our new class as the click listener for our listview:
ListView listView = getListView();
listView.setOnItemClickListener(new DoubleClickListener() {
@Override
public void onDoubleClick(View v) {
// TODO - handle our double click here!
}
});
Upvotes: 0
Reputation: 537
This is the workin code..use it..cheers.. :)
public class ListViewAndroidExample extends Activity {
ListView listView ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_android_example);
// Get ListView object from xml
listView = (ListView) findViewById(R.id.list);
// Defined Array values to show in ListView
String[] values = new String[] { "Android List View",
"Adapter implementation",
"Simple List View In Android",
"Create List View Android",
"Android Example",
"List View Source Code",
"List View Array Adapter",
"Android Example List View"
};
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third parameter - ID of the TextView to which the data is written
// Forth - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
// Assign adapter to ListView
listView.setAdapter(adapter);
// ListView Item Click Listener
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
}
}
Upvotes: 1
Reputation: 537
This will work for you :)
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) listView.getItemAtPosition(position);
// Show Alert
Toast.makeText(getApplicationContext(),
"Position :"+itemPosition+" ListItem : " +itemValue , Toast.LENGTH_LONG)
.show();
}
});
Upvotes: 3
Reputation: 1704
Use setOnItemClickListener() that way list is also clickable whichever the clicked item do your work as it can be recocnized that list is clicked
Upvotes: 0