问题
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" />
回答1:
The problem is not with your button click listener, it's that you're not using a List as the data source of your Adapter.
I just got this working using an ArrayList as the data source, and updating it in the button click listener. Each time you click the button, whatever is in the EditText gets added to the ListView.
public class MainActivity extends ActionBarActivity {
ArrayList<String> arrayNames = new ArrayList<String>();
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, arrayNames);
listNames.setAdapter(adapterNames);
Button buttonPlus = (Button)findViewById(R.id.buttonPlus);
buttonPlus.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
arrayNames.add(0, namesText.getText().toString());
adapterNames.notifyDataSetChanged();
namesText.setText("");
}
}
);
}
回答2:
In order to achive what you want, it seems without sense set the adapter inside the onclick. What you should do is to add an item inside the onClick()
method, and then call the method notifyDataSetChanged()
on the adapter, for ensuring that the new data are displayed.
This is the code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listNames = (ListView) findViewById(R.id.listNamesId);
final TextView namesText = (TextView) findViewById(R.id.namesTexter);
final ArrayAdapter<String> adapterNames = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
listNames.setAdapter(adapterNames);
Button buttonPlus = (Button)findViewById(R.id.buttonPlus);
buttonPlus.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
if(!namesText.getText().toString().isEmpty()){
adapterNames.add(namesText.getText().toString());
namesText.setText("");
adapterNames.notifyDataSetChanged();
}
}
}
);
}
回答3:
You can do something like this:
public void onMyButtonClick(View view){
listNames.setAdapter(adapterNames);
}
And in your xlm file
<Button>
.....
android:onclick="onMyButtonClick"
....
Hope this will help you.
回答4:
Looks like namesText is a TextView. But its being cast as a List in the next line.
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);
来源:https://stackoverflow.com/questions/31279216/how-to-add-an-item-to-a-listview-with-each-button-click