我使用Master Detail流编写RSS阅读器,左侧是列表视图,右侧是详细视图。
创建屏幕时,右侧为空,左侧没有选择项目。如何默认加载第一个项目?
我发现此帖与我的问题类似:Select the first item by default in a Master Detail flow
我的ListFragment中的代码如下所示:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Set a listener to be invoked when the list should be refreshed.
listView = (PullToRefreshListView) getListView();
listView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
//refresh list
refresh_list();
}
});
listView.setVerticalFadingEdgeEnabled(true);
// Set custom list adapter to the ListView
adapter = new CustomListAdapter(getActivity(), feed);
listView.setAdapter(adapter);
//create button, on click go to website
final Button btnAddMore = new Button(getActivity());
btnAddMore.setText(R.string.button_text_end_list);
btnAddMore.setBackgroundColor(getResources().getColor(R.color.white));
btnAddMore.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// go to website on click
String url = "http://www.test.de";
Intent web = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(web);
}
});
//add button at the end of the listview
listView.addFooterView(btnAddMore);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Restore the previously serialized activated item position.
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState
.getInt(STATE_ACTIVATED_POSITION));
}
//select the first item in the listview by default
listView.requestFocusFromTouch();
listView.setSelection(0);
listView.performItemClick(listView
.getAdapter().getView(0, view, null), 0, 0);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Hallo in ItemListFragment");
// show ActionBar
setHasOptionsMenu(true);
// get reference to activity
myApp = getActivity().getApplication();
// check if intent from ItemListActivity is null
Bundle be = getActivity().getIntent().getExtras();
if (be == null) {
// if null read local feed
feed = ReadFeed(fileName);
Log.d(TAG, "Lese Feed lokal :" + feed);
} else {
// else get extras from the intent
feed = (RSSFeed) getActivity().getIntent().getExtras().get("feed");
Log.d(TAG,
"Intent von ItemListActivity an ItemListFragment vorhanden");
}
}
但是我收到了这个错误:
01-19 06:39:14.723: E/AndroidRuntime(1921): Caused by: java.lang.NullPointerException
01-19 06:39:14.723: E/AndroidRuntime(1921): at ItemListFragment.onViewCreated(ItemListFragment.java:185)
在
行listView.requestFocusFromTouch();
请帮助我!
CustomListAdapter
public class CustomListAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
public RSSFeed _feed;
public Date pDate;
public CustomListAdapter(Activity activity, RSSFeed feed) {
_feed = feed;
layoutInflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(activity.getApplicationContext());
}
public void setNewFeed(RSSFeed feed) {
// set new Feed list, after refresh
_feed = feed;
}
@Override
public int getCount() {
// Set the total list item count
return _feed.getItemCount();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the item layout and set the views
View listItem = convertView;
int pos = position;
if (listItem == null) {
listItem = layoutInflater.inflate(R.layout.list_item, null);
}
// Initialize the views in the layout
ImageView iv = (ImageView) listItem.findViewById(R.id.thumb);
TextView tvTitle = (TextView) listItem.findViewById(R.id.title);
TextView tvDate = (TextView) listItem.findViewById(R.id.date);
TextView comment_bubble = (TextView) listItem.findViewById(R.id.comment_bubble);
// Set the views in the layout
imageLoader.DisplayImage(_feed.getItem(pos).getImage(), iv);
tvTitle.setText(_feed.getItem(pos).getTitle());
// calculate the time difference to the actual system time
String pubDate = _feed.getItem(pos).getDate();
SimpleDateFormat df = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss",Locale.ENGLISH);
try {
try {
pDate = df.parse(pubDate);
} catch (java.text.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pubDate = "Vor "
+ DateUtils.getDateDifference(pDate);
} catch (ParseException e) {
Log.e("DATE PARSING", "Error parsing date..");
}
//set time difference
tvDate.setText(pubDate);
//set comment in bubble
comment_bubble.setText(_feed.getItem(pos).getComment());
return listItem;
}
}
答案 0 :(得分:1)
Fragment
,onViewCreated()
将在onActivityCreated()
之前调用,此处您正在ListView
初始化onActivityCreated()
并使用onViewCreated()
导致NullPointerException
(因为ListView
仍未初始化)。相反,您可以在onViewCreated()
上初始化它并在onActivityCreated()
...
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = (PullToRefreshListView) getListView();
listView.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
//refresh list
refresh_list();
}
});
listView.setVerticalFadingEdgeEnabled(true);
// Set custom list adapter to the ListView
adapter = new CustomListAdapter(getActivity(), feed);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// handle List item click here...
}
});
ListAdapter adapter = listView.getAdapter();
listView.performItemClick(listView.getChildAt(0), 0, adapter.getItemId(0)); // this will call OnItemClickListener
......
}
答案 1 :(得分:1)
我在ListFragment中解决了这个问题:
@Override
public void onStart() {
super.onStart();
//set first item activated by default
onListItemClick(listView, getView(), 1, 0);
}
/**
* Turns on activate-on-click mode. When this mode is on, list items will be
* given the 'activated' state when touched.
*/
public void setActivateOnItemClick(boolean activateOnItemClick) {
// When setting CHOICE_MODE_SINGLE, ListView will automatically
// give items the 'activated' state when touched.
listView.setChoiceMode(activateOnItemClick ? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}