This is the code I'm working on, it should simply add an item in a ListView (w/ array) each time the Button is clicked. The text inserted by the user on a EditText should be added to the list each time the Button is clicked.
I find that this line of code:
new Button.OnClickListener()
is seen as an 'anonymous' thing, and it's not run, please help me out ;(
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import java.lang.*;
import java.util.List;
public class MainActivity extends ActionBarActivity {
//Setup Lists
String[] arrayNames;
ListView listNames;
TextView namesText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating and Printing Lists
listNames = (ListView) findViewById(R.id.listNamesId);
namesText = (TextView) findViewById(R.id.namesTexter);
final ArrayAdapter<String> adapterNames = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1, (List<String>) namesText);
Button buttonPlus = (Button)findViewById(R.id.buttonPlus);
buttonPlus.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
listNames.setAdapter(adapterNames);
}
}
);
}
XML
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ListView
android:id="@+id/listNamesId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="1dp"
android:headerDividersEnabled="true"
android:layout_marginTop="50dp"></ListView>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textnametextname"
android:id="@+id/buttonPlus"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:nestedScrollingEnabled="false" />
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/namesTexter"
android:layout_alignBottom="@+id/buttonPlus"
android:layout_centerHorizontal="true"
android:inputType="text" />