【发布时间】:2017-03-22 20:21:29
【问题描述】:
我一直在尝试在数组列表中添加一个颜色参数,但我似乎无法做到,我花了无数个小时尝试做到这一点,甚至在这里寻找答案。
这是我的代码:
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;
}
}
品牌.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));
文本颜色会相应地变为橙色。
感谢任何形式的帮助。
【问题讨论】:
-
为什么你的
Brands类扩展AppCompatActivity这意味着它是活动.. 我理解你只是存储Brand的名称所以它应该是一些 pojo 类 -
谢谢你让我知道,我删除了它,基本上我想做的是在主屏幕上列出一个列表,然后在每个列表项上单击它会移动到正确的页面,我正在尝试使每个列表项都具有代表品牌的颜色。