从安卓中的 Parse.com 检索动态数组列表



![在此处输入图像描述][1]我是Android的初学者,我正在使用后端 Parse.com 在我的应用程序中存储和检索信息。问题是我想检索带有姓名和职业的人员列表,但是我只能检索一个人的详细信息,我有多个人,但它在我的文本视图中只显示一条记录,我应该使用 listview,但我不明白适配器。我尝试在以下教程后实现,但它们不起作用。请告诉我如何在应用活动中检索多条记录或从 parse.com 动态加载文本视图。

显示

数据活动仅显示 1 人的记录,但我有多个记录

public class ShowData extends ActionBarActivity {
    Button Bsearch;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_data);
        Parse.initialize(this, "keys", "keys");

        // Uses a layout with a ListView (id: "listview"), which uses our Adapter.
        Bsearch= (Button) findViewById(R.id.Bsearch);

        Bsearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

               ParseQuery<ParseObject> query = ParseQuery.getQuery("Pro");
                query.whereEqualTo("City", "Pune");
                query.getFirstInBackground(new GetCallback<ParseObject>() {
                    public void done(ParseObject emp, ParseException e) {
                        if (emp == null) {
                            Toast.makeText(ShowData.this,"Error", Toast.LENGTH_LONG).show();
                        } else {
                            String n =emp.getString("Name");
                            String prof= emp.getString("Professional");
                            //Toast.makeText(MainActivity.this,"User"+un+"n Password"+pwd,Toast.LENGTH_LONG).show();
                            TextView t = (TextView)findViewById(R.id.TVresult);
                            t.setText(""+n);
                            TextView t1 = (TextView) findViewById(R.id.TVresult1);
                            t1.setText("" + prof);
                        }
                    }
                });
            }
        });

    }

}

编辑的查询不起作用

  Bsearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Pro");
            query.whereEqualTo("City", "Pune");
            query.findInBackground(new FindCallback<ParseObject>() {
                public void done(List<ParseObject> emp, ParseException e) {
                    if (e == null) {
                        String n =emp.getString("Name");
                        String prof= emp.getString("Professional");
                        Toast.makeText(ShowData.this,"success", Toast.LENGTH_LONG).show();
                        TextView t = (TextView)findViewById(R.id.TVresult);
                        t.setText(""+n);
                        TextView t1 = (TextView) findViewById(R.id.TVresult1);
                        t1.setText("" + prof);
                    } else {
                        Log.d("score", "Error: " + e.getMessage());
                    }
                }
            });
        }
    });

https://i.stack.imgur.com/yxUqv.png

设法修复了emp对象错误,但是当我点击搜索按钮应用程序崩溃时,不幸的是,我弹出了应用程序已停止在这里是logcat

    04-24 18:39:34.208    4280-4280/com.example.sangram.nearbyapp E/Trace﹕ error opening trace file: No such file or directory (2)
04-24 18:39:34.432    4280-4280/com.example.sangram.nearbyapp E/ResourceType﹕ Style contains key with bad entry: 0x01010479
04-24 18:40:05.836    4280-4280/com.example.sangram.nearbyapp E/ResourceType﹕ Style contains key with bad entry: 0x01010479
04-24 18:40:08.397    4280-4280/com.example.sangram.nearbyapp E/ResourceType﹕ Style contains key with bad entry: 0x01010479
04-24 18:40:08.443    4280-4280/com.example.sangram.nearbyapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sangram.nearbyapp/com.example.sangram.nearbyapp.ShowData}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.access$600(ActivityThread.java:149)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
            at android.app.ListActivity.onContentChanged(ListActivity.java:243)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:261)
            at android.app.Activity.setContentView(Activity.java:1867)
            at com.example.sangram.nearbyapp.ShowData.onCreate(ShowData.java:68)
            at android.app.Activity.performCreate(Activity.java:5020)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
            at android.app.ActivityThread.access$600(ActivityThread.java:149)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:153)
            at android.app.ActivityThread.main(ActivityThread.java:5086)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
            at dalvik.system.NativeStart.main(Native Method)

您正在调用getFirstInBackground,它将只返回一条记录。将查询更改为:

query.findInBackground(new FindCallback<ParseObject>() {
     public void done(List<ParseObject> objects, ParseException e) {
         if (e == null) {
             objectsWereRetrievedSuccessfully(objects);
         } else {
             objectRetrievalFailed();
         }
     }
 }

我想你想实现动态列表视图。您可以查看本教程。这将是有帮助的。

http://www.pcsalt.com/android/listview-using-baseadapter-android/

这应该有效,您只调用一个结果返回,并且您没有 for 循环来遍历对象。 我尝试编辑您的代码,看看并让我知道它是否有效:

public void updateMegaMillionsData() {
    ParseQuery<GameDataParseClass> query = ParseQuery.getQuery("Pro");
    query.whereEqualTo("City", "Pune");
    query.getFirstInBackground(new GetCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> emp, ParseException e) {
            if (emp != null) {
                String n = String.valueOf(emp.get("Name"));
                String prof= String.valueOf(emp.get("Professional"));
                Toast.makeText(ShowData.this, "success", Toast.LENGTH_LONG).show();
                TextView t = (TextView)findViewById(R.id.TVresult);
                t.setText(""+n);
                TextView t1 = (TextView) findViewById(R.id.TVresult1);
                t1.setText("" + prof);
            }
        }
    });
}

最新更新