Ich habe versucht, einen Farbparameter in der Array-Liste hinzuzufügen und ich kann es einfach nicht tun, ich habe unzählige Stunden damit verbracht, es zu versuchen und sogar nach einem gesucht antworte hier.Ändern der Objektfarbe in einer Array-Liste
dies ist mein Code:
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;
}
}
}
Was ich versuche, Do fügt dem 01 eine Farbe hinzuso statt
brands.add(new Brands("KTM"));
Ich werde diese Codezeile haben:
brands.add(new Brands("KTM", Orange));
und die Textfarbe wird entsprechend auf die orange Farbe ändern.
Vielen Dank für jede Art von Hilfe.
Warum Ihre 'Brands' Klasse' AppCompatActivity' ausdehnt, was bedeutet, dass es Aktivität ist. Und was ich verstehe, speichern Sie einfach den Namen 'Brand' so dass es eine Pojo Klasse sein sollte – miljon
ich danke Ihnen dafür entfernt es, im Grunde, was ich versuche zu tun, ist eine Liste auf dem Hauptbildschirm und auf jedem Listenelement klicken Sie auf die richtige Seite, ich versuche, jedes Listenelement haben die Farbe, die die Marke darstellt. – Skity