# Frage zu ListViews



## lucien (30. Jan 2015)

Hi ich hab eine Frage zu List Views. Ich versteh das nämlich nicht so ganz, auch wenn ich mir viele Tutorials ansehe.
Also ich hab erst die XML-Klasse der mainactivity

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoText="true"
        android:text="Name" />
    <EditText
        android:id="@+id/nummer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wohnort"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="Wohnort" />
    <EditText
        android:id="@+id/wohnort"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:text="Nummer"
        android:layout_toLeftOf="@+id/scrollView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />



    <Button
        android:id="@+id/kontakt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Kontakt hinufügen"
        android:layout_below="@+id/nummer"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/listView"
        android:layout_below="@+id/kontakt"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />


</RelativeLayout>
```

Dann hab ich die Klasse für list-items


```
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@+id/textView3" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>
```

Wie krieg ich es jetzt hin, dass ein List-Item so aufgebaut ist, wie ich es in der Klasse eingestellt habe?
Und kennt jemand ein gutes, deutschsprachiges Tutorial für den Adapter?


----------



## lucien (3. Feb 2015)

Ich hab jetzt ein einfaches ListView erstellt. Diese kann ich jedoch in einem zweiten Tab nicht öffnen.
Die MainActivity

```
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MyActivity">

    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoText="true"
        android:hint="Name" />
    <EditText
        android:id="@+id/nummer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/wohnort"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:hint="Wohnort" />
    <EditText
        android:id="@+id/wohnort"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/name"
        android:hint="Nummer"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />



    <Button
        android:id="@+id/kontakt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Kontakt hinzufügen"
        android:layout_below="@+id/nummer"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/listView"
        android:layout_below="@+id/kontakt"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Suche"
        android:id="@+id/suche"
        android:layout_above="@id/listView"
        android:layout_toRightOf="@id/kontakt"
        android:layout_toEndOf="@id/kontakt"
        android:layout_alignRight="@id/listView"
        android:layout_alignEnd="@id/listView" />


</RelativeLayout>
```
Die SecondActivity

```
<?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:weightSum="1">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:hint="Suche" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:text="Suche" />

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ergebnisse" />

</LinearLayout>
```
Das list Item:

```
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/text_name"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/text_nummer"
        android:layout_below="@id/text_name"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/text_ort"
        android:layout_below="@id/text_nummer"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>
```
Der Adapter:

```
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.LinkedList;
import java.util.List;


public class Adapter2 extends BaseAdapter {
    List<Person> data;
    Context bla;
    LayoutInflater inflater;
    public Adapter2(Context c, List<Person> liste){
        data = liste;
        bla=c;
        inflater=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {


        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView==null){
            convertView=inflater.inflate(R.layout.list_item,parent,false);
        }

        Person person = data.get(position);

        TextView name=(TextView)convertView.findViewById(R.id.text_name);
        TextView nummer=(TextView)convertView.findViewById(R.id.text_nummer);
        TextView ort=(TextView)convertView.findViewById(R.id.text_ort);


        name.setText(person.getName());
        nummer.setText(person.getNummer());
        ort.setText(person.getWohnort());
        return convertView;
    }
}
```
Die Main-Activity:

```
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;


public class MyActivity extends Activity implements View.OnClickListener {
    int x=0;
    List<Person> personenListe;
    Person test=new Person("Name","Ort","Nummer");
    Button kontakt;
    Button suche;
    String name;
    String nummer;
    String ort;

    Adapter adapter;
   // Button suche=(Button)findViewById(R.id.suche);
    //dummy.setAdapter(dummy);
    //dummy.setOnClickListener(new AdapterView.OnItemClickListener);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        kontakt = (Button)findViewById(R.id.kontakt);
        kontakt.setOnClickListener(this);
        suche=(Button)findViewById(R.id.suche);
       suche.setOnClickListener(this);
        personenListe = new ArrayList<Person>();
        ListView anzeige = (ListView)findViewById(R.id.listView);
        adapter = new Adapter(this, personenListe);
        anzeige.setAdapter(adapter);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void onClick(View v) {
if(v==kontakt) {
    EditText namef = (EditText) findViewById(R.id.name);
    EditText nummerf = (EditText) findViewById(R.id.nummer);
    EditText wohnortf = (EditText) findViewById(R.id.wohnort);

    name = namef.getText().toString();
    nummer = nummerf.getText().toString();
    ort = wohnortf.getText().toString();

    Person person = new Person(name, nummer, ort);
    personenListe.add(person);

    adapter.notifyDataSetChanged();
}
        if(v==suche) {

            Intent i = new Intent( this, ActivityTwo.class );
            i.putExtra("activitytwo", R.layout.activitytwo);
            startActivity(i);
        }



    }

    public List getData(){
        return personenListe;

    }

 }
```

Die second-Activity:

```
public class ActivityTwo extends Activity{
   List<Person> personList;
    MyActivity data=new MyActivity();
    Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activitytwo);
        personList=data.getData();
        ListView anzeige = (ListView)findViewById(R.id.ergebnisse);
        adapter = new Adapter(this, personList);
        anzeige.setAdapter(adapter);
    }

}
```

Warum funktioniert der Adapter in der ersten aber nicht in der zweiten Methode?


----------

