列表片段中的项目不会改变颜色

时间:2014-12-25 15:50:19

标签: java android android-studio

在Android Studio 1.0.2中,我试图获取列表片段中的项目以更改颜色,但存在3个错误,我不知道如何解决它们。所有帮助将受到高度赞赏。屏幕截图和代码如下。

error screenshot

package com.apptacularapps.exitsexpertlondonlite;

import android.app.ListFragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class LinesListFragment extends ListFragment {

    ListView linechooserList;

    String[] line = {"Bakerloo line",
            "Central line",
            "Circle line",
            "District line",
            "Hammersmith & City line",
            "Jubilee line",
            "Metropolitan line",
            "Northern line",
            "Piccadilly line",
            "Victoria line",
            "Waterloo & City line",
            "Docklands Light Railway",
            "London Overground",
            "Tramlink"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lineslistfragment);


        linechooserList = (ListView)findViewById(R.id.lines_listView);
        MyColoringAdapter adapter = new MyColoringAdapter(this,line);
        linechooserList.setAdapter(adapter);

        linechooserList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                //When clicked, go to specific activity

            }
        });
    }

    private class MyColoringAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] values;

        public MyColoringAdapter(Context context, String[] values) {
            super(context, R.layout.list_item, values);
            this.context = context;
            this.values = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.list_item, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.list_textview);
            // Set text
            textView.setText(values[position]);
            // Set color depending on position
            int textColorId = R.color.white; // Default color
            switch (position) {
                case 0: textColorId = R.color.bakerloo; break;
                case 1: textColorId = R.color.central; break;
                case 2: textColorId = R.color.circle; break;
                case 3: textColorId = R.color.district; break;
                case 4: textColorId = R.color.hc; break;
                case 5: textColorId = R.color.jubilee; break;
                case 6: textColorId = R.color.metropolitan; break;
                case 7: textColorId = R.color.white; break;
                case 8: textColorId = R.color.piccadilly; break;
                case 9: textColorId = R.color.victoria; break;
                case 10: textColorId = R.color.wc; break;
                case 11: textColorId = R.color.dlr; break;
                case 12: textColorId = R.color.overground; break;
                case 13: textColorId = R.color.tramlink; break;
            }
            textView.setTextColor(getResources().getColor(textColorId));
            return rowView;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.lineslistfragment, container, false);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    }
}

红色下划线错误 enter image description here

1 个答案:

答案 0 :(得分:1)

您正在使用片段,因此您应该在 onCreateView

中夸大您的布局并初始化变量

由于您使用的是 ListFragment ,因此可以将项目设置为listview适配器,如下所示

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
              "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
              "Linux", "OS/2" };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
    android.R.layout.simple_list_item_1, values);

    setListAdapter(adapter);
}

您可以阅读有关ListFragments和列表here的更多信息。