我正在使用Java开发一个类项目,探索使用ContentProvider
类在应用程序之间移动数据。我们正在制作一个基本的CRUD应用程序,使用SQLite
存储和ContentProvider将数据移动到第二个应用程序。我的调试显示我的SQLite插件似乎正在工作并构建我的数据库,但我的ListView
将不会填充。它永远给出一个旋转加载环,这不是我通常用ListView看到的问题。我已经仔细检查了适配器是否到位,我使用loadData()
方法的一部分来创建数据库中的项目对象列表,这是我无法解决的问题。我不确定这个问题可能来自哪里。
MainActivity:
public static DatabaseHelper helper;
public static ItemProvider provider;
static Button addNew;
public static final int NEXT_REQUESTCODE = 1;
static ListFragment frag;
static ArrayList<ItemObject> items = new ArrayList<ItemObject>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper = new DatabaseHelper(this);
provider = new ItemProvider(helper);
FragmentManager mgr = getFragmentManager();
FragmentTransaction trans = mgr.beginTransaction();
final Context context = this.getApplicationContext(); //to pass as context
frag = ListFragment.newInstance(context, items);
frag.loadData();
trans.replace(R.id.fragment_container, frag, ListFragment.TAG);
addNew = (Button) findViewById(R.id.addNew);
addNew.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent addActivity = new Intent(context, FormActivity.class); //sets intent for addactivity
startActivityForResult(addActivity, NEXT_REQUESTCODE); //starts activity expecting response
}
});
}
//handles response from addactivity
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode == RESULT_OK && requestCode == NEXT_REQUESTCODE) {
Bundle result = data.getExtras();
ItemObject placeholder = new ItemObject(result.getString("weight"), result.getInt("steps"), result.getString("date")); //builds the object to be added to the list from addactivity response
frag.addObject(placeholder); //adds the object to the list
}
}
Context getContext() {
return this.getContext();
}
}
SQLiteOpenHelper类
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_FILE = "database.db";
private static final int DATABASE_VERSION = 1;
public static SQLiteDatabase db;
SQLiteDatabase.CursorFactory factory;
private static final String DROP_TABLE = "DROP TABLE IF EXISTS item_table";
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS item_table ( _id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT, steps INTEGER, weight TEXT)";
public DatabaseHelper(Context c) {
super(c, DATABASE_FILE, null, DATABASE_VERSION);
db = getWritableDatabase();
// db.execSQL(DROP_TABLE);
db.execSQL(CREATE_TABLE);
//db.openOrCreateDatabase(DATABASE_FILE, factory);
}
@Override
public void onCreate(SQLiteDatabase _db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void addItem(ContentValues v)
{
}
}
ListFragment类
public class ListFragment extends Fragment {
public static final String TAG = "ListFragment.TAG";
public static ArrayList<ItemObject> items = new ArrayList<ItemObject>();
static ListView itemList;
static ItemAdapter adapter;
static int selectedPosition;
public static final int NEXT_REQUESTCODE = 2;
static Context context;
static ArrayList<ItemObject> placeholder;
static MainActivity activity;
static Uri uri = Uri.EMPTY;
//constructor
public static ListFragment newInstance(Context _context, ArrayList<ItemObject> _items) {
ListFragment fragment = new ListFragment();
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View theView = inflater.inflate(R.layout.list_fragment_layout, parent, false);
//create and attach custom adapter
MainActivity activity = (MainActivity) getActivity();
context = activity.getContext();
itemList = (ListView) theView.findViewById(R.id.itemList);
adapter = new ItemAdapter(context, items);
itemList.setAdapter(adapter);
itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long arg3) {
selectedPosition = position;
openView(items.get(position));
}
});
loadData();
adapter.notifyDataSetChanged();
return theView;
}
//function for opening the "View details" view
public void openView(ItemObject selected) {
Intent detailActivity = new Intent(getActivity(), DetailActivity.class);
detailActivity.putExtra("weight", selected.getWeight());
detailActivity.putExtra("steps", selected.getSteps());
detailActivity.putExtra("date", selected.getDate());
detailActivity.putExtra("position", selectedPosition);
startActivityForResult(detailActivity, NEXT_REQUESTCODE);
}
public void saveItem(ItemObject _item) {
ContentValues v = new ContentValues();
v.put("weight", _item.getWeight());
v.put("steps", _item.getSteps());
v.put("date", _item.getDate());
activity.provider.insert(uri, v);
loadData();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void loadData() {
String[] columns = new String[]{"weight","steps","date"};
Uri destination = Uri.parse(Environment.getDataDirectory().toString()+"/data/ContentProviderApp/database.db");
Cursor c = activity.provider.query(destination, columns, null, null, null, null);
if(c != null)
{
c.moveToFirst();
ArrayList<ItemObject> placeholder = new ArrayList<ItemObject>();
for(int i = 0; i < c.getCount(); i++)
{
int x = c.getCount();
ItemObject newItem = new ItemObject(c.getString(3),c.getInt(2), c.getString(1));
placeholder.add(newItem);
c.moveToNext();
}
items = placeholder;
}
}
public void deleteData() {
}
public void updateData() {
}
//handles a delete request from "view" activity
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == getActivity().RESULT_OK && requestCode == NEXT_REQUESTCODE) {
Bundle result = data.getExtras();
adapter.notifyDataSetChanged();
}
}
//Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
public ListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
//adds an object passed in from the activity
public void addObject(ItemObject _item) {
saveItem(_item);
//items.add(_item);
//adapter.notifyDataSetChanged();
//saveData();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
// mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
// mListener = null;
}
}
ContentProvider类
public class ItemProvider extends ContentProvider {
DatabaseHelper helper;
public ItemProvider() {
}
public ItemProvider(DatabaseHelper _helper) {
helper = _helper;
}
@Override
public boolean onCreate() {
return false;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
return helper.db.query("item_table", null, null, null, null, null, null, null);
}
@Nullable
@Override
public String getType(Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
helper.db.insert("item_table", null, values);
return null;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
helper.db.delete("item_table", selection, selectionArgs);
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
helper.db.update("item_table", values, selection, selectionArgs);
return 0;
}
}
ItemAdapter class
public class ItemAdapter extends BaseAdapter {
private Context context;
private static final long ID_CONSTANT = 0x666666L;
private ArrayList<ItemObject> items;
public ItemAdapter()
{
}
public ItemAdapter(Context _context, ArrayList<ItemObject> _items)
{
context = _context;
items = _items;
}
@Override
public int getCount() {return items.size();}
@Override
public ItemObject getItem(int _position) {return items.get(_position);}
@Override
public long getItemId(int _position) { return ID_CONSTANT + _position;}
@Override
public View getView (int _position, View _convertView, ViewGroup _parent) {
if(_convertView == null) {
_convertView = LayoutInflater.from(context).inflate(R.layout.item_layout, _parent, false);
}
ItemObject item = getItem(_position);
TextView text = (TextView) _convertView.findViewById(R.id.weight);
text.setText(item.getWeight().toString());
text = (TextView) _convertView.findViewById(R.id.steps);
text.setText(Integer.toString(item.getSteps()));
text = (TextView) _convertView.findViewById(R.id.date);
text.setText(item.getDate().toString());
return _convertView;
}
}
答案 0 :(得分:0)
我认为问题出在loadData方法中:
items.addAll(placeholder)
应该是public void loadDataWithBaseURL(String baseUrl,
String data,
String mimeType,
String encoding,
String historyUrl)
。
你有没有机会测试这个?
答案 1 :(得分:0)
在 item =占位符行后的 loadData()中添加 adapter.notifyDataSetChanged(); 。