Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I am using a database to store chemical formulas in.

I have an activity where I use a listview to display the formulas and when I add a formula I want it to display on the listview.

I tried using notifyDataSetChanged() but I'm not so sure this will redraw the listview. Also, whenever I click the menu item to add another formula, the previous formula which I have added is added to the listview.

Here is my activity:
Java
<pre>public class FormulaActivity extends WindowListActivity {
	private ArrayAdapter<String> aa;
	private List<String> names = new ArrayList<String>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		tvHeader.setText("Formula Saves");
		
		setContentView(R.layout.formula);
		
		aa = getArrayAdapter();
		setListAdapter(aa);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// Display dialog of formula
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.formula_menu, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		
		switch(item.getItemId()) {
		case R.id.itmAddFormula:
			List<String> temp = names;
			names.clear();
			addFormula();
			
			int lastIndex = formulaDataSource.getAllFormulas().size() - 1;
			temp.add(formulaDataSource.getAllFormulas().get(lastIndex).getName());
			names.addAll(temp);
			aa.notifyDataSetChanged();
			
			break;
			
		default:
			break;
		}
		
		return true;
	}
	
	public ArrayAdapter<String> getArrayAdapter() {
		for(int i = 0; i < formulaDataSource.getAllFormulas().size(); i++) {
			names.add(formulaDataSource.getAllFormulas().get(i).getName());
		}
		
		return new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, 
				names);
	}
}



Here is addFormula() if necessary:
Java
protected void addFormula() {
		AlertDialog.Builder builder = new AlertDialog.Builder(WindowListActivity.this);
		builder.setTitle("Add Formula");
		
		LayoutInflater inflater = getLayoutInflater();
		View layout  = inflater.inflate(R.layout.add_formula_dialog, (ViewGroup) findViewById(R.id.llAddFormula));
	
		final EditText etFormulaName = (EditText) layout.findViewById(R.id.etFormulaName);
		final EditText etFormula = (EditText) layout.findViewById(R.id.etFormula);
		final EditText etFormulaDesc = (EditText) layout.findViewById(R.id.etFormulaDesc);
		
		builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				// Add the formula to database
				formulaDataSource.createFormula(etFormulaName.getText().toString(),
						etFormula.getText().toString(),
						etFormulaDesc.getText().toString());
				
			}
		});
		
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});
		
		builder.setView(layout);
		builder.create().show();
	}
Posted
Updated 13-Apr-12 15:19pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900