在OnTimeClickListener方法中检索Parse ObjectID

时间:2016-01-15 22:36:02

标签: android parse-platform android-listview

我想检索一个项目的objectID(具体来说,它是一个名为&#34的类;我们的Parse模型中的Room")在ListView内点击。 之后,我想使用Intent将检索到的ObjectID传递给另一个类。

我试过解析文档' getObjectId();方法,但似乎它不会工作。 我应该如何找回它?

这是我的代码。

Button createBtn;
Button searchBtn;
Button myGroupBtn;
Button settingBtn;


String[] courses;
List<String> listItems;
ArrayAdapter<String> adapter;
ListView listView;
TextView textView;
// when CREAT button is tapped
public void createBtn(View view){
    Intent i = new Intent(getApplicationContext(), Create.class);
    // Removes animation
    i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(i);
}
// when Setting button is tapped
public void settingBtn(View view) {
    Intent i = new Intent(getApplicationContext(), Setting.class);
    // Removes animation
    i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(i);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    String courseName = intent.getStringExtra("courseName");
    String courseNumber = intent.getStringExtra("courseNumber");

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);
    // Making Links to Buttons on Create
    createBtn = (Button) findViewById(R.id.createBtn);
    searchBtn = (Button) findViewById(R.id.searchBtn);
    myGroupBtn = (Button) findViewById(R.id.myGroupBtn);
    settingBtn = (Button) findViewById(R.id.settingBtn);


    //Chaning the button colors
    searchBtn.setTextColor(0xFFFFFFFF);
    createBtn.setTextColor(0xFFBFBFBF);
    myGroupBtn.setTextColor(0xFFBFBFBF);
    settingBtn.setTextColor(0xFFBFBFBF);

    listView= (ListView)findViewById(R.id.listView);
    textView= (TextView)findViewById(R.id.textView2);
    textView.setText(courseName + " " + courseNumber);

    listItems = new ArrayList<>();


    ParseQuery<ParseObject> roomQuery = ParseQuery.getQuery("Room");
    roomQuery.whereEqualTo("course" , courseName);
    roomQuery.whereEqualTo("number" , courseNumber);

    roomQuery.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> objects, ParseException e) {

            if (e == null) {
                for (ParseObject room : objects) {

                    Log.i("Appinfo", String.valueOf(room.get("title")));
                    String stringToAdd = "";

                    String opened = String.valueOf(room.get("opened"));
                    String x;
                    if(opened.equals(true)){
                        x = "Open";
                    }else{
                        x = "Closed";
                    }

                    stringToAdd = stringToAdd + String.valueOf(room.get("studyDate")) + "   " +
                            String.valueOf(room.get("category")) + "    " + x + "\n"
                            + String.valueOf(room.get("title")) +
                            "            "
                    ;
                    listItems.add(stringToAdd);
                    Log.i("Appinfo", "A");
                }


            } else {
                Log.i("Appinfo", "B");
                e.printStackTrace();
            }

        }
    });
    initList();

}
public void initList() {

    Log.i("Appinfo", "C");



    adapter = new ArrayAdapter<String>(this, R.layout.list_two, R.id.txtvw, listItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);


}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Intent intent = new Intent(getApplicationContext(), Room.class);
    String category = listItems.get(position);
}

}

1 个答案:

答案 0 :(得分:0)

你的问题是你在&#34; objectId&#34;中使用了AdapterStrings:你使用Adapter和基本的android new ArrayAdapter<String>(this, R.layout.list_two, R.id.txtvw, listItems);(那里:String)显示你的元素;问题是将复杂的Room对象转换为objectId并且不包含大量信息会使您放弃要保留的Adapter

您的问题的解决方案是一个名为Custom Adapter的非常Android-ish模式,或者更具体地针对此案例Room,它将向设备解释如何将ListView元素呈现到您应用的所有单元格getCount()

这是非常好的记录(更好的是我可以在这篇文章中写的所有内容)所以here是通用官方文档,对我来说,the best tutorial来获取概念。< / p>

PS:对于初学者,我推荐Base Adapter,对我来说,这是最简单的:)