S
seaside
Neues Mitglied
- 0
Hallo zusammen,
ich bin absoluter Neuling in der Android Programmierung und sitze gerade an einer recht simplen App. Die App soll über eine Liste, eine EditText-Feld und einem Suchen-Button verfügen.
Ein einzelnes Listenelement soll aus zwei Strings (zwei TextViews) bestehen.
Das EditText-Feld und der Button werden angezeigt die Liste jedoch nicht.
Ich weiß leider nicht woran es liegt. Vielleicht findet von euch ja jemand den Fehler
Hier der Code:
activity_main.xml
itemlayout.xml
item.java
MainActivity.java
Vielen Dank schon mal
ich bin absoluter Neuling in der Android Programmierung und sitze gerade an einer recht simplen App. Die App soll über eine Liste, eine EditText-Feld und einem Suchen-Button verfügen.
Ein einzelnes Listenelement soll aus zwei Strings (zwei TextViews) bestehen.
Das EditText-Feld und der Button werden angezeigt die Liste jedoch nicht.
Ich weiß leider nicht woran es liegt. Vielleicht findet von euch ja jemand den Fehler
Hier der Code:
activity_main.xml
Code:
<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"
tools:context=".MainActivity" >
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/suchen" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/suchen" />
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/editText"/>
</RelativeLayout>
itemlayout.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/Grad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/Name" />
</RelativeLayout>
item.java
Code:
public class Item {
public String name, grad;
public Item(String _name, String _grad){
this.name = _name;
this.grad = _grad;
}
}
MainActivity.java
Code:
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
public ArrayList<Item> datensaetze;
Adapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
ListView listView = (ListView) findViewById(R.id.listView);
myAdapter = new Adapter();
listView.setAdapter(myAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void init(){
datensaetze = new ArrayList<Item>();
datensaetze.add(new Item("Aal", "Keine"));
datensaetze.add(new Item("Agar-Agar", "Sehr stark"));
datensaetze.add(new Item("Amaranth", "Keine"));
}
public class Adapter extends BaseAdapter {
LayoutInflater mInflater;
@Override
public int getCount() {
return datensaetze.size();
}
@Override
public Object getItem(int position) {
return datensaetze.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = mInflater.inflate(R.layout.itemlayout, parent, false);
}
TextView nameTextView = (TextView) convertView.findViewById(R.id.Name);
TextView gradTextView = (TextView) convertView.findViewById(R.id.Grad);
Item item = (Item) getItem(position);
nameTextView.setText(item.name);
gradTextView.setText(item.grad);
return convertView;
}
}
}
Vielen Dank schon mal