Reputation: 435
UPDATED ItemEntryFragment.java
and ToDoActivity.java
to include my latest attempt to get this thing to work, but it doesn't
I am in an android development class. our assignment is to make a simple todo list using two fragments. The first fragment is an EditText and the second is a listView.
when a user enters an item into the edittext box and hits enter, the string is added to the listview.
update 2 The application now successfully loads on my phone and the fragments look as expected. however when I press the enter key after inputting text in the EditText line, the setOnKeyListener is never fired. The code segments are the most current.
any suggestions/pointers would be appreciated.
Below are my xml files that I think are correct
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:name="com.todo.ToDoListFragment"
android:id="@+id/todolistFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:name="com.todo.ItemEntryFragment"
android:id="@+id/textboxFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
_
list_view_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="@string/addItemContentDescription"
android:hint="@string/addItemHint" />
</LinearLayout>
_
edit_text_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
and here are my .java files
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
public class ItemEntryFragment extends Fragment{
private ToDoListActivity activity;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.text_entry_box, container, false);
final EditText myEditText = (EditText)view.findViewById(R.id.myEditText);
myEditText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || (keyCode == KeyEvent.KEYCODE_ENTER)) {
String newItem = myEditText.getText().toString();
activity.addItem(newItem);
myEditText.setText("");
return true;
}
return false;
}
});
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = (ToDoListActivity)activity;
}
}
_
public class ToDoListFragment extends ListFragment {
//this class is empty because the ListFragment auto adds the ListView
}
and here is my main java file
import java.util.ArrayList;
import android.app.Activity;
import android.app.FragmentManager;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class ToDoListActivity extends Activity {
private ArrayAdapter<String> aa;
private ArrayList<String> todoItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//inflate view
setContentView(R.layout.activity_to_do_list);
//get references to fragments
FragmentManager fm = getFragmentManager();
ToDoListFragment todoListFragment = (ToDoListFragment) fm.findFragmentById(R.id.todolistFragment);
todoItems = new ArrayList<String>();
//create array adaptor to bind array to list view
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, todoItems);
//bind array adapter to list view
todoListFragment.setListAdapter(aa);
}
public void addItem(String newItem){
todoItems.add(newItem);
aa.notifyDataSetChanged();
}
}
Upvotes: 0
Views: 3033