我一直試圖在數組列表中添加一個顏色參數,我只是無法做到這一點,我花了無數小時試圖做它,甚至搜索了一個在這裏回答。更改數組列表中的對象的文本顏色
這是我的代碼:
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;
}
}
}
我想什麼爲01添加顏色所以不是
brands.add(new Brands("KTM"));
我也會有這樣的一行代碼:
brands.add(new Brands("KTM", Orange));
和文字顏色將相應更改爲橙色。
非常感謝各位的幫助。
爲什麼你的'品牌'類擴展'AppCompatActivity',這意味着它是活動。而我所理解的只是存儲品牌名稱,所以它被稱爲pojo類 – miljon
謝謝你讓我知道,我刪除它,基本上我試圖做的是在主屏幕上列出一個列表,並在每個列表項上單擊它將移動到正確的頁面,即時嘗試使每個列表項具有代表品牌的顏色。 – Skity