是的,基本上我打算制作一个Android应用程序,使用SQLite和所有内容保存和显示手机上的联系人。我是这方面的新手,并且没有在Android Application Developemnt中接受过正式培训。联系人管理器应用程序是我脑海中酝酿的更大应用程序构想的一部分。我首先想要创建的基本功能是使用侧滚动寻呼机保存联系人并在相同的片段活动中查看它们的功能。问题是,寻呼机工作正常,直到我放置一些代码来保存和显示FragmentActivities中的联系人。当我使用测试片段活动时,一切都很顺利。请看下面的代码,试着告诉我用我的代码拧干什么。另外,我使用的是实际手机来运行应用程序(诺基亚X),请告诉我这是否也是一个因素?
package com.example.nikhil.test1;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
/**
* Created by nikhil on 22-10-2015.
*/
public class ContactEdit extends Fragment {
MyDatabaseHelper databaseHelper = new MyDatabaseHelper(this.getActivity());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_contact_edit, container, false);
Button SaveBTN = (Button) getActivity().findViewById(R.id.BtnSave);
SaveBTN.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText _Name = (EditText) getView().findViewById(R.id.textName);
EditText _Mobile = (EditText) getView().findViewById(R.id.textMobile);
EditText _Home = (EditText) getView().findViewById(R.id.textHome);
EditText _Address = (EditText) getView().findViewById(R.id.textAddress);
String name = _Name.getText().toString();
String mobile = _Mobile.getText().toString();
String home = _Home.getText().toString();
String address = _Address.getText().toString();
databaseHelper.addFriend(name, address, mobile, home);
}
});
return rootView;
}
}
package com.example.nikhil.test1;
import android.app.LoaderManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
public class ContactList extends Fragment implements LoaderCallbacks<Cursor> {
SimpleCursorAdapter adapter;
private LoaderManager supportLoaderManager;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.contact_row, container, false);
MyDatabaseHelper dbHelper = new MyDatabaseHelper(this.getActivity());
final Cursor cursor = dbHelper.getAllFriends();
String [] from = new String[] {"name","phonenumber"};
int[] to = { R.id.name, R.id.phonenumber};
ListAdapter adapter=new SimpleCursorAdapter(this.getActivity(),android.R.layout.simple_list_item_2, cursor, from, to);
ListView myList=(ListView) getView().findViewById(R.id.list);
myList.setAdapter(adapter);
//setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1));
return rootView;
}
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
Uri uri = Contacts.CONTENT_URI;
return new CursorLoader(this.getActivity(), uri, null, null, null, null);
}
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
adapter.swapCursor(arg1);
}
public void onLoaderReset(Loader<Cursor> arg0) {
adapter.swapCursor(null);
}
}
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
public class Contacts extends ContentProvider {
public static final String PROVIDER_NAME = "com.example.nikhil.test1.Contacts";
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/contacts" );
private static final int Contacts = 1;
private static final UriMatcher uriMatcher ;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "contacts", Contacts);
}
MyDatabaseHelper mMyDataBaseHelper;
@Override
public boolean onCreate() {
mMyDataBaseHelper = new MyDatabaseHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return null;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
if(uriMatcher.match(uri)==Contacts){
return (Uri) mMyDataBaseHelper.getAllFriends();
}else{
return null;
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
return 0;
}
}
我只提供了相关的代码。这也是我的Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nikhil.test1" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".FirstSplashScreen"
android:label="@string/title_activity_first_splash_screen" >
<intent-filter>
<action android:name="com.example.nikhil.test1.FirstSplashScreen" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<provider
android:name=".Contacts"
android:authorities="com.example.nikhil.test1.Contacts"
android:exported="false" />
</application>
</manifest>