2014-02-25 40 views
0

我按照教程學習瞭如何在操作欄中正確顯示自定義列表。 問題是,現在我想要突出顯示(或選擇)列表中的特定項目,例如當用戶退出應用程序時,應該記下用戶在列表中選擇的最後一個項目是什麼(我可以做到這一點使用共享偏好),然後當用戶再次打開我的應用時,列表視圖應該將自己恢復到最後選擇的項目。如何在操作欄中引用列表視圖Android

我不知道如何在自定義列表視圖中以編程方式選擇一個項目。也許我必須先反悔。但我也不知道。

這裏是我的代碼: bus_names.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="5dp" > 

    <ImageView 
     android:id="@+id/busIcon" 
     android:layout_width="30dp" 
     android:layout_height="30dp" 
     android:layout_gravity="center_vertical" 
     android:layout_marginRight="5dp" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/busTitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:text="Alaknanda" 
     android:textColor="@color/white" 
     android:textSize="18sp" 
     android:textStyle="bold" /> 

</LinearLayout> 

SpinnerNavItem.java:

package com.hpubts50.hpubustracker.model; 

public class SpinnerNavItem { 

    private String title; 
    private int icon; 

    public SpinnerNavItem(String title, int icon){ 
     this.title = title; 
     this.icon = icon; 
    } 

    public String getTitle(){ 
     return this.title; 
    } 

    public int getIcon(){ 
     return this.icon; 
    } 

} 

BusNavigation.java:

package com.hpubts50.hpubustracker.adapter; 

import java.util.ArrayList; 

import android.content.Context; 
import android.support.v7.app.ActionBarActivity; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 

import com.hpubts50.hpubustracker.R; 
import com.hpubts50.hpubustracker.model.SpinnerNavItem; 

public class BusNavigation extends BaseAdapter { 
    private ImageView busIcon; 
    private TextView busTitle; 
    private ArrayList<SpinnerNavItem> spinnerNavItem; 
    private Context context; 

    public BusNavigation(Context context, ArrayList<SpinnerNavItem> spinnerNavItem) { 
     this.spinnerNavItem = spinnerNavItem; 
     this.context = context; 
    } 

    @Override 
    public int getCount() { 
     return spinnerNavItem.size(); 
    } 

    @Override 
    public Object getItem(int index) { 
     return spinnerNavItem.get(index); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater mInflater = (LayoutInflater) context.getSystemService(ActionBarActivity.LAYOUT_INFLATER_SERVICE); 
      convertView = mInflater.inflate(R.layout.bus_names, parent, false); 
     } 

     busIcon = (ImageView) convertView.findViewById(R.id.busIcon); 
     busTitle = (TextView) convertView.findViewById(R.id.busTitle); 

     busIcon.setImageResource(spinnerNavItem.get(position).getIcon()); 
     busIcon.setVisibility(View.GONE); 
     busTitle.setText(spinnerNavItem.get(position).getTitle()); 
     return convertView; 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater mInflater = (LayoutInflater) context.getSystemService(ActionBarActivity.LAYOUT_INFLATER_SERVICE); 
      convertView = mInflater.inflate(R.layout.bus_names, parent, false); 
     } 

     busIcon = (ImageView) convertView.findViewById(R.id.busIcon); 
     busTitle = (TextView) convertView.findViewById(R.id.busTitle); 

     busIcon.setImageResource(spinnerNavItem.get(position).getIcon()); 
     busTitle.setText(spinnerNavItem.get(position).getTitle()); 
     return convertView; 
    } 

} 

回答

0

嘗試使用getSupportActionBar()獲取操作欄視圖

相關問題