2014-01-19 21 views
3

我使用左側的列表視圖和右側的詳圖查看主節點流程的RSS閱讀器。Android:在MasterView模板的ListView中默認選擇第一個項目

當屏幕創建時,右側是空的,沒有選擇左側的項目。我怎麼能默認加載第一個項目?

我發現這個職位它類似於我的問題: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; 
    } 
} 

回答

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); 
} 
+0

我可以在默認情況下選擇第一項,但ListView不會將其標記爲已激活!任何線索? – CinCout

1

Fragment S,onViewCreated()將被調用之前onActivityCreated(),在這裏你都在onActivityCreated()初始化ListView,並用它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 
     ...... 
} 
+0

好吧,我改變了它這個樣子,現在我收到'NullPointerException'在\t \t'listView.performItemClick(ListView中 \t \t \t \t .getAdapter()getView(0,視圖,NULL),0,0);' –

+0

@ Mr.Mojo設置適配器的ListView在onViewCreated().. ..看我的編輯 –

+0

謝謝!當然,如果我還沒有設置適配器,應該如何獲得適配器。現在沒有更多的錯誤,但另一個奇怪的事情:第一個項目被選中,但ListView沒有顯示,但只有細節片段。 –

相關問題