我正在了解CouchBase Lite for android应用程序的过程,因为我只知道如何创建管理器,数据库,文档和视图。但是我想从文档中加载数据并在应用程序中显示它但尝试了几次并遵循一些教程后失败了。有没有办法可以保存并加载这些数据并在Listview中操作它?我现在使用的代码如下:
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());
}
}
答案 0 :(得分:0)
不清楚您遵循的教程以及您观察到的错误。无论如何,请查看此blog,其中讨论了如何使用Couchbase Lite作为数据源在RecyclerView中加载文档。推荐的模式可以轻松应用于ListView,或者只是在应用程序中使用RecyclerView而不是ListView。