I'm a in the process of understanding CouchBase Lite for android application,as far i only know how to create a manager, database, document and view.However i want to load data from the documents and show it in the app but failed after trying several times and following some tutorials.Is there a way i can save and load this data and manipulate it in Listview? The code i used til now is the following:
public class MainActivity extends AppCompatActivity {
final String dbname = "userdils";
private String docId = "2";
private Manager manager;
private Database couchDb;
private TextView tvLogWindow;
private EditText u_id;
private EditText pwd;
Button btn_login,btn_login1;
TextView version;
private String TAG = "CouchBaseLiteLoginPage";
String email;
String password;
private UserDetails ud,udd;
private ArrayList<UserDetails> ud1;
String doc_id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//tvLogWindow = (TextView) findViewById(R.id.tv_log_window);
u_id = (EditText) findViewById(R.id.user_id);
pwd = (EditText) findViewById(R.id.password);
btn_login = (Button) findViewById(R.id.btn_login);
btn_login1 = (Button) findViewById(R.id.btn_login1);
/*tvLogWindow.setText(tvLogWindow.getText().toString() + "\n\nStart Couchbase App!");
if (!Manager.isValidDatabaseName(dbname)) {
tvLogWindow.setText(tvLogWindow.getText().toString() + "\n\nBad couchbase db name!");
return;
}*/
createManager();
createCouchdb();
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Log.d(TAG, "Create: " + 1);
login();
}
});
btn_login1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
Log.d(TAG, "View: " + 2);
docView();
// retrieveMovie(manager,couchDb,docId);
}
});
//createDocument(docId);
//Document retrievedDocument = retrieveDocument();
/*updateDocument(retrievedDocument);
deleteDocument(retrievedDocument);*/
//tvLogWindow.setText(tvLogWindow.getText().toString() + "\n\nEnd the App!");
}
public void createManager() {
try {
manager = new Manager(new AndroidContext(getApplicationContext()), Manager.DEFAULT_OPTIONS);
} catch (IOException e) {
return;
}
}
public void createCouchdb() {
try {
couchDb = manager.getDatabase(dbname);
} catch (CouchbaseLiteException e) {
return;
}
}
public Document retrieveDocument(String docId) {
Document retrievedDocument = couchDb.getDocument(docId);
Log.d(TAG, "Retrieved Data:- " + String.valueOf(retrievedDocument.getProperties()));
// Let's find the documents that have conflicts so we can resolve them:
Query query = couchDb.createAllDocumentsQuery();
query.setAllDocsMode(Query.AllDocsMode.ALL_DOCS);
try {
QueryEnumerator result = query.run();
for (Iterator<QueryRow> it = result;
it.hasNext(); ) {
QueryRow row = it.next();
Object movieObj = row.getDocument().getProperties().get("user_det");
Gson gson = new Gson();
String jsonString = gson.toJson(movieObj, Map.class); //Convert the object to json string using Gson
UserDetails movie = gson.fromJson(jsonString, UserDetails.class); //convert the json string to Movie object
if(movie.getUser_pwd().equals("000000")) {
Log.i("getMovieFromDocument", "movie>>>" + movie.getUser_nm());
}
Log.d(TAG, "Document Data ID:- " + row.getDocumentId());
}
}
catch (CouchbaseLiteException e) {
}
// Log.d(TAG, "User Name:- " +(String)retrievedDocument.getProperty("user_det"));
return retrievedDocument;
}
public void login() {
Log.d(TAG, "validate: " + 4);
email = u_id.getText().toString().trim();
password = pwd.getText().toString().trim();
Log.d(TAG, "login: " + 5);
btn_login.setEnabled(false);
ud = new UserDetails();
udd = new UserDetails();
ud1 = new ArrayList<UserDetails>();
ud.setUser_nm(email);
ud.setUser_pwd(password);
udd.setUser_nm("NAME_007");
udd.setUser_pwd("PWD_007");
ud1.add(ud);
ud1.add(udd);
createDocument(docId,ud1);
}
public void createDocument(String docId,ArrayList<UserDetails> ud) {
// create some dummy data
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
Calendar calendar = GregorianCalendar.getInstance();
String currentTimeString = dateFormatter.format(calendar.getTime());
//Movie movie = new Movie("Star War", "The force awakens!", 120);
// put those dummy data together
Map<String, Object> docContent = new HashMap<String, Object>();
docContent.put("message", "Hey Couchbase Lite");
docContent.put("creationDate", currentTimeString);
docContent.put("user_det", ud);
//docContent.put("movie", movie);
Log.d(TAG, "userDetails: " + String.valueOf(docContent));
// create an empty document, add content and write it to the couchDb
//Document document = new Document(couchDb,docId);
//Document document = couchDb.createDocument();
Document document = new Document(couchDb,docId);
try {
document.putProperties(docContent);
//doc_id = document.getId();
// Document retrieveDoc = retrieveDocument(docId);
Log.d(TAG, "Document Count couchDb : " + couchDb.getDocumentCount());
} catch (CouchbaseLiteException e) {
}
}
public void docView() {
// Document retrievedDocument = retrieveDocument(docId);
Document retrievedDocument = retrieveDocument(docId);
}
private void retrieveMovie(Manager manager, Database couchDb, String docId) {
Document retrievedDocument = couchDb.getDocument(docId); // Retrieve the document by id
Object movieObj = retrievedDocument.getProperties().get("user_det");
Gson gson = new Gson();
String jsonString = gson.toJson(movieObj, Map.class); //Convert the object to json string using Gson
UserDetails movie = gson.fromJson(jsonString, UserDetails.class); //convert the json string to Movie object
Log.i("getMovieFromDocument", "jsonString>>>" + jsonString);
Log.i("getMovieFromDocument", "movie>>>" + movie.getUser_nm());
}
}