Android编程新手,无法使用待办事项列表,时间日志,费用记录和练习日志等活动编写应用程序。每个活动都是具有多选择布局的列表视图。例如:
TO-DO: 这是一个单页待办事项列表。用户无法添加其他待办事项列表页面。因此,屏幕显示为空白,底部有两个按钮:ADD和DELETE。用户将使用Android后退按钮退出此活动并返回主视图。您必须为此待办事项列表提供良好的背景,该列表与此功能的主题配合得很好,并且不会太“吵”。您应该使用可滚动的列表视图。每个待办事项都需要为用户提供一种将其标记为已完成的方式。 ADD按钮显示一个弹出文本框,供用户输入待办事项。弹出窗口有一个DONE和CANCEL按钮。按DONE可在待办事项列表的末尾添加待办事项。按CANCEL会将用户返回到待办事项列表,但不会从弹出窗口中添加文本。
我需要使用片段,但无法理解如何编写将附加到所有活动的ListFragment。
待办事项:
public class ToDoActivity extends Activity implements ListViewFragment.OnItemSelectedListener {
final Context context = this;
private ArrayAdapter<String> adapter;
private ArrayList<String> arrayList = new ArrayList<>();
private ListView toDoList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.todo);
// SET ACTION BAR
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_multiple_choice, arrayList);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public void onButtonItemSelected(String link) {
if (link.contentEquals("add")) {
final Dialog dialog = new Dialog(context);
LayoutInflater inflater = getLayoutInflater();
dialog.setContentView(inflater.inflate(R.layout.todo_dialog, null));
dialog.setTitle("Add Task");
dialog.show();
final EditText task = (EditText) dialog.findViewById(R.id.etTaskInfo);
Button btDone = (Button) dialog.findViewById(R.id.btDone);
btDone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayList.add(task.getText().toString());
adapter.notifyDataSetChanged();
dialog.dismiss();
}
});
Button btCancel = (Button) dialog.findViewById(R.id.btCancel);
btCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
if (link.contentEquals("delete")) {
int count = toDoList.getCount();
SparseBooleanArray checkedItemPositions = toDoList.getCheckedItemPositions();
for (int i=0; i < count; i++) {
if (checkedItemPositions.get(i))
arrayList.remove(i);
}
adapter.notifyDataSetChanged();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.menuitem_exit) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setMessage("Are you sure?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
if (id == R.id.menuitem_help) {
Intent helpIntent = new Intent(ToDoActivity.this, Help.class);
helpIntent.putExtra("helptext", getString(R.string.help_todo));
startActivity(helpIntent);
}
if (id == R.id.menuitem_settings) {
Intent settingsIntent = new Intent(ToDoActivity.this, Settings.class);
startActivity(settingsIntent);
}
return super.onOptionsItemSelected(item);
}
}
LISTVIEWFRAGMENT:
public class ListViewFragment extends Fragment {
private String information;
private OnItemSelectedListener listener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_fragment, container, false);
Button btAdd = (Button) view.findViewById(R.id.btAdd);
btAdd.setOnClickListener(actionSelectedListener);
Button btDel = (Button) view.findViewById(R.id.btDelete);
btDel.setOnClickListener(actionSelectedListener);
return view;
}
private OnClickListener actionSelectedListener = new OnClickListener() {
@Override
public void onClick(View v) {
String description = (String) getView().getContentDescription();
information = description;
passSelection();
}
};
public interface OnItemSelectedListener {
void onButtonItemSelected(String link);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
if (activity instanceof OnItemSelectedListener) {
listener = (OnItemSelectedListener) activity;
}
else {
throw new ClassCastException(activity.toString() + "must implement ListViewFragment.OnItemSelectedListener");
}
}
public void passSelection() {
listener.onButtonItemSelected(information);
}
}
LIST_FRAGMENT.XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/todo_bkgd"
xmlns:tools="http://schemas.android.com/tools">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:background="@color/light_grey"/>
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/btAdd"
android:layout_marginBottom="25dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="25dp"
android:background="@color/grey"
android:text="@string/add_btn"
android:textColor="@color/white"
android:textSize="20sp"
android:gravity="center"
android:contentDescription="@string/add_btn" />
<Button
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/btDelete"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="25dp"
android:layout_marginEnd="25dp"
android:background="@color/grey"
android:text="@string/delete_btn"
android:textColor="@color/white"
android:textSize="20sp"
android:gravity="center"
android:contentDescription="@string/delete_btn" />
</RelativeLayout>
TODO.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--FRAGMENT: list_fragment.xml-->
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment
android:id="@+id/fragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
class="mcgill.christina.personaldataassistant_actionbar.ListViewFragment"
tools:layout="@layout/list_fragment"/>
</LinearLayout>
答案 0 :(得分:0)
Activity中的适配器与Fragment中的适配器完全分开。事实上,Activity中的适配器没有设置为任何东西,而Fragment适配器只是空的,因此完全可以解释为什么你什么也看不见。
基本上,将添加和删除方法移到Fragment类。我不确定你想要如何处理添加和删除按钮,但是你可能会使这个过程比你需要的更难。换句话说,你的Fragment只是一个ListView ......真的没必要它
无论如何,如果你真的想要它,测试Fragment甚至可以显示任何东西,将数据添加到你已经拥有的东西
例如
public class ListViewFragment extends Fragment {
private ArrayAdapter<String> adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_fragment, container, false);
// fixed this id reference
ListView listView = (ListView) view.findViewById(android.R.id.list);
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_multiple_choice, arrayList);
listView.setAdapter(adapter);
adapter.add("Working?");
return view;
}