I am trying to add a feature where, whenever the user clicks on an editText to start typing in the last empty gridview item, a new empty gridview item will be added. The problem is that the listener doesn't get called when the user starts typing. I know there are some questions here about similar issues, but my gridView has an editText in it which is what I think the problem is.
Here is the listener I have:
roleLayout.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println(roleArrayList.get(position).getRole() + "******");
if(roleArrayList.get(position).getRole().equals("")) {
if (roleArrayList.size() == 1 ) {
roleArrayList.add(new NewRoleElement());
} else {
if (!roleArrayList.get(roleArrayList.size() - 1).getRole().equals("")) {
roleArrayList.add(new NewRoleElement());
}
}
}
}
});
And here is the xml code for my GridView:
<GridView
android:id="@+id/roleList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:numColumns="1"
android:stretchMode="columnWidth" />
Here is my xml code for the actual item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_rowWeight="1"
android:background="@drawable/my_layout_bg"
android:orientation="horizontal">
<EditText
android:id="@+id/roleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:ems="10"
android:hint="Enter a new role"
android:inputType="textPersonName" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="Repeat?" />
</LinearLayout>
My adapter code is here:
public class RoleAdapter extends BaseAdapter{
Context c;
public RoleAdapter(Context context) {
this.c = context;
}
@Override
public int getCount() {
return roleArrayList.size();
}
@Override
public Object getItem(int position) {
return roleArrayList.get(position);
}
@Override
public long getItemId(int position) {
return roleArrayList.indexOf(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.role_template, null);
}
roleArrayList.get(position).setRoleInput((EditText) convertView.findViewById(R.id.roleText));
roleArrayList.get(position).setRepeatCheckBox((CheckBox) convertView.findViewById(R.id.checkBox));
return convertView;
}
}
The main problem is that
EditTextandCheckBoxcapture the focusTry this in your code.
Add
android:descendantFocusability="blocksDescendants"in the root xml code .Another way
Also you can set
android:focusable="false"in theEditTextand setandroid:clickable="false"in theCheckBox.