关于Bomb后台数据库的的问题
发布时间:2018-02-01 来源:查字典编辑
摘要:我写的安卓APP用的Bomb做的数据库。在调用Bomb的bmobQuery.findObjects方法参数用了匿名内部类,导致里面的list...
我写的安卓APP用的Bomb做的数据库。在调用Bomb的bmobQuery.findObjects方法 参数用了匿名内部类,导致里面的list传不出来。不能用全局变量,因为是异步的。怎么才能把它给传出来啊。
public boolean seachData()
{
BmobQuery< UserText > bmobQuery = new BmobQuery< UserText >();
bmobQuery.addWhereEqualTo("Username",Username);//查找条件为Username字段为Username
bmobQuery.findObjects(new FindListener< UserText >(){
//查询方法
@Override
public void done(List< UserText > list, BmobException e) {
if(e == null){
Toast.makeText(getApplicationContext(),"查询成功:共" + list.size() + "条数据。",Toast.LENGTH_LONG);
}
else
{
Toast.makeText(getApplicationContext(),"查询失败:"+e.getMessage(),Toast.LENGTH_LONG);
}
}
});
}
}
public void done(List< UserText > list, BmobException e)里的List< UserText > list如何传出来通过Seach方法return出去 求详细解答啊啊啊啊啊。学渣伤不起
回复讨论(解决方案)
不需要吧...... 用接口回调就可以完成 很简单 能给个示例看看吗 求详细解答啊![
private IUser user;
public void setIUserListener(IUser user){
this.user = user;
}
public boolean seachData() {
BmobQuery< UserText > bmobQuery = new BmobQuery< UserText >();
bmobQuery.addWhereEqualTo("Username", Username);//查找条件为Username字段为Username
bmobQuery.findObjects(new FindListener< UserText >() {
//查询方法
@Override
public void done(List< UserText > list, BmobException e) {
if (e == null) {
if (user!=null){
user.getUser(list);
}
Toast.makeText(getApplicationContext(), "查询成功:共" + list.size() + "条数据。", Toast.LENGTH_LONG);
} else {
Toast.makeText(getApplicationContext(), "查询失败:" + e.getMessage(), Toast.LENGTH_LONG);
}
}
});
}
}
interface IUser{
void getUser(List< UserText > list); 这个是回调代码 然后你在哪里需要用这个集合 然后就实现那个Iuser接口 然后去调用那个 setIUserListener方法就可以了 再有一个办法 的话 你可以用Evenbus去做 不过这个就需要集成第三方的东西了 或者发广播也是可以实现的
主UI
public class MainActivity extends AppCompatActivity {
List< UserText > list_1;
Button m_btnConfirm;
Button m_btnCancel;
ListView listView;
MyAdapter myAdapter;//声明适配器并绑定list
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myAdapter = new MyAdapter(MainActivity.this,list_1);
Bmob.initialize(this, "c8fb80597fb1111a90416da038f59456");//初始化链接数据
listView = (ListView) findViewById(R.id.chen);//找到listView
DataEdit dataEdit = new DataEdit("Cyk");//初始化数据库操作对象
dataEdit.seachData(new DataEdit.Iuser() {
@Override
public void getList(List< UserText > list) {
list_1.addAll(list);
myAdapter.notifyDataSetChanged();
listView.setAdapter(myAdapter);
}
数据库查找类
public void seachData(final Iuser iuser)
{
final List< UserText > list_1 = new ArrayList< UserText >();
BmobQuery< UserText > bmobQuery = new BmobQuery< UserText >();
bmobQuery.addWhereEqualTo("Username",Username);//查找条件为Username字段为Username
bmobQuery.findObjects(new FindListener< UserText >(){
//查询方法
@Override
public void done(List< UserText >list, BmobException e) {
if(e == null){
iuser.getList(list);
Toast.makeText(getApplicationContext(),"查询成功:共" + list.size() + "条数据。",Toast.LENGTH_LONG);
}
else
{
Toast.makeText(getApplicationContext(),"查询失败:"+e.getMessage(),Toast.LENGTH_LONG);
}
}
});
}
interface Iuser
{
void getList(List< UserText > list);
void getid(String Objectid);
}
在主UI调用查找方法 传入接口的实现对象。单步到list_1.addall(list); list是全查出来的 为什么再走一步 list_1还是NULL