Os : Arch linux
Android Studio = 3.1.4
I'm working on app that needs custom listView and I have a problem when I want to make custom listview in android.
My Custom listView Class:
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimatedStateListDrawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomListView extends ArrayAdapter<String> {
private static final String TAG = "CustomListView";
private final int layoutResource;
private final LayoutInflater layoutInflater;
private String _read_points;
private String _team1_points;
private String _team2_points;
private String _team1_name;
private String _team2_name;
private String _arrow;
public CustomListView(@NonNull Context context, int resource,
String team1_name, String team2_name, String team1Points, String team2Points,
String readPoints, String arrow) {
super(context, resource);
this.layoutResource = resource;
this.layoutInflater = LayoutInflater.from(context);
this._read_points = readPoints;
this._team1_name = team1_name;
this._team2_name = team2_name;
this._team1_points = team1Points;
this._team2_points = team2Points;
this._arrow = arrow;
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
Log.d(TAG, "getView: Called with null convert view");
convertView = layoutInflater.inflate(layoutResource, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
Log.d(TAG, "getView: Provided a convert view");
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView_team1_name.setText(_team1_name);
viewHolder.textView_team2_name.setText(_team2_name);
viewHolder.textView_team1_points.setText(_team1_points);
viewHolder.textView_team2_points.setText(_team2_points);
viewHolder.textView_read_points.setText(_read_points);
viewHolder.textView_arrow.setText(_arrow);
return convertView;
//return super.getView(position, convertView, parent);
}
private class ViewHolder {
final TextView textView_team1_name;
final TextView textView_team2_name;
final TextView textView_team1_points;
final TextView textView_team2_points;
final TextView textView_arrow;
final TextView textView_read_points;
ViewHolder(View v) {
this.textView_team1_name = v.findViewById(R.id.textView_team1_name);
this.textView_team2_name = v.findViewById(R.id.textView_team2_name);
this.textView_team1_points = v.findViewById(R.id.textView_team1_points);
this.textView_team2_points = v.findViewById(R.id.textView_team2_points);
this.textView_arrow = v.findViewById(R.id.textView_arrow);
this.textView_read_points = v.findViewById(R.id.textView_read_points);
}
}
}
My ListActivity Class:
private void addLineToListView() {
String _arrow = "-->";
CustomListView customListView = new CustomListView(ListActivity.this, R.layout.custom_list_view, _first_team_name, _second_team_name,
_first_team_score,_second_team_score, _read_point_from_dialog, _arrow);
_listView_results.setAdapter(customListView);
}
Custom List View XML
<?xml version="1.0" encoding="utf-8"?>
<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">
<TextView
android:id="@+id/textView_team1_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Team1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_team2_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="Team2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView_team1_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="100"
app:layout_constraintEnd_toEndOf="@+id/textView_team1_name"
app:layout_constraintStart_toStartOf="@+id/textView_team1_name"
app:layout_constraintTop_toBottomOf="@+id/textView_team1_name" />
<TextView
android:id="@+id/textView_team2_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="150"
app:layout_constraintEnd_toEndOf="@+id/textView_team2_name"
app:layout_constraintStart_toStartOf="@+id/textView_team2_name"
app:layout_constraintTop_toBottomOf="@+id/textView_team2_name" />
<TextView
android:id="@+id/textView_label_read_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:text="Read Points"
app:layout_constraintEnd_toStartOf="@+id/textView_team2_name"
app:layout_constraintStart_toEndOf="@+id/textView_team1_name"
tools:layout_editor_absoluteY="16dp" />
<TextView
android:id="@+id/textView_read_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:text="125"
app:layout_constraintEnd_toEndOf="@+id/textView_label_read_points"
app:layout_constraintHorizontal_bias="0.473"
app:layout_constraintStart_toStartOf="@+id/textView_label_read_points"
app:layout_constraintTop_toBottomOf="@+id/textView_arrow" />
<TextView
android:id="@+id/textView_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="-->"
app:layout_constraintEnd_toEndOf="@+id/textView_label_read_points"
app:layout_constraintStart_toStartOf="@+id/textView_label_read_points"
app:layout_constraintTop_toBottomOf="@+id/textView_label_read_points" />
</android.support.constraint.ConstraintLayout>
I can't see this logs in logcat window:
Log.d(TAG, "getView: Called with null convert view");
and
Log.d(TAG, "getView: Provided a convert view");
App run without error but list view is empty.
where is the problem?
addLineToListView()from your activity onCreate?