Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义listView出现问题-列表视图为空_Java_Android - Fatal编程技术网

Java 自定义listView出现问题-列表视图为空

Java 自定义listView出现问题-列表视图为空,java,android,Java,Android,操作系统:ArchLinux Android Studio=3.1.4 我正在开发需要自定义listView的应用程序,当我想在android中自定义listView时,我遇到了一个问题 我的自定义listView类: import android.app.Activity; import android.content.Context; import android.graphics.drawable.AnimatedStateListDrawable; import android.supp

操作系统:ArchLinux

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);
        }
    }
}
自定义列表视图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(标记“getView:使用空转换视图调用”)

Log.d(标记“getView:提供了一个转换视图”)

应用程序运行时无错误,但列表视图为空


哪里出问题了?

先生,我为您做了这个,希望能有所帮助

1-声明您的ArrayList并用此数据填充hashmap

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

        }
    });
ArrayList xxxxxxx=新的ArrayList();
HashMap=newHashMap();
//将每个子节点添加到HashMap key=>value
map.put(“team1_name”,第一个团队名称);
map.put(“第二团队名称”、“第二团队名称”);
map.put(“团队1分”,第一团队得分);
map.put(“团队2分”,第二个团队得分);
map.put(“读取点”,“从对话框读取点”);
地图。放置(“箭头”,“-->”);
//将哈希列表添加到ArrayList
xxxxxxx.添加(地图);
_listView_结果=(listView)findViewById(R.id.listView_结果);
//通过传递xml数据ArrayList获取适配器
CustomListView适配器=新的CustomListView(ListActivity.this,xxxxxxx);
_listView_results.setAdapter(适配器);
//单击单个列表行的事件
_listView_results.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长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); 
map=newhashmap();
//将每个子节点添加到HashMap key=>value
map.put(“team1_name”,第一个团队名称);
map.put(“第二团队名称”、“第二团队名称”);
map.put(“团队1分”,第一团队得分);
map.put(“团队2分”,第二个团队得分);
map.put(“读取点”,“从对话框读取点”);
地图。放置(“箭头”,“-->”);
//将哈希列表添加到ArrayList
xxxxxxx.添加(地图);
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;
}
}
import java.util.ArrayList;
导入java.util.HashMap;
导入android.app.Activity;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
公共类CustomListView扩展BaseAdapter{
私人活动;
私有数组列表数据;
专用静态充气机=空;
公共CustomListView(活动a,ArrayList d){
活动=a;
数据=d;
充气器=(LayoutInflater)activity.getSystemService(Context.LAYOUT\u充气器\u SERVICE);
}
public int getCount(){
返回data.size();
}
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图vi=转换视图;
if(convertView==null)
vi=充气机。充气(R.layout.custom\u list\u视图,空);
this.textView\u team1\u name=vi.findviewbyd(R.id.textView\u team1\u name);
this.textView\u team2\u name=vi.findviewbyd(R.id.textView\u team2\u name);
this.textView\u team1\u points=vi.findViewById(R.id.textView\u team1\u points);
this.textView\u team2\u points=vi.findViewById(R.id.textView\u team2\u points);
this.textView_arrow=vi.findviewbyd(R.id.textView_arrow);
this.textView\u read\u points=vi.findviewbyd(R.id.textView\u read\u points);
HashMap singeldata=新HashMap();
SingleData=数据获取(位置);
//在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”);
返回vi;
}
}
如果你没有使用Hashmap,那么在我和你一起做任何事情之前,试着把注意力集中在提示上


Happy Code^

是否在创建时从活动中调用addLineToListView()
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;
}
}