Java android setAdapter不';不触发getView或不';t生成列表视图
问了太多次,但我在自己的代码中找不到问题 和其他人一样,我有一个one ArrayList,需要显示它 问题是,当活动启动时,ListView不可见,我不知道为什么 这是我的密码: 布局:activity\u dashboard\u screen.xmlJava android setAdapter不';不触发getView或不';t生成列表视图,java,android,listview,baseadapter,Java,Android,Listview,Baseadapter,问了太多次,但我在自己的代码中找不到问题 和其他人一样,我有一个one ArrayList,需要显示它 问题是,当活动启动时,ListView不可见,我不知道为什么 这是我的密码: 布局:activity\u dashboard\u screen.xml <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
<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>
当我执行时。。嗯,这不是主要的活动。它按预期运行,但ArrayList
包含一个元素(至少)
请不要询问前面的代码是如何工作的,比如“Controller.getCurrentMatches()”,即该方法从远程服务器获取包含数据的json,除了与ListAdapter相关的代码之外,其他所有代码都在之前测试过,并且工作正常。来自
第十三章。教程:如何在列表视图中显示两个项目
package de.vogella.android.listactivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class MyTwoListItemsActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("Android", "Mobile"));
list.add(putData("Windows7", "Windows7"));
list.add(putData("iPhone", "iPhone"));
return list;
}
private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}
}
package de.vogella.android.listactivity;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.Map;
导入android.app.ListActivity;
导入android.os.Bundle;
导入android.widget.simpledapter;
公共类MyTwoListItemsActivity扩展了ListActivity{
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
ArrayList=buildData();
字符串[]from={“name”,“purpose”};
int[]to={android.R.id.text1,android.R.id.text2};
SimpleAdapter=新SimpleAdapter(此,列表,
android.R.layout.simple_list_item_2,from,to);
setListAdapter(适配器);
}
私有ArrayList buildData(){
ArrayList=新建ArrayList();
添加(putData(“安卓”、“移动”);
添加(putData(“Windows7”、“Windows7”));
添加(putData(“iPhone”、“iPhone”);
退货清单;
}
私有HashMap putData(字符串名称、字符串用途){
HashMap项=新建HashMap();
项目。填写(“名称”,名称);
项目.付诸表决(“目的”,目的);
退货项目;
}
}
这不是对这个问题的准确回答,但它解决了我的问题。您在适配器的
getCount()
中做了什么?
您应该在
getCount()
中返回一个大于0的值。您可以在自定义适配器中添加一些日志行,以查看是否调用了这些方法。您好,谢谢。适配器的构造函数被调用,但getView()不会被执行:\getCount之类的方法是如何实现的?getCount是由listview调用的吗?我需要的任何地方都是在同一行中播放两个项目…很抱歉,问题解决了这么久,但我没有公布答案。非常感谢。
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);
}
package de.vogella.android.listactivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class MyTwoListItemsActivity extends ListActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<Map<String, String>> list = buildData();
String[] from = { "name", "purpose" };
int[] to = { android.R.id.text1, android.R.id.text2 };
SimpleAdapter adapter = new SimpleAdapter(this, list,
android.R.layout.simple_list_item_2, from, to);
setListAdapter(adapter);
}
private ArrayList<Map<String, String>> buildData() {
ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(putData("Android", "Mobile"));
list.add(putData("Windows7", "Windows7"));
list.add(putData("iPhone", "iPhone"));
return list;
}
private HashMap<String, String> putData(String name, String purpose) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("name", name);
item.put("purpose", purpose);
return item;
}
}