Java 可穿戴ListView项目的着色
有没有办法改变Android Wear上列表项的文本颜色?我以前只见过这样的代码,因此我对手机也这么做了,但我不认为相同的代码可以用于佩戴 活动代码Java 可穿戴ListView项目的着色,java,android,listview,android-listview,Java,Android,Listview,Android Listview,有没有办法改变Android Wear上列表项的文本颜色?我以前只见过这样的代码,因此我对手机也这么做了,但我不认为相同的代码可以用于佩戴 活动代码 public class MainActivity extends Activity implements WearableListView.ClickListener{ private WearableListView mListView; @Override protected void onCreate(Bund
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扩展活动实现WearableListView.ClickListener{
私人可穿戴列表视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mycloringadapter=新的mycloringadapter(此,列表项);
最终的WatchViewStub存根=(WatchViewStub)findViewById(R.id.watch\u view\u存根);
stub.setOnlayOutingFlatedListener(新的WatchViewStub.OnlayOutingFlatedListener(){
@凌驾
仅限公共空间布局平坦(WatchViewStub){
mListView=(WearableListView)stub.findViewById(R.id.listView1);
setAdapter(新的MyAdapter(MainActivity.this));
mListView.setClickListener(MainActivity.this);
}
});
}
私有类MyColoringAdapter扩展了ArrayAdapter{
私人最终语境;
私有最终字符串[]值;
公共MyColoringAdapter(上下文,字符串[]值){
super(上下文、R.layout.row\u简单项\u布局、值);
this.context=上下文;
这个值=值;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(R.layout.row\u simple\u item\u layout,parent,false);
TextView TextView=(TextView)rowView.findViewById(R.id.list_项);
//设置文本
setText(值[位置]);
//根据位置设置颜色
int textColorId=R.color.white;//默认颜色
开关(位置){
案例0:
textColorId=R.color.red;中断;
案例1:
textColorId=R.color.yellow;中断;
案例2:
textColorId=R.color.green;中断;
}
setTextColor(getResources().getColor(textColorId));
返回行视图;
}
}
私有静态ArrayList列表项;
静止的{
listItems=new ArrayList();
列表项目。添加(“项目1”);
列表项目。添加(“项目2”);
列表项目。添加(“项目3”);
}
@凌驾
public void onClick(WearableListView.ViewHolder ViewHolder){
}
@凌驾
公共无效OnToptyRegionClick(){
}
私有类MyAdapter扩展了WearableListView.Adapter{
私人最终布局平面图;
私有MyAdapter(上下文){
mInflater=LayoutInflater.from(上下文);
}
@凌驾
public WearableListView.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
返回新的WearableListView.ViewHolder(
mInflater.充气(R.layout.row_simple_item_layout,null));
}
@凌驾
BindViewHolder上的公共无效(WearableListView.ViewHolder,int位置){
TextView视图=(TextView)holder.itemView.findViewById(R.id.list\u项);
view.setText(listItems.get(position.toString());
holder.itemView.setTag(位置);
}
@凌驾
public int getItemCount(){
返回listItems.size();
}
}
}
为适配器代码着色
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;
}
}
私有类MyColoringAdapter扩展了ArrayAdapter{
私人最终语境;
私有最终字符串[]值;
公共MyColoringAdapter(上下文,字符串[]值){
super(上下文、R.layout.list_项、值);
this.context=上下文;
这个值=值;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutFlater充气器=(LayoutFlater)上下文
.getSystemService(上下文布局\充气机\服务);
视图行视图=充气机。充气(R.layout.list\u项,父项,false);
TextView TextView=(TextView)rowView.findViewById(R.id.list_项);
setText(值[位置]);
int textColorId=R.color.white;//默认颜色
开关(位置){
案例0:
textColorId=R.color.red;中断;
案例1:
textColorId=R.color.yellow;中断;
案例2:
textColorId=R.color.green;中断;
}
setTextColor(getResources().getColor(textColorId));
返回行视图;
}
}
错误
类MainActivity.MyColoringAdapter中的构造函数MyColoringAdapter不能应用于给定类型;
必需:上下文,字符串[]
找到:MainActivity,ArrayList
原因:无法通过方法调用转换将实际参数ArrayList转换为字符串[]
警告
对于mycloringadapter=newmycloringadapter(此为列表项)代码>
从未使用变量“adapter”
类MainActivity.mycloringAdapter中的构造函数mycloringAdapter
无法应用于给定类型;必需:上下文,找到字符串[]:
MainActivity,ArrayList原因:实际参数ArrayList不能为空
通过方法调用转换转换为字符串[]
问题是数组数据的构造函数。它需要一个字符串[]
,但在您的活动中,您传递的是一个数组列表
改变
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;
}
你需要一个b