我想了解如何创建可点击的Listview并转到新页面,用户可以在创建待办事项列表时在列表中添加项目,我希望用户能够创建多个列表,然后点击列表,并在该列表中输入多个项目。
感谢。
更新 - 添加.java代码以供参考。这可以解释我迄今为止实施的内容比上述文本更好。
public class ActionBarMenuDemoActivity extends SherlockListActivity {
private static final String[] items = { };
private ArrayList<String> words = null;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
initAdapter();
registerForContextMenu(getListView());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.option, menu);
EditText add = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
View v = menu.findItem(R.id.add).getActionView();
if (v != null) {
add = (EditText) v.findViewById(R.id.title);
}
}
if (add != null) {
add.setOnEditorActionListener(onSearch);
}
return (super.onCreateOptionsMenu(menu));
}
public void onCreateContextMenu(Menu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
new MenuInflater(this).inflate(R.menu.context, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.add:
add();
break;
case R.id.preferences:
startActivity(new Intent(getApplicationContext(),SettingsActivity.class));
break;
}
return (super.onOptionsItemSelected(item));
}
@Override
@SuppressWarnings("unchecked")
public boolean onContextItemSelected(android.view.MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();
int itemId = item.getItemId();
if (itemId == R.id.cap) {
String input = words.get(info.position);
input = input.toUpperCase();
adapter.remove(words.get(info.position));
adapter.insert(input, info.position);
return (true);
}
return (super.onContextItemSelected(item));
}
private void initAdapter() {
words = new ArrayList<String>();
for (String s : items) {
words.add(s);
}
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, words));
}
private void add() {
final View addView = getLayoutInflater().inflate(R.layout.add, null);
new AlertDialog.Builder(this).setTitle("Add a List").setView(addView)
.setPositiveButton("Create", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
addWord((TextView) addView.findViewById(R.id.title));
}
}).setNegativeButton("Cancel", null).show();
}
@SuppressWarnings("unchecked")
private void addWord(TextView title) {
ArrayAdapter<String> adapter = (ArrayAdapter<String>) getListAdapter();
adapter.add(title.getText().toString());
Toast.makeText(ActionBarMenuDemoActivity.this,
title.getText().toString() + " Created", Toast.LENGTH_LONG)
.show();
title.setText("");
}
private TextView.OnEditorActionListener onSearch = new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event == null || event.getAction() == KeyEvent.ACTION_UP) {
addWord(v);
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return (true);
}
};
}
有些代码将被删除,因为我以教程为出发点。