Custom Adapter erstellen

  • 3 Antworten
  • Letztes Antwortdatum
T

theAydinator

Neues Mitglied
0
Hallo Forum,

vielleicht kann mir beim Programmieren helfen.

Tutorial

Das was ich gesucht habe, nur würde ich es gerne mit eigenem Adapter ersetzen. Ich möchte list_item.xml erstellen und beispielsweise 3 verschiedene werte von "employees" in die textviews zu weisen.

list_item.xml
Code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:paddingLeft="4dp"
    android:paddingRight="15dp"
    android:paddingTop="13dp"
    android:paddingBottom="13dp"
    android:weightSum="1"
    android:background="#FFFFFF" >
    
    <TextView
        android:id="@+id/text1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="left"
        android:layout_weight="0.90"
        android:paddingLeft="10dp"
        android:textColor="#222222"/>
 
    <TextView
        android:id="@+id/text2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="left"
        android:layout_weight="0.90"
        android:paddingLeft="10dp"
        android:textColor="#222222"/>
</LinearLayout>

Ich würde die Zeile in der XMLPullParserActivity
Code:
ArrayAdapter<Employee> adapter =
        new ArrayAdapter<Employee>(this,R.layout.list_item, employees);
mit dem hier ersetzen:
Code:
MyCustomAdapter adapter = new MyCustomAdapter(this, R.layout.list_item, employees);

Wie sollte dann die Datei MyCustomAdapter entsprechend zu diesem Tutorial aussehen ?
Kann mir jemand da helfen bitte? =)

Danke
Aydin
 
Zuletzt bearbeitet:
Habe es endlich geschafft!

Lösung:
Code:
package de.jfd.xmlpullparser;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyCustomAdapter extends BaseAdapter{

	// Declare Variables
    Context context;
    LayoutInflater inflater;
    private List<Employee> employees = null;
    private ArrayList<Employee> arraylist;
 
    public MyCustomAdapter(Context context, List<Employee> employees) {
        this.context = context;
        this.employees = employees;
        inflater = LayoutInflater.from(context);
        this.arraylist = new ArrayList<Employee>();
        this.arraylist.addAll(employees);
    }
 
    public class ViewHolder {
        TextView name;
        TextView email;
    }
 
    @Override
    public int getCount() {
        return employees.size();
    }
 
    @Override
    public Object getItem(int position) {
        return employees.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    public View getView(final int position, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.list_item, null);

            holder.name = (TextView) view.findViewById(R.id.text1);
            holder.email = (TextView) view.findViewById(R.id.text2);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        // Set the results into TextViews
        holder.name.setText(employees.get(position).getName());
        holder.email.setText(employees.get(position).getEmail());
        return view;
    }
}


Der ursprüngliche Beitrag von 13:32 Uhr wurde um 13:34 Uhr ergänzt:

Vacutainer schrieb:

Danke, übrigens!
 
Jetzt habe ich leider einen weiteres Problem entdeckt:
Wenn die Liste so groß ist, dass man auf dem Screen scrollen kann und dies auch tut
stürzt die Applikation dabei

Ich meine das kommt durch den eigenen Adapter, ich kann es mir aber nicht erklären

Weiß jemand vielleicht genauer ?

Danke!
 

Ähnliche Themen

Jennywise
Antworten
2
Aufrufe
662
Jennywise
Jennywise
Zurück
Oben Unten