This has been driving me crazy over the past several hours... and I feel like it's something obvious...
I've gotten my code down to a simple ListView with a single TextView in it, but still can't seem to make it work (I don't see the toast or log message in the console).
Any help is greatly appreciated! :)
MainActivity.java
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SparseArray<Data> data = createData();
ListView listView = (ListView) findViewById(R.id.listView);
final CustomListAdapter adapter = new CustomListAdapter(this, data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
Log.v("onItemClick", "Clicked");
Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
CustomListAdapter.java
public class CustomListAdapter extends BaseAdapter {
private final SparseArray<Data> data;
private LayoutInflater inflater;
private final Activity activity;
public CustomListAdapter(Activity activity, SparseArray<Data> data) {
this.activity = activity;
this.data = data;
this.inflater = activity.getLayoutInflater();
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
Log.v("getItem", "" + position);
return data.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getItemViewType(int position) {
//TODO: Implement Image vs no image views
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parentView) {
if(convertView == null) {
convertView = inflater.inflate(R.layout.data, null);
}
Data data = (Data) getItem(position);
((TextView) convertView.findViewById(R.id.dataTitle)).setText(data.getTitle());
return convertView;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
@Override
public boolean isEmpty() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEnabled(int arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:background="@color/background" >
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background" />
</LinearLayout>
data.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp" >
<TextView
android:id="@+id/dataTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"
android:paddingLeft="0dp"
android:paddingBottom="8dp"
android:textSize="26sp"
android:textColor="@color/dataTitle" />
</LinearLayout>
NOTE: I do see that the touch even is at least being registered by the device:
07-15 22:00:50.734: I/InputReader(804): Touch event's action is 0x0 (deviceType=0) [pCnt=1, s=0.23535 ] when=378716384422000
07-15 22:00:50.734: I/InputDispatcher(804): Delivering touch to: action: 0x4
07-15 22:00:50.734: I/InputDispatcher(804): Delivering touch to: action: 0x0
android:clickable="false" from the TextView.android:focusable="false". You can't catch clicks if you have these set as false.
Secondly,
@Override
public boolean areAllItemsEnabled() {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isEnabled(int arg0) {
// TODO Auto-generated method stub
return false;
}
Return true in these methods. You won't be able to catch clicks if these return false for every item.
IMO it's not a good idea to override methods that you do not necessarily have to or are unsure as to what the method should return.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With