Background von ListActivity

S

soma-web

App-Anbieter (kostenpfl.)
19
Hi Guys, hat jemand ne Idee wie ich den Hintergrund einer Listactivity ohne XML File ändere?

Code:
public class DrinkList extends ListActivity {

	NewDBHandler dbHandler;
	Map<String, Drink> list;

	/** Called when the activity is first created. */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		dbHandler = new NewDBHandler(this);
		list = new HashMap<String, Drink>();

		list = dbHandler.getAllDrinkNames();

		String[] names = new String[list.size() + 1];
		int i = 1;
		names[0] = "Neues Getränk";
		for (Drink d : list.values()) {
			names[i] = d.getName();
			i++;
		}

		this.setListAdapter(new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, names));
		
	}

Will wirklich nur ein anderen Hintergrund und evtl dem ersten Element in meiner ListActivity noch eine MarginTop von 15sp geben.

Danke und Grüße
Soma
 
Zuletzt bearbeitet:
getListView().setBackgroundXXX
 
  • Danke
Reaktionen: soma-web
Danke dir!!
Funktioniert soweit aber jetzt sind meine Einträge in der ListView noch mit schwarzem hintergrund. Sieht iwie kacke aus
 
naja dein simple_list_item_1 wird wohl einen schwarzen hintergrund haben
 
Nur für die Nachwelt, ich habe eigtl zwei klassen erstellt eine Activity die mit ein Layout aufruft und einen Adapter, der die Listview mit meinen Parametern füttert:

Layout:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="match_parent"
	android:layout_height="match_parent"                   android:background="@drawable/zettel">
    <TextView android:id="@+id/tv_newDrink" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_width="wrap_content" android:text="TextView" android:layout_marginTop="100sp" android:layout_marginLeft="15sp"></TextView>
	<ListView android:layout_height="wrap_content" android:id="@+id/lv_drink"
		android:layout_width="match_parent" 
		android:background="@null" android:paddingLeft="15sp"
		 android:listSelector="@drawable/listselector" android:scrollingCache="false" android:cacheColorHint="#00000000"></ListView>


</LinearLayout>

Dann die Helper Klasse für die ListView:
Ich habe sie geschrieben, damit ich mein eigenes Font in der ListView verwenden kann.

Code:
public class MyAdapter extends BaseAdapter {

	private List<String>        objects; // obviously don't use object, use whatever you really want
	private final Context   context;
	Typeface tf;
	int kulli;
	static int size = 30;
	
	public MyAdapter(Context context, List<String> objects, Typeface tf, int color) {
	    this.context = context;
	    this.objects = objects;
	    this.tf = tf;
	    this.kulli = color;
	}

	public int getCount() {
	    return objects.size();
	}

	public String getItem(int position) {
	    return objects.get(position);
	}

	public long getItemId(int position) {
	    return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {

	    String obj = objects.get(position);

	    TextView tv = new TextView(context);
	    tv.setText(obj.toString());
	    tv.setTypeface(tf);
	    tv.setTextColor(kulli);
	    tv.setTextSize(size);
	   
	    // use whatever method you want for the label
	    // set whatever typeface you want here as well
	    return tv;
	}
}

Und noch die onCreate der eigtl Activity:
Code:
public class NewDrinkList extends Activity {

	ListView listView;
	NewDBHandler dbHandler;
	Map<String, Drink> list;
	TextView neu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.drink_list);
		
		Typeface tf = Typeface.createFromAsset(this.getAssets(),
		"fonts/Handwriting.ttf");
		int kulli = this.getResources().getColor(R.color.kulli);
		
		dbHandler = new NewDBHandler(this);
		list = new HashMap<String, Drink>();
		list = dbHandler.getAllDrinkNames();
		neu = (TextView)findViewById(R.id.tv_newDrink);
		neu.setTypeface(tf);
		neu.setTextColor(kulli);
		neu.setText("Neues Getränk");
		neu.setTextSize(30);
		neu.setOnClickListener(listener);
		listView = (ListView) findViewById(R.id.lv_drink);
		
		List<String> stringlist = new ArrayList<String>();

		for (Drink d : list.values()) {
			stringlist.add(d.getName());
		}

		
		
		listView.setDivider(null);
		listView.setDividerHeight(0);
		// listView.setSelector(R.drawable.listselector);
		listView.setAdapter(new MyAdapter(this, stringlist, tf, kulli));

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> adapter, View view,
					int position, long arg) {
				// Object listItem = ((AdapterView<ListAdapter>)
				// list).getItemAtPosition(position);
				Object o = adapter.getItemAtPosition(position);
				String keyword = o.toString();
				final Intent intent = new Intent();
				if (list.containsKey(keyword)) {
					Drink d = list.get(keyword);			
					intent.putExtra("Name", d.getName());
					intent.putExtra("Alkohol", d.getAlk());
				}
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
		// listView.setOnItemClickListener(new
		// android.widget.AdapterView.OnItemClickListener());
	}

Der Bugfix mit dem XML Attribut des ListViews "android:scrollingCache="false" android:cacheColorHint="#00000000"" verhindert, dass der Hintergrund während dem Scrollen schwarz wird!
DANKE swordi!!!
Grüße Soma
 
Zuletzt bearbeitet:

Ähnliche Themen

S
Antworten
17
Aufrufe
550
jogimuc
J
5
Antworten
22
Aufrufe
1.414
590239
5
M
Antworten
4
Aufrufe
1.168
swa00
swa00
Zurück
Oben Unten