Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.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
Android-ListView适配器不可见_Android_Listview - Fatal编程技术网

Android-ListView适配器不可见

Android-ListView适配器不可见,android,listview,Android,Listview,正在尝试实现高分ListView。我想向我的ListView添加一个适配器。我用3个TextView元素构建了自己的适配器。我的ListView甚至没有显示在我的活动中 private class HighscoreAdapter extends ArrayAdapter<String> { private class ViewHolder { private TextView name; private TextView highscore

正在尝试实现高分ListView。我想向我的ListView添加一个适配器。我用3个TextView元素构建了自己的适配器。我的ListView甚至没有显示在我的活动中

private class HighscoreAdapter extends ArrayAdapter<String> {

    private class ViewHolder {
        private TextView name;
        private TextView highscore;
        private TextView date;
    }

    private final Context context;
    private final String[] values;
    private final int textViewResourceId;

    public HighscoreAdapter(Context context, int textViewResourceId, String[] values) {
        super(context, textViewResourceId);
        this.textViewResourceId = textViewResourceId;
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        final ViewHolder viewHolder;

        if (convertView == null) {
            convertView = getLayoutInflater().inflate(textViewResourceId, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.name = (TextView) convertView.findViewById(R.id.highscore_name);
            viewHolder.highscore = (TextView) convertView.findViewById(R.id.highscore_points);
            viewHolder.date = (TextView) convertView.findViewById(R.id.highscore_date);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.name.setText("xman");
        viewHolder.highscore.setText("2000");
        viewHolder.date.setText("today");

        return convertView;
    }
}
这就是我的活动布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >
....

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="70dp"
    android:layout_marginBottom="70dp"
    android:id="@+id/highscore_listview"
    android:layout_gravity="center_horizontal" />
</RelativeLayout>

....
这是我的list_line.xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >

    <TextView
        android:id="@+id/icon"
        android:layout_width="30dip"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="6dip"
        android:contentDescription="TODO"
        android:textSize="40sp"
        android:text="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_toRightOf="@+id/icon"
        android:text="Large Text"
        android:id="@+id/highscore_name"
        android:layout_centerVertical="true"
        android:layout_toEndOf="@+id/icon" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="20.000"
        android:id="@+id/highscore_points"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:textSize="23dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="30 Dec 2015"
        android:id="@+id/highscore_date"
        android:layout_below="@id/highscore_points"
        android:layout_alignParentEnd="true"
        android:textSize="15dp" />

</RelativeLayout>

这就是问题所在

final ListView listview = (ListView) findViewById(android.R.id.list);
您在此处使用了错误的
ListView
id(
android.R.id.list

将其更改为类似于布局
R.layout.highscore\u活动中的
R.id.my\u列表

更新

改变

final ListView listview = (ListView) findViewById(android.R.id.list);

这就是问题所在

final ListView listview = (ListView) findViewById(android.R.id.list);
您在此处使用了错误的
ListView
id(
android.R.id.list

将其更改为类似于布局
R.layout.highscore\u活动中的
R.id.my\u列表

更新

改变

final ListView listview = (ListView) findViewById(android.R.id.list);

这个

应该是

final ListView listview = (ListView) findViewById(android.R.id.highscore_listview);
这个

应该是

final ListView listview = (ListView) findViewById(android.R.id.highscore_listview);

您的ListView应该如下所示:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="70dp"
    android:layout_marginBottom="70dp"
    android:id="@android:id/list"
    android:layout_gravity="center_horizontal" />

您的ListView应该如下所示:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="70dp"
    android:layout_marginBottom="70dp"
    android:id="@android:id/list"
    android:layout_gravity="center_horizontal" />

如下所示更改适配器:

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.TextView;

import java.util.ArrayList;

public class HighScoreAdapter extends BaseAdapter {
private ArrayList<String> items;
private Context context;

public HighScoreAdapter(Context context, ArrayList<String> items){
    this.context = context;
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater layoutInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_line, null);
    }
    TextView name, highscore, date;
    name = (TextView) convertView.findViewById(R.id.highscore_name);
    highscore = (TextView) convertView.findViewById(R.id.highscore_points);
    date = (TextView) convertView.findViewById(R.id.highscore_date);

    name.setText(items.get(position));
    highscore.setText(items.get(position));
    date.setText(items.get(position));
    return convertView;
}
String[] values = new String[] { "one", "two", "three"};

    ArrayList<String> items = new ArrayList<>();
    for (String item : values){
        items.add(item);
    }

    final HighScoreAdapter highscoreAdapter = new HighScoreAdapter(this, items);
    listview.setAdapter(highscoreAdapter);
导入android.app.Activity;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类HighScoreAdapter扩展了BaseAdapter{
私有ArrayList项;
私人语境;
公共HighScoreAdapter(上下文、ArrayList项){
this.context=上下文;
这个项目=项目;
}
@凌驾
public int getCount(){
返回items.size();
}
@凌驾
公共对象getItem(int位置){
返回项目。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)
context.getSystemService(Activity.LAYOUT\u INFLATER\u SERVICE);
convertView=LayoutFlater.充气(R.layout.list_行,空);
}
文本视图名称、高分、日期;
name=(TextView)convertView.findViewById(R.id.highscore_name);
highscore=(TextView)convertView.findViewById(R.id.highscore_points);
日期=(TextView)convertView.findViewById(R.id.highscore\u日期);
name.setText(items.get(position));
highscore.setText(items.get(position));
date.setText(items.get(position));
返回视图;
}
}

然后像下面那样使用它:

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.TextView;

import java.util.ArrayList;

public class HighScoreAdapter extends BaseAdapter {
private ArrayList<String> items;
private Context context;

public HighScoreAdapter(Context context, ArrayList<String> items){
    this.context = context;
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater layoutInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_line, null);
    }
    TextView name, highscore, date;
    name = (TextView) convertView.findViewById(R.id.highscore_name);
    highscore = (TextView) convertView.findViewById(R.id.highscore_points);
    date = (TextView) convertView.findViewById(R.id.highscore_date);

    name.setText(items.get(position));
    highscore.setText(items.get(position));
    date.setText(items.get(position));
    return convertView;
}
String[] values = new String[] { "one", "two", "three"};

    ArrayList<String> items = new ArrayList<>();
    for (String item : values){
        items.add(item);
    }

    final HighScoreAdapter highscoreAdapter = new HighScoreAdapter(this, items);
    listview.setAdapter(highscoreAdapter);
String[]values=新字符串[]{“一”、“二”、“三”};
ArrayList items=新建ArrayList();
用于(字符串项:值){
项目。添加(项目);
}
最终HighScoreAdapter HighScoreAdapter=新的HighScoreAdapter(此,项);
setAdapter(highscoreAdapter);

如下所示更改适配器:

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.TextView;

import java.util.ArrayList;

public class HighScoreAdapter extends BaseAdapter {
private ArrayList<String> items;
private Context context;

public HighScoreAdapter(Context context, ArrayList<String> items){
    this.context = context;
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater layoutInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_line, null);
    }
    TextView name, highscore, date;
    name = (TextView) convertView.findViewById(R.id.highscore_name);
    highscore = (TextView) convertView.findViewById(R.id.highscore_points);
    date = (TextView) convertView.findViewById(R.id.highscore_date);

    name.setText(items.get(position));
    highscore.setText(items.get(position));
    date.setText(items.get(position));
    return convertView;
}
String[] values = new String[] { "one", "two", "three"};

    ArrayList<String> items = new ArrayList<>();
    for (String item : values){
        items.add(item);
    }

    final HighScoreAdapter highscoreAdapter = new HighScoreAdapter(this, items);
    listview.setAdapter(highscoreAdapter);
导入android.app.Activity;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.TextView;
导入java.util.ArrayList;
公共类HighScoreAdapter扩展了BaseAdapter{
私有ArrayList项;
私人语境;
公共HighScoreAdapter(上下文、ArrayList项){
this.context=上下文;
这个项目=项目;
}
@凌驾
public int getCount(){
返回items.size();
}
@凌驾
公共对象getItem(int位置){
返回项目。获取(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)
context.getSystemService(Activity.LAYOUT\u INFLATER\u SERVICE);
convertView=LayoutFlater.充气(R.layout.list_行,空);
}
文本视图名称、高分、日期;
name=(TextView)convertView.findViewById(R.id.highscore_name);
highscore=(TextView)convertView.findViewById(R.id.highscore_points);
日期=(TextView)convertView.findViewById(R.id.highscore\u日期);
name.setText(items.get(position));
highscore.setText(items.get(position));
date.setText(items.get(position));
返回视图;
}
}

