0

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?

1
  • are you calling addLineToListView() from your activity onCreate? Commented Oct 1, 2018 at 22:26

1 Answer 1

1

I made this for you sir hope it helps.

1 - Declare your ArrayList and fill the hashmap with this data

ArrayList<HashMap<String, String>> xxxxxxx = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map = new HashMap<String, String>(); 
        // adding each child node to HashMap key => value
        map.put("team1_name", _first_team_name); 
        map.put("team2_name", _second_team_name); 
        map.put("team1Points", _first_team_score); 
        map.put("team2Points", _second_team_score); 
        map.put("readPoints", _read_point_from_dialog); 
        map.put("arrow", "-->"); 
        // adding HashList to ArrayList
        xxxxxxx.add(map); 


    _listView_results=(ListView)findViewById(R.id.listView_results);

    // Getting adapter by passing xml data ArrayList
    CustomListView adapter=new CustomListView(ListActivity.this, xxxxxxx);
    _listView_results.setAdapter(adapter);

    // Click event for single list row
    _listView_results.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

        }
    });

HINT: you can make more and more data by this code

        map = new HashMap<String, String>(); 
        // adding each child node to HashMap key => value
        map.put("team1_name", _first_team_name); 
        map.put("team2_name", _second_team_name); 
        map.put("team1Points", _first_team_score); 
        map.put("team2Points", _second_team_score); 
        map.put("readPoints", _read_point_from_dialog); 
        map.put("arrow", "-->"); 
        // adding HashList to ArrayList
        xxxxxxx.add(map); 

2 - This is your new class

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListView extends BaseAdapter { 


private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater=null; 

public CustomListView(Activity a, ArrayList<HashMap<String, String>> d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
}

public int getCount() {
    return data.size();
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.custom_list_view, null);

        this.textView_team1_name = vi.findViewById(R.id.textView_team1_name);
        this.textView_team2_name = vi.findViewById(R.id.textView_team2_name);
        this.textView_team1_points = vi.findViewById(R.id.textView_team1_points);
        this.textView_team2_points = vi.findViewById(R.id.textView_team2_points);
        this.textView_arrow = vi.findViewById(R.id.textView_arrow);
        this.textView_read_points = vi.findViewById(R.id.textView_read_points);

    HashMap<String, String> singeldata = new HashMap<String, String>();
    singeldata = data.get(position);

    // Setting all values in listview
    textView_team1_name.setText(singeldata.get("team1_name"));
    textView_team2_name.setText(singeldata.get("team2_name"));
    textView_team1_points.setText(singeldata.get("team1_points"));
    textView_team2_points.setText(singeldata.get("team2_points"));
    textView_read_points.setText(singeldata.get("read_points"));
    textView_arrow.setText(singeldata.get("arrow"));
    return vi;
}
}

Try to focus on the HINT if you didn't work with Hashmap before anything I'm with you just ask

Happy Code ^^

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.