I uploaded a CSV file to import my database to Parse under a Custom class in Parse.
But for some reason, I can't make a new row via code on Android using the Parse APK.
For example, lets say the my custom class is called "newClass". I have many columns under this class and no new rows can be added. Here is the code I have so far:
//class was populated via CSV file
ParseObject database = new ParseObject("newClass");
database.put(firstColumn, val1);
database.put(secondColumn, val2);
database.put(thirdColumn, val3);
database.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Toast.makeText(coolActivity.this, "Item added to database", Toast.LENGTH_LONG).show();
}
});
When I check parse.com to see if the item was added, I don't see anything updated and it is the same database as before.
Related
I am taking photo using default camera in mobile. After that saving the photo in specific path in folder which path have been created using File class. In Room database I have storing only the path of image. Now, I added delete method to delete specific photo. I am using following query to delete,
#Query("DELETE FROM record_table WHERE photo_path = photoPath")
int deletePhotoPath(String photoPath);
below methods are used for deleting photo(DataRepository.class)
public void deletePhotoPath(String photoPath){
new deletePhotoPathAsyncTask(RecordDao).execute(photoPath);
}
private class deletePhotoPathAsyncTask extends AsyncTask<String,Void,Void>{
public deletePhotoPathAsyncTask(RecordDao dao) {
RecordDao=dao;
}
#Override
protected Void doInBackground(final String... strings) {
int photoPathDeleted=RecordDao.deletePhotoPath(strings[0]);
Log.d("deletePhotoPath"," Photo Path"+photoPathDeleted+strings[0]);
return null;
}
}
I am calling delete method as below(MyActivity.class),
HolderData.deletePhotoPath(photopathArrayList.get(positionOfCurrentViewPhoto));
In HolderData class I have following method to call delete method from database(HolderData.class),
public static void deletePhotoPath(String photoPath){
Log.d(LOG_TAG, "deletPhotoPath:"+photoPath);
dataRepository.deletePhotoPath(photoPath);
}
But my photo is not getting deleted. Logcats inside delete method works fine.
Doesn't know to delete photo. And How do I reflect the change in my design.
Anybody help me to solve this..Already Surfed a lot but not able to find a solution.
Don`t forget to add the colon when you refer to the argument parameter in your query:
DELETE FROM record_table WHERE photo_path = :photoPath
Before deleting the link from Room extract it, create a File instance programmatically and call "delete" method on that instance:
File file = new File("path to your file");
file.delete();
I am trying to display a list in android using guidance from vogella's tutorial for sqlite in android :
this is part of my ProjectListDataSource class (This gets all data from the sqlite database):
public List<ProjectList> getAllProjects() {
List<ProjectList> projects = new ArrayList<ProjectList>();
Cursor cursor = database.query(ProjectListHelper.TABLE_PROJECT_LIST,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
projects.add(cursorToProjectList(cursor));
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return projects;
}
private ProjectList cursorToProjectList(Cursor cursor) {
ProjectList projList = new ProjectList();
projList.setId(cursor.getLong(0));
projList.setProjName(cursor.getString(1));
projList.setProjComment(cursor.getString(2));
projList.setProjDateTime(cursor.getString(3));
return projList;
}
And this is my activity class :
public class ProjectListActivity extends ListActivity implements
OnClickListener {
private static final String TAG = "ProjectListActivity";
private ProjectListDataSource datasource;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_projectlist);
Log.d(TAG, "On Creat'd");
init();
}
private void init() {
// Getting data from database and adding to ListView
datasource = new ProjectListDataSource(this);
datasource.open();
List<ProjectList> values = datasource.getAllProjects();
ArrayAdapter<ProjectList> adapter = new ArrayAdapter<ProjectList>(this,
android.R.layout.simple_list_item_activated_1, values);
setListAdapter(adapter);
}
But on doing that I am getting unexpected result like this (see image) :
http://i.imgur.com/tQMooi8.png
But the database has records like this (see image):
http://i.imgur.com/HfY2azs.png
Can anyone please explain and give a solution as to why I cant get the list view to show the records as in the database...
Thanks,
Viney
Basic adapters, like ArrayAdapter or CursorAdapter, will map only one value to a single view. Here, a ProjectList object to a TextView with the id of android.R.id.simple_list_item_activated_1
You need a single layout(for a single view within the ListView) with multiple views to which you will map id, name, date, comment, etc. You need to extend one of the adapters. Preferably BaseAdapter or CursorAdapter.
There are several issues here, depending on what you want to display.
The reason you're seeing the object string reference in your list is because you're using a plain ArrayAdapter, which simply calls toString() on the objects in the array. If you override toString() in ProjectList, you can display what you want (though that's normally not the best way to solve this problem).
Another option which would allow you to keep using a plain ArrayAdapter would be to create an array of strings from the ProjectList objects of the data you want to display. A bit wasteful, but that's another option.
What you normally want to do is extend ArrayAdapter and override getView(). In getView() you assign the data you want to display in the view.
If you want to display all the data from your ProjectList objects in a single list item, you'll also need to create a custom layout to represent the row.
I have set up a basic entity class in my App Engine backend.
`#Entity`
`public class Club {`
`#Id`
`private int id;`
`private String clubName;`
`public Club() {`
`}`
`public int getId() {`
`return id;`
`}`
`public void setId(int id){
this.id =id;
}`
`public String getClubName() {
return clubName;
}`
`public void setClubName(String clubName) {
this.clubName = clubName;
}
}`
I have generated the cloud endpoint class and generated the cloud endpoint library.
I want to be able to populate the clubName from the datastore into a listview in android but not sure how to do this.
I'm trying to follow this https://developers.google.com/appengine/docs/java/endpoints/consume_android but so far I am unable to understand what to do. I'm new to this and would be greatful if anyone lead me in the right direction please.
You can use the below steps to achieve your objective:
1, As mentioned in the google doc on consuming cloud endpoints in android, ready your app by adding the required libraries and make API calls and then on getting data from your backend, you can store that data into a SQL database. You can do this step in the onpostexecute method of your asynch task
protected void onPostExecute(ScoreCollection scores) {
// Do something with the result. maybe the store the data to sqlite database
}
}
To know how to store data in SQLITe database please refer http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
http://www.vogella.com/articles/AndroidSQLite/article.html
2, Now query data from your SQLITE db into a cursor and then use a simplecursoradapter to display the data from cursor on a listview in your layout or activity screen
An approximate piece of code will have steps like below:
yourcursor = yourdatabase.query; //this is the cursor returned from querying your db
adapter = new SimpleCursorAdapter(this.getActivity(), R.layout.onerowinalistview, yourcursor, FROM, TO,0); // this connects the columns of your DB mentioned in FROM array to the elements in a row of your list as mentioned in TO array
ListView yourlistview = (ListView) view.findViewById(R.id.yourlistview);//this is the listview element in your screen layout
yourlistview.setAdapter(adapter); // setting the adapter to the listview
Hope this helps
The content of the tab is formed and displayed when the application is loaded. Later the content of the tab may be changed by other actions. I want to show the newer content after each action. And each time when I click the tab sheet, the content should be refresh/updated. But I failed.
//the content of the tab from the "reprintsTab" class
//in the "reprintsTab" it query data from database and print out
//later I update the data in the database from somewhere else, and I want the tab shows the new content
//I want to click the tab sheet to reload the "reprintTab" class and print out the new content
//here is what I did:
public TabSheet sheet;
//add tab and add the content from "reprintTab" into this tab
sheet.addTab(new reprintsTab());
//add the listener
sheet.addListener(new TabSheet.SelectedTabChangeListener() {
#Override
public void selectedTabChange(SelectedTabChangeEvent event) {
//I know it does not work, because it only reload the class. but not put the content under the tab I want
new reprintsTab();
}
});
What should I do? please help me, thanks.
You can use TabSheet.replaceComponent method to do this:
//Field to store current component
private reprintsTab currentComponent;
//during initialization
currentComponent = new reprintsTab();
sheet.addTab(currentComponent);
sheet.addListener(new TabSheet.SelectedTabChangeListener() {
#Override
public void selectedTabChange(SelectedTabChangeEvent event) {
reprintsTab newComponent = new reprintsTab();
sheet.replaceComponent(currentComponent, newComponent);
currentComponent = newComponent;
}
});
Also, you might want to reload this tab only when it's shown:
sheet.addListener(new TabSheet.SelectedTabChangeListener() {
#Override
public void selectedTabChange(SelectedTabChangeEvent event) {
if (event.getTabSheet().getSelectedTab() == currentComponent) {
//here goes the code
}
}
});
This should work for you, but I would suggest a cleaner approach: implement reprintsTab as a container for components, create method reload or buildInterface method to refresh its' state, so you can just call:
currentComponent.reload();
when you need to update interface.
Also, I hope reprintsTab is just an example name, java class names starting with lowercase letter look ugly.
I have a :
Client Class
ListView
TextField
I need to populate my ListView in order to form a table:
WORKING CODE:
clientModel = new LoadableDetachableModel() {
#Override
protected Object load() {
return Client.getClientListByCompanyName(searchClientInput.getValue());
}
};
searchClientInput.setModel(new Model<String>());
searchClientInput.add(new AjaxFormComponentUpdatingBehavior("onkeyup") {
#Override
protected void onUpdate(AjaxRequestTarget target) {
target.add(clientListViewContainer);
}
});
clientListView = new ListView<Client>(CLIENT_ROW_LIST_ID, clientModel) {
#Override
protected void populateItem(ListItem<Client> item) {
Client client = item.getModelObject();
item.add(new Label(CLIENT_ROW_COMPANY_CNPJ_ID, client.getCompanyName()));
item.add(new Label(CLIENT_ROW_COMPANY_NAME_ID, client.getCompanyCnpj()));
}
};
clientListViewContainer.setOutputMarkupId(true);
clientListViewContainer.add(clientListView);
add(clientListViewContainer);
Now, in my HTML, I have a TextField. Whenever an user types something in this TextField, a select will be made in the database with whatever he typed. So for each word, a select is made, and the table needs to be updated. I am guessing I will need to use AJAX and possibly a Model. I'm kind of lost about how I can do this, if someone can provide me examples I would be very grateful.
EDIT: New code that is throwing exception: Last cause: Attempt to set model object on null model of component: searchClientForm:searchClientInput
EDIT 2: Ok so the exception was that my TextField didn't had a model to bind data to. So what I did was: searchClientInput.setModel(new Model<String>());
I also had a problem with the event. Using onkeydown was working, but not as intended. I had Company Name 1-4. If I typed Company Name 1, I would need to press one key again so the table would get updated. With onkeyup this don't happens. Thanks for the help.
You could give the ListView a LoadableDetachableModel which provides the selected clients matching your TextField's value.
Use an AjaxFormComponentUpdatingBehavior on your TextField which add a parent of the ListView to the request target (don't forget #setOutputMarkupId().
I believe the best way to perform what you want (which is repainting a table/list at each input change --> DB access) is with a DataView and a DataProvider.
A DataView is just like the ListView component except it uses an IDataProvider to get the data you want to present. You are able to implement the DataProvider so it accesses your DB, and you can add restrictions (where clauses) to the DataProvider.
[this is more like pseudo-code]
public final class MyDataProvider<T> extends SortableDataProvider<T> {
// ...
Set filters;
// filters is the set where the restrictions you want to apply are stored
...
#Override
public Iterator<T> iterator(int first, int count) {
// DAO (Data Access Object) access to DB
// ...
return dao.findByRestrictions(filters).iterator();
}
...
}
Now on the ajax event on your input component you are able to update the filter being used in the DataProvider, and in the the next repaint of the DataView, the provider will "pull" the data matching the restrictions defined in the filter.
Hope it helps. Best regards.