我有一个列表视图,显示整个星期的天气预报。我还在这个ListView上设置了一个适配器mForecastAdapter。但是当我预览fragment_main.xml文件时,我得到的预览就是这个
这是我在MainActivity.java中的代码
package com.example.android.sunshine.app;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String[] forecastArray = {
"Today - Sunny - 88/64",
"Tomorrow - Rainy - 73/54",
"Wednesday - Snow - 63/54",
"Thursday - Rainy - 83/54",
"Friday - Rainy - 73/58",
"Saturday - Gloomy - 64/54",
"Sunday - Rainy - 73/64"
};
ArrayAdapter<String> mForecastAdapter;
List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));
mForecastAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast);
ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecastAdapter);
return rootView;
}
}
} ArrayAdapter有什么问题?如果这有任何不同,我正在使用Android Studio。
答案 0 :(得分:2)
在布局预览中,默认情况下,ListView
将显示与上面类似的列表,方法是显示默认的ListView,其中包含&#34;项目1&#34;,&#34;项目2&#34;,&#34;项目3&#34;等...和&#34;子项目1&#34;,&#34;子项目2&#34;,&#34;子项目3&# 34;等...
在您在模拟器中运行应用程序或在实际设备上进行测试之前,您将无法看到实际的ListView(假设您已正确编码)。