Android ListView not updating and force closing
I'm trying just to get a simple ListView to update when clicking an add button on my UI and taking the edittext and putting in the listview. Can someone look at this and tell me what I'm doing wrong. I've searched here and the developer site and can't figure it out. Thanks
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
public class Ta开发者_C百科skTracker extends Activity {
private Button addButton;
int count = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addButton=(Button)findViewById(R.id.button1);
ListView myListView= (ListView)findViewById(R.id.myListView);
final EditText myEditText= (EditText)findViewById(R.id.editText1) ;
final ArrayList<String> taskitems = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this, 0);
myListView.setAdapter(aa);
addButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
taskitems.add(count, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
count++;
finish();
}
});
}
}
what about this taskitems? I would suggest to do it like that:
addButton.setOnClickListener(new OnClickListener(){
public void onClick(View v){
//taskitems.add(count, myEditText.getText().toString());
aa.add(myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
count++;
finish();
}
});
You need to override the base adapter in order to have button clicks on list items.
Check out this link and follow one of the answers to his blog...
Android: ListView elements with multiple clickable buttons
精彩评论