I have created a study helper app for android. I use a custom array adapter that has a checkbox and a textview in each row. Some of my listviews are populated using this custom adapter. My listviews are used to navigate to other activities, and have onItemClick listeners.
On the android studio emulator, everything works fine. However, I exported the apk and installed it on my phone, and the listviews using my custom adapter no longer did anything on click.
Here is my custom adapter:
public class CheckboxAdapter extends BaseAdapter {
private Context context;
ArrayList<String[]> list = new ArrayList<>();
Boolean completed[];
public CheckboxAdapter(ArrayList<String[]> list,Context context) {
super();
this.context = context;
this.list = list;
completed = new Boolean[list.size()];
for (int i = 0; i < completed.length; i++){
if(list.get(i)[1].equalsIgnoreCase("true")){
completed[i] = true;
} else {
completed[i] = false;
}
}
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
// Class to temporarily store the textview and checkbox in each row
public class ViewHolder {
public TextView nametext;
public CheckBox tick;
}
// Override getView method to set custom row layout
// Use viewHolder to get the textview and checkbox then inflate it
// Set text to passed in list of string data
// Set checkbox to passed in array of booleans, then disable the checkbox
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder view = null;
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
if (view == null) {
view = new ViewHolder();
convertView = inflater.inflate( R.layout.checkbox_adapter, null);
view.nametext = (TextView) convertView.findViewById(R.id.adaptertextview);
view.tick = (CheckBox)convertView.findViewById(R.id.adaptercheckbox);
convertView.setTag(view);
} else {
view = (ViewHolder) convertView.getTag();
}
view.tick.setTag(position);
view.nametext.setText(list.get(position)[0]);
view.tick.setChecked(completed[position]);
view.tick.setEnabled(false);
return convertView;
}
}
And its XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/adaptercheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/adaptertextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Here is an example of a listview using the custom adapter, and its onclick function:
ArrayList<String[]> content = new ArrayList<String[]>();
content.add(new String[] {"Lesson 1 Highest Mark: " + highest[0], completed[0]});
content.add(new String[] {"Lesson 2 Highest Mark: " + highest[1], completed[1]});
content.add(new String[] {"Lesson 3 Highest Mark: " + highest[2], completed[2]});
content.add(new String[] {"Lesson 4 Highest Mark: " + highest[3], completed[3]});
content.add(new String[] {"Lesson 5 Highest Mark: " + highest[4], completed[4]});
content.add(new String[] {"Combined Quiz", "false"});
ListView quizList = (ListView) findViewById(R.id.quizListView);
CheckboxAdapter adapter = new CheckboxAdapter(content, this);
quizList.setAdapter(adapter);
quizList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
System.out.println(parent. getItemAtPosition(position));
switch (position) {
case 0: Intent i0 = new Intent(Quiz.this, Quiz1.class);
startActivity(i0);
break;
case 1: Intent i1 = new Intent(Quiz.this, Quiz2.class);
startActivity(i1);
break;
case 2: Intent i2 = new Intent(Quiz.this, Quiz3.class);
startActivity(i2);
break;
case 3: Intent i3 = new Intent(Quiz.this, Quiz4.class);
startActivity(i3);
break;
case 4: Intent i4 = new Intent(Quiz.this, Quiz5.class);
startActivity(i4);
break;
case 5: Intent i5 = new Intent(Quiz.this, QuizCombined.class);
startActivity(i5);
break;
default: return;
}
}
});
Any ideas why this only works on the android studio emulator? I've tried on two devices and Memu emulator but it didn't work on any of them, only the android studio emulator.
EDIT: the xml for the activity using the above listview
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Quiz">
<TextView
android:id="@+id/quizTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="56dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/quizTextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/quizTextView" />
<ListView
android:id="@+id/quizListView"
android:layout_width="368dp"
android:layout_height="493dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/quizTextView2" />
</android.support.constraint.ConstraintLayout>
<include
layout="@layout/app_bar_main_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main_menu"
app:menu="@menu/activity_main_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
EDIT: I copied the project to my laptop and ran it in the emulator there, and it didn't work. Seems like it ONLY works on my pc