可穿戴ListView项目的着色

时间:2015-06-13 11:41:01

标签: java android listview android-listview

有没有办法在Android Wear上更改列表项的文本颜色?我之前只看过这个,因此为手机做过这个,但不认为相同的代码可以用于佩戴。

活动代码

public class MainActivity  extends Activity implements WearableListView.ClickListener{

    private WearableListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyColoringAdapter adapter = new MyColoringAdapter(this,listItems);

        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mListView = (WearableListView) stub.findViewById(R.id.listView1);
                mListView.setAdapter(new MyAdapter(MainActivity.this));
                mListView.setClickListener(MainActivity.this);

            }
        });
    }

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

        public MyColoringAdapter(Context context, String[] values) {
            super(context, R.layout.row_simple_item_layout, 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.row_simple_item_layout, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.list_item);
            // 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.red; break;
                case 1:
                    textColorId = R.color.yellow; break;
                case 2:
                    textColorId = R.color.green; break;
            }
            textView.setTextColor(getResources().getColor(textColorId));
            return rowView;
        }
    }

    private static ArrayList<String> listItems;
    static {
        listItems = new ArrayList<String>();
        listItems.add("Item 1");
        listItems.add("Item 2");
        listItems.add("Item 3");
    }

    @Override
    public void onClick(WearableListView.ViewHolder viewHolder) {

    }

    @Override
    public void onTopEmptyRegionClick() {

    }

    private class MyAdapter extends WearableListView.Adapter {
        private final LayoutInflater mInflater;

        private MyAdapter(Context context) {
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public WearableListView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new WearableListView.ViewHolder(
                    mInflater.inflate(R.layout.row_simple_item_layout, null));
        }

        @Override
        public void onBindViewHolder(WearableListView.ViewHolder holder, int position) {
            TextView view = (TextView) holder.itemView.findViewById(R.id.list_item);
            view.setText(listItems.get(position).toString());
            holder.itemView.setTag(position);
        }

        @Override
        public int getItemCount() {
            return listItems.size();
        }
    }
}

着色适配器代码

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_item);
            textView.setText(values[position]);
            int textColorId = R.color.white; // Default color
            switch (position) {
                case 0:
                    textColorId = R.color.red; break;
                case 1:
                    textColorId = R.color.yellow; break;
                case 2:
                    textColorId = R.color.green; break;
            }
            textView.setTextColor(getResources().getColor(textColorId));
            return rowView;
        }
    }

错误

  类MainActivity.MyColoringAdapter中的

构造函数MyColoringAdapter不能应用于给定类型;   required:Context,String []   发现:MainActivity,ArrayList   原因:实际参数ArrayList无法通过方法调用转换

转换为String []

enter image description here

警告

MyColoringAdapter adapter = new MyColoringAdapter(this,listItems);

  

永远不会使用变量'适配器'

1 个答案:

答案 0 :(得分:1)

   MainActivity.MyColoringAdapter类中的构造函数MyColoringAdapter   不能适用于给定的类型; required:Context,String []找到:   MainActivity,ArrayList原因:实际参数ArrayList不能   通过方法调用转换

转换为String []

问题是ArrayAdpater的构造函数。它需要String[],但在您的活动中,您传递的是ArrayList<String>

更改

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;
        }

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

    public MyColoringAdapter(Context context, ArrayList<String> values) {
        super(context, R.layout.list_item, values);
        this.context = context;
        this.values = values;
    }

而不是textView.setText(values[position]);,您将拥有textView.setText(values.get(position));

作为副节点,您应该回收convertView

编辑:我注意到你有两个适配器。但是你需要一个适配器,MyAdapter。摆脱MyColoringAdapter。如果要更改文本颜色,请更新onBindView

 @Override
 public void onBindViewHolder(WearableListView.ViewHolder holder, int position) {
            TextView view = (TextView)   holder.itemView.findViewById(R.id.list_item);
            view.setText(listItems.get(position).toString());
            holder.itemView.setTag(position);
            int textColorId = R.color.white; // Default color
            switch (position) {
                case 0:
                    textColorId = R.color.red; break;
                case 1:
                    textColorId = R.color.yellow; break;
                case 2:
                    textColorId = R.color.green; break;
            }
            view.setTextColor(getResources().getColor(textColorId));
  }