我正在尝试在Activity中创建ListView的片段,以便与CursorLoader交互以从sqlite数据库获取数据并将其同步加载到Frgament中的listview中
活动代码:
package com.example.startup.myfriends;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class MainActivity extends AppCompatActivity {
FloatingActionButton floatingActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setToolbar();
floatingActionButton = findViewById(R.id.mainActivityAddFriendFloatingButton);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getBaseContext(), AddFriend.class));
}
});
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.familyLayoutContainer, new FamilyListViewFragment()).commit();
}
private void setToolbar() {
android.support.v7.widget.Toolbar toolbar = findViewById(R.id.mainActivityToolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Family");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_add_friend, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("You'll Delete All Items. Are You Sure ?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.show();
return super.onOptionsItemSelected(item);
}
}
CursorLoader片段代码:
package com.example.startup.myfriends;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.example.startup.myfriends.data.AppContract;
import com.example.startup.myfriends.data.FamilyListCursorAdapter;
public class FamilyListViewFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
ListView listView;
FamilyListCursorAdapter familyListCursorAdapter;
private Cursor mDetailCursor;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_family_list_view, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = getActivity().findViewById(R.id.listView);
final String projection [] = {
AppContract.Family.COLUMN_ID,
AppContract.Family.COLUMN_NAME,
AppContract.Family.COLUMN_LAST_NAME,
AppContract.Family.COLUMN_AGE,
AppContract.Family.COLUMN_GENDER
};
Cursor cursor = getActivity().getContentResolver().query(AppContract.Family.CONTENT_URI,
projection, null, null, null);
/*
familyListCursorAdapter = new FamilyListCursorAdapter(getActivity().getBaseContext(), cursor);
listView.setAdapter(familyListCursorAdapter);
*/
familyListCursorAdapter = new FamilyListCursorAdapter(getActivity().getBaseContext(), cursor);
listView.setAdapter(familyListCursorAdapter);
getLoaderManager().initLoader(1, null, this);
}
@Override
public Loader<Cursor> onCreateLoader(int i, Bundle bundle) {
Uri CONTACT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
CursorLoader cursorLoader = new CursorLoader(getActivity(), CONTACT_URI, null,
null, null, null);
/*
cursor.moveToFirst();
*/
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
familyListCursorAdapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
familyListCursorAdapter.swapCursor(null);
}
}
MyApp Always总是这样崩溃:Crashed App snap shot
请任何正在尝试修复的帮助从昨天开始尝试由我自己修复,但我退出了:(
答案 0 :(得分:0)
它读取fragment_family_list_view.xml
(您的内容)必须具有ListView
,其id属性为android.R.id.list
;与(包名称)R.id.list
不同。
以<ListView android:id="@android:id/list" ...