java - android setAdapter 不触发 getView 或不构建 ListView

标签 java android listview baseadapter

问了太多次,但我在自己的代码中找不到问题。

和其他人一样,我有一个 ArrayList,我需要显示它。

问题是当 Activity 启动时 ListView 不可见,我不知道为什么。

这是我的代码:

布局:activity_dashboard_screen.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/ly_dashboard"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.ciroreed.war_horizon.ui.Dashboard_screen" >

<TextView
    android:id="@+id/tv_dashboard_display_username"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="username"
    tools:ignore="HardcodedText" />

<TableRow
    android:id="@+id/row_dashboard_1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="end" >

</TableRow>

<TableRow
    android:id="@+id/row_dashboard_2"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/lv_dashboard_matchlist"
        android:layout_width="fill_parent"
        android:layout_height="match_parent" />

</TableRow>

扩展BaseAdapter的自定义类:MatchAdapter.java

    package com.ciroreed.war_horizon;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.TwoLineListItem;

import com.ciroreed.war_horizon.data.Match;

public class MatchAdapter extends BaseAdapter {

    private Context con;
    private ArrayList<Match> MATCHLIST;


    public MatchAdapter(Context appcontext, ArrayList<Match> matchlist){
        con = appcontext;
        MATCHLIST = matchlist;

    }

..............

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

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) con
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            tll = (TwoLineListItem) inflater.inflate(
                    android.R.layout.simple_list_item_2, parent);
        } else {
            tll = (TwoLineListItem) convertView;
        }

        TextView text1 = tll.getText1();
        TextView text2 = tll.getText2();

        text1.setText("VS "+MATCHLIST.get(position).getEnemy_name());
        text2.setText("turno: "+MATCHLIST.get(position).isPlayerTurn());

        return tll;
    }

}

这是可以运行它的 Activity :

    package com.ciroreed.war_horizon.ui;

import java.util.concurrent.ExecutionException;

import org.json.JSONException;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ciroreed.war_horizon.Controller;
import com.ciroreed.war_horizon.MatchAdapter;
import com.ciroreed.war_horizon.R;

public class Dashboard_screen extends Activity implements OnClickListener{

    private TextView tv_dashboard_username;
    private ListView lv_dashboard_matchlist;

    private MatchAdapter MATCHLIST_adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dashboard_screen);

        init();
    }

    private void init() {
        // TODO Auto-generated method stub
        tv_dashboard_username = (TextView)findViewById(R.id.tv_dashboard_display_username);
        lv_dashboard_matchlist = (ListView)findViewById(R.id.lv_dashboard_matchlist);


        try {
            if(Controller.getCurrentMatches()){

                MATCHLIST_adapter = new MatchAdapter(getApplicationContext(), Controller.MATCHLIST);    

                Toast.makeText(getApplicationContext(), "Partidas cargadas correctamente", Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(getApplicationContext(), "Error al cargar las partidas", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        MATCHLIST_adapter.notifyDataSetChanged();
        lv_dashboard_matchlist.setAdapter(MATCHLIST_adapter);
    }

当我执行时,.. 好吧,这不是主要 Activity 。它按预期运行,但是 ArrayList<Match>包含一个元素(至少)..

请不要询问前面的代码如何工作,如“Controller.getCurrentMatches()”,即该方法从远程服务器获取包含数据的 json,除了与 ListAdapter 相关的代码之外,其他所有内容都已在之前测试过并且工作正常。

最佳答案

您在适配器的 getCount() 中做了什么? 您应该在 getCount()

中返回一个 > 0 的值

关于java - android setAdapter 不触发 getView 或不构建 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26684034/

相关文章:

java - JButton & Action & KeyBinding

android - 在 recyclerview 中使用多个标题

android - ListView 工具 :listitem not working in android studio 2. 2 稳定 channel

java - 远程分析 JVM

java - 获取所有系统壁纸

安卓屏幕尺寸

Android ListView 类似于 iOS UITableView

android - ListView 标题拉伸(stretch)

java - Tomcat7 启动时间过长

android - 支持华为IAP多种构建类型