我一直试图弄清楚这件事发生了一段时间,也许我已经盯着它看了太久......我觉得它很简单。
这是我的ListActivity类
public class ResultsListActivity extends ListActivity {
private ArrayList<Result> results = new ArrayList<Result>();
private ListView list;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sp_history);
//set up list
list = new ListView(this);
}
@Override
protected void onResume()
{
super.onResume();
results = openAndQueryDatabase();
displayResultList();
}
private void displayResultList() {
list.setAdapter(new HistoryArrayAdapter(this.getBaseContext(), results));
}
private ArrayList<Result> openAndQueryDatabase() {
DatabaseHandler dbh = new DatabaseHandler(getBaseContext());
ArrayList<Result> toReturn = dbh.getAllResults();
return toReturn;
}//open and query database
public int getCount() {
return null == results ? 0 : results.size();
}
}
这是我的ListAdapterClass
public class HistoryArrayAdapter extends ArrayAdapter<Result> {
private final Context context;
private final ArrayList<Result> values;
private int layoutId;
public HistoryArrayAdapter(Context context, ArrayList<Result> results) {
super(context, R.layout.history_row, results);
this.context = context;
this.layoutId = R.layout.history_row;
this.values = results;
}
@Override
public int getCount() {
return values.size();// more than zero
}
@Override
public Result getItem(int position) {
return values.get(position);// may be in your case
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
// inflate the layout
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(layoutId, parent, false);
}// if convertView == null
//set up our views
TextView uploadSpeed = (TextView) convertView.findViewById(R.id.upload_speed_history);
TextView downloadSpeed = (TextView) convertView.findViewById(R.id.download_speed_history);
TextView pingTime = (TextView) convertView.findViewById(R.id.ping_history_text);
TextView networkName = (TextView) convertView.findViewById(R.id.history_network_name);
TextView dateTime = (TextView) convertView.findViewById(R.id.history_dateTime);
ImageView imageView = (ImageView) convertView.findViewById(R.id.history_connection);
//set up our values
Result row = values.get(position);
uploadSpeed.setText(row.getUpload());
downloadSpeed.setText(row.getDownload());
pingTime.setText(row.getPingTime());
networkName.setText(row.getNetworkName());
dateTime.setText(row.getDateTime());
if(row.getNetworkType() == "NETWORK_WIFI")
imageView.setImageResource(R.drawable.ico_wifi);
else
imageView.setImageResource(R.drawable.ico_cell);
return convertView;
}//getView
}
这是ListView的.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#b5b5b5"
android:dividerHeight="1dp" />
</LinearLayout>
这是行布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/realtive_line_1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/history_network_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
<ImageView
android:id="@+id/history_connection"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft = "20dip"
android:layout_marginTop = "5dip"
android:src="@drawable/ico_wifi" />
<TextView
android:id="@+id/history_dateTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="65dp"
android:layout_below="@+id/network_name"
android:text="2013-08-15 12:35:00 PM"
android:textSize="10sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/realtive_line_1"
>
<ImageView
android:id="@+id/imageView2"
android:layout_width="15dip"
android:layout_height="15dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="23dp"
android:layout_marginTop = "15dip"
android:src="@drawable/ico_ping" />
<TextView
android:id="@+id/ping_history_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@+id/imageView2"
android:text="30 ms"
android:textSize = "15sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/results_icon_up"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/ico_up"
android:layout_marginRight = "40dip" />
<ImageView
android:id="@+id/results_icon_down"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_alignParentRight="true"
android:layout_marginTop="5dip"
android:layout_below ="@+id/results_icon_up"
android:src="@drawable/ico_down"
android:layout_marginRight = "40dip" />
<TextView
android:id="@+id/upload_speed_history"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/results_icon_down"
android:layout_marginRight="15dp"
android:layout_toLeftOf="@+id/results_icon_up"
android:text="4.26 Mbps"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/download_speed_history"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/upload_speed_history"
android:layout_alignTop="@+id/results_icon_down"
android:text="24.56 Mbps"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
<ImageView
android:id="@+id/imageView3"
android:layout_width="20dip"
android:layout_height="20dip"
android:layout_alignParentRight ="true"
android:layout_centerVertical="true"
android:src="@drawable/arrow" />
</RelativeLayout>
我知道的事情......我确实有结果。我可以在调试器中看到它并且它们填充得很好。我知道我有一些看似不必要的方法,有一些工作要做,以确保应用程序在填充列表之前不在进程的中间。
答案 0 :(得分:2)
更改您的onCreate()
,如下所示
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sp_history);
//set up list
list = getListView();
}
答案 1 :(得分:0)
如果您已声明正在使用异步调用来获取数据库结果...即使在调试时您获得结果,但实际运行时可能会在设置适配器后获得结果在以下行中,适配器中results
的长度为0,因此您的getView()
没有收到回调。
可以有一个自定义Listener
,它将在获取结果时获得回调,然后您可以将适配器设置为列表视图,或者另一个解决方案可能是保留适配器的实例然后添加使用Adapter.add()
方法将获取的结果发送到适配器,然后调用Adapter.notifyDataSetChanged()
方法。
修改: - 当您使用ListActivity
时,无需创建新的ListView
实例。只需调用getListView()
方法即可获得上述回答者建议的ListView
实例。