如何使用位于 parse.com 上的字符串为 listView 创建一个数组?

How can I do an array for a listView with Strings located on parse.com?

我有这个字符串数组只是为了测试列表视图

  <string-array name="prueba">

        <item> calculo </item>
        <item> fisica </item>
        <item>logica</item>
        <item>programacion</item>
        <item> adsad</item>
        <item>asdasdasdsad</item>
        <item> trtr </item>
        <item> yuuyu </item>
        <item>ngngn</item>
        <item>dgdg</item>
        <item> liuj</item>
        <item>kmbbm</item>

    </string-array>

这是列表视图代码

public class Grades extends ListFragment implements AdapterView.OnItemClickListener {    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.grades_fragment,container,false);    
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.prueba, android.R.layout.simple_list_item_1);
        setListAdapter(adapter);
        getListView().setOnItemClickListener(this);
    }    

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {    

        Toast.makeText(getActivity(),"item" + position , Toast.LENGTH_SHORT).show();

    }
}

所以我想要的是不使用 "test" 数组,而是使用位于 Parse.com 数据上的一些字符串,在将字符串添加到数据时将元素添加到数组以 Parse.com

为基地

我该怎么做?

您需要从保存时使用的同名对象中获取字符串。比方说它是 listStrings.

ParseQuery<ParseObject> myObject= ParseQuery.getQuery("listStrings");

之后调用这个函数

myObject.fetchInBackground(new GetCallback<ParseObject>() {
  public void done(ParseObject object, ParseException e) {
   if (e == null) {
     // Assign the values here and use it accordingly
   } else {
     // Failure!
  }
}
});

您还可以使用以下功能:

fetchAllInBackground(List<T> objects)

有一些关于如何从 parse.com here -> android_guide#objects 检索数据的信息 使用 ParseQuery,您可以获得 ParseObject。 然后您可以查询此对象的详细信息并将其添加到您的列表视图中。

以上 link 中的代码片段。

获取对象。

ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
query.getInBackground("xWMyZ4YEGZ", new GetCallback<ParseObject>() {
  public void done(ParseObject object, ParseException e) {
    if (e == null) {
      // object will be your game score
    } else {
      // something went wrong
    }
  }
});

查询对象:

int score = gameScore.getInt("score");
String playerName = gameScore.getString("playerName");
boolean cheatMode = gameScore.getBoolean("cheatMode");

它们有 "getX" 种适用于所有数据类型的方法。

将此添加到您的列表视图。 使用 arrayadapter

void add(T object)

Adds the specified object at the end of the array.

void addAll(Collection collection)

Adds the specified Collection at the end of the array.

void addAll(T... items)

Adds the specified items at the end of the array.

这个 post here 展示了如何使用它。