我一直在尝试在阵列列表中添加颜色参数,我似乎无法做到这一点,我花了无数个小时试图去做,甚至在这里寻找答案。 / p>
这是我的代码:
MainActivity.java:
package com.example.ofir.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayList<Brands> brands = new ArrayList<>();
brands.add(new Brands("KTM"));
brands.add(new Brands("BMW"));
brands.add(new Brands("Suzuki"));
brands.add(new Brands("Yamaha"));
brands.add(new Brands("HONDA"));
BrandAdapter itemsAdapter = new BrandAdapter(this, brands);
ListView listView = (ListView) findViewById(R.id.brandlist);
listView.setAdapter(itemsAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
brands.get(position);
Intent intent = new Intent(MainActivity.this, infoPage.class );
startActivity(intent);
}
});
}
}
BrandAdapter.java
package com.example.ofir.myapplication;
import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by Ofir on 21-Mar-17.
*/
public class BrandAdapter extends ArrayAdapter<Brands>{
private int mColorResourceId;
//Resource id for background color of list
public BrandAdapter(Activity context, ArrayList<Brands> brands) {
super(context, 0, brands);
// mColorResourceId = ColorId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// check if the current view is reused else inflate the view
View listItemView = convertView;
if(listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
Brands brand_item = getItem(position);
TextView brandName = (TextView) listItemView.findViewById(R.id.brandtextview);
brandName.setText(brand_item.getBrand());
return listItemView;
}
}
Brands.java
package com.example.ofir.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
/**
* Created by Ofir on 20-Mar-17.
*/
public class Brands extends AppCompatActivity {
private String mBrandName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public Brands(String brandName){
mBrandName = brandName;
}
public String getBrand(){
return mBrandName;
}
}
}
我尝试做的是为Arraylist
添加一种颜色,而不是
brands.add(new Brands("KTM"));
我会有这行代码:
brands.add(new Brands("KTM", Orange));
,文字颜色会相应变为橙色。
感谢先进的任何帮助。
答案 0 :(得分:0)
因此,您希望拥有包含2个值,品牌名称和颜色的Object列表。 如果是这样,你必须在你的品牌类中声明颜色。
public class Brands {
private String mBrandName;
private String colour;
//constructor with 2 arguments, brand name and colour
public Brands(String brandName, String colour){
mBrandName = brandName;
this.colour = colour;
}
public String getBrand(){
return mBrandName;
}
public String getColour(){ //getter of colour
return colour;
}
}
然后你可以在列表中添加你的颜色(作为字符串,所以在引号中):
brands.add(new Brands("KTM", "#FF9933")); //#FF9933 means orange in RGB
如果要显示颜色,则必须更改适配器,此行:
brandName.setText(brand_item.getBrand());
这样的事情:
brandName.setText(brand_item.getBrand());
brandName.setColorText(Color.parseColor(brand_item.getColour()));
有关设置颜色的更多信息,请点击此处:
https://stackoverflow.com/a/4602929/3870382
=========旧文本
但我强烈建议添加将显示颜色的新textView,然后在适配器中查找它(如 brandName
)并将Brand的颜色分配给新创建的textView of color。