自定义listView的问题-列表视图为空

时间:2018-10-01 22:05:21

标签: java android

  

Os:Arch Linux

     

Android Studio = 3.1.4

我正在开发需要自定义listView的应用程序,当我想在android中制作自定义listview时遇到问题。

我的自定义listView类:

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);
        }
    }
}

我的ListActivity类:

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);
    }

自定义列表视图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>

我在logcat窗口中看不到此日志:

Log.d(TAG, "getView: Called with null convert view");

Log.d(TAG, "getView: Provided a convert view");

应用正常运行,但列表视图为空。

问题出在哪里?

1 个答案:

答案 0 :(得分:1)

先生,我为您准备了这个,希望对您有帮助。

1 -声明您的ArrayList并使用此数据填充哈希图

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) {

        }
    });

提示:您可以通过此代码制作越来越多的数据

        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 -这是您的新课程

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;
}
}

如果在我遇到的任何问题之前没有使用Hashmap,请尝试着重于提示

快乐代码^^