然后像下面那样使用它:

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.TextView;

import java.util.ArrayList;

public class HighScoreAdapter extends BaseAdapter {
private ArrayList<String> items;
private Context context;

public HighScoreAdapter(Context context, ArrayList<String> items){
    this.context = context;
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

@Override
public Object getItem(int position) {
    return items.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView == null){
        LayoutInflater layoutInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.list_line, null);
    }
    TextView name, highscore, date;
    name = (TextView) convertView.findViewById(R.id.highscore_name);
    highscore = (TextView) convertView.findViewById(R.id.highscore_points);
    date = (TextView) convertView.findViewById(R.id.highscore_date);

    name.setText(items.get(position));
    highscore.setText(items.get(position));
    date.setText(items.get(position));
    return convertView;
}
String[] values = new String[] { "one", "two", "three"};

    ArrayList<String> items = new ArrayList<>();
    for (String item : values){
        items.add(item);
    }

    final HighScoreAdapter highscoreAdapter = new HighScoreAdapter(this, items);
    listview.setAdapter(highscoreAdapter);
String[]values=新字符串[]{“一”、“二”、“三”};
ArrayList items=新建ArrayList();
用于(字符串项:值){
项目。添加(项目);
}
最终HighScoreAdapter HighScoreAdapter=新的HighScoreAdapter(此,项);
setAdapter(highscoreAdapter);

首先更改此行:

ListView listview = (ListView) findViewById(android.R.id.list);
致:

**您应该使用包R类来查找id,并且应该设置正确的id:D

一些提示:

为什么两个文本视图使用相同的id?每个视图使用uniqe id!
我认为最后一个关键字在这里是无用的!为什么对listview使用final???

首先更改此行:

ListView listview = (ListView) findViewById(android.R.id.list);
致:

**您应该使用包R类来查找id,并且应该设置正确的id:D

一些提示:

为什么两个文本视图使用相同的id?每个视图使用uniqe id!
我认为最后一个关键字在这里是无用的!为什么在listview中使用final???

我使用了这两种方法,我还在“R.layout.highscore\u活动”中将其更改为“R.id.my\u list”。没有更改…请发布您的xml布局。谢谢,我必须使用BaseAdapter。我使用了两种方法,我还将其更改为“R.layout.highscore_活动”中的“R.id.my_list”。没有更改…请发布您的xml布局。谢谢。我必须使用BaseAdapter。请将您的.xml文件添加到我在您的xml布局中没有看到listview。您是否尝试扩展ListActivity?这就是android.R.id.list的工作方式。plz将您的.xml文件添加到我在您的xml布局中没有看到listview。您是否尝试扩展ListActivity?这就是android.R.id.list的工作方式。我忽略了我