如何在Action Bar Android中引用列表视图

时间:2014-02-25 08:34:33

标签: java android android-listview android-actionbar

我按照教程学习了如何在Action Bar中显示自定义列表。 问题是,现在我想以编程方式突出显示(或选择)列表中的特定项目,例如当用户退出应用程序时,应用程序应该记下用户在列表中选择的最后一项是什么(我可以使用共享首选项)然后当用户再次打开我的应用程序时,列表视图应该将自身恢复到最后选择的项目。

我不知道如何在自定义列表视图中以编程方式选择项目。也许我必须首先考虑它。但我也不知道。

这是我的代码: 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;
    }

}

1 个答案:

答案 0 :(得分:0)

尝试使用getSupportActionBar()获取操作栏视图