I am working on a storage handling application(real storage). My app. is calling on button push the ZXing barcode reader, after scaning the code, a new activity(window) pops up. Where i can read the scanned code, and i can put in an amount(how much i want from that product). I got there a next and a finish button, on the finish button i go back to the main window. On the next button, the ZXing is called again, i scan the result window pops up , amount input and so on so on... Now to the question, i am going to store the scannedcode+amount in an array. So i can display them in a ListView. I got this:
String[] names = new String[] { "Linux", "Windows7", "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone"};
so i know thats how i make an array with data and thats how i display it:
this.setListAdapter(n开发者_开发技巧ew ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked, product));
I would like to know how i do an empty array, and how i can add new string to it when ever i push the Next button, i will pass the array with bundle between the activitys. So thats it. Uhm and yea is it possible to edit a specific part of the array, cause thats the reason of the list where i show the array to the user, so he can go for example on the Eclipse row, click it and he can write in Android and save.
I hope some one can help or give me some hint on both problems. Thank you in front.
You'd be more flexible if instead you used an ArrayList. You don't need to change the ArrayAdapter part because it has a constructor that works with ArrayLists.
See the doc here: http://developer.android.com/reference/java/util/ArrayList.html
ArrayList allows you to replace, append, and remove as many elements as you want without worrying about reallocating your array or any other low level operation you'd need to do if you'd implemented your own data structure.
Here's an entire example that I was playing with the other day:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class TODOActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get a handle to the ListView and hook it up to the todoList via the
// adapter.
ListView myListView = (ListView) findViewById(R.id.myListView);
final ArrayList<String> todoList = new ArrayList<String>();
final ArrayAdapter<String> todoListAdapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, todoList);
myListView.setAdapter(todoListAdapter);
todoList.
// Get a handle to the EditText and set a key listener to enter text.
// with the dpad or enter key.
final EditText myEditText = (EditText) findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
|| keyCode == KeyEvent.KEYCODE_ENTER)) {
todoList.add(0, myEditText.getText().toString());
todoListAdapter.notifyDataSetChanged();
myEditText.setText("");
return true; // consume event
}
// TODO Auto-generated method stub
return false;
}
});
}
Try using ArrayList<String>
instead of String[]
. Its a little bit more overhead, but it lets you do things like
arrayList.add("Hello, world!");
精彩评论