无法从解析服务器获取数据和显示

时间:2016-02-20 08:06:46

标签: android parse-platform

我使用parse.com来存储我的数据,我想要检索某个对象并且我没有收到任何错误或异常,只是一个空的活动,我试图extends ListActivity setListAdapter()代替AppCompatActivityActivity,但我得到了不同的错误

ProblemList.java

public class ProblemList extends AppCompatActivity {
ListView listview;
List<ParseObject> ob;
ProgressDialog mProgressDialog;
ListViewAdapter adapter;
private List<Problems> problemsList = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_problem_list);
    new DataFetch().execute();
}
private class DataFetch extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(ProblemList.this);
        mProgressDialog.setTitle("Getting data from Parse.com servers");
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        problemsList=new ArrayList<>();
        try {
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>("Problem");
            ob=query.find();
            for (ParseObject problem : ob)
            {
                Problems p=new Problems();
                p.setReportDate(problem.getCreatedAt().toString());
                p.setReportUser((String) problem.get("Username"));
                p.setReportInfo((String) problem.get("problem"));
                problemsList.add(p);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        listview = (ListView) findViewById(R.id.listview);
        adapter = new ListViewAdapter(ProblemList.this, problemsList);
            listview.setAdapter(adapter);
        mProgressDialog.dismiss();
    }
}
}

listviewAdapter.java

public class ListViewAdapter extends BaseAdapter {
Context context;
LayoutInflater inflater;
private List<Problems> problemlist = null;
private ArrayList<Problems> arraylist;
public ListViewAdapter(Context context,List<Problems> problemlist) {
    this.context=context;
    this.problemlist=problemlist;
    inflater=LayoutInflater.from(context);
    this.arraylist=new ArrayList<Problems>();
    this.arraylist.addAll(problemlist);
}
@Override
public int getCount() {
    return problemlist.size();
}
@Override
public Object getItem(int position) {
    return problemlist.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
    final ViewHolder holder;
    if (view==null){
        holder=new ViewHolder();
        view = inflater.inflate(R.layout.rows, parent);
        holder.mDate= (TextView) view.findViewById(R.id.reportDate);
        holder.mProblem= (TextView) view.findViewById(R.id.reportMsg);
        holder.mUsername=(TextView) view.findViewById(R.id.reportedUser);
        view.setTag(holder);
    }
    else {
       holder= (ViewHolder) view.getTag();
    }
    holder.mDate.setText(problemlist.get(position).getReportDate());
    holder.mProblem.setText(problemlist.get(position).getReportInfo());
    holder.mUsername.setText(problemlist.get(position).getReportUser());
    return view;
}
public class ViewHolder{
    TextView mDate;
    TextView mUsername;
    TextView mProblem;
}
}

Problem.java(setter and getters)

public class Problems {
String reportUser;
String reportDate;
String reportInfo;
public String getReportUser() {
    return reportUser;
}
public void setReportUser(String reportUser) {
    this.reportUser = reportUser;
}

public String getReportDate() {
    return reportDate;
}
public void setReportDate(String reportDate) {
    this.reportDate = reportDate;
}

public String getReportInfo() {
    return reportInfo;
}

public void setReportInfo(String reportInfo) {
    this.reportInfo = reportInfo;
}
}

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
    android:id="@+id/listview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

我按照本教程here,感谢您的时间。

1 个答案:

答案 0 :(得分:1)

首先我建议使用自定义扩展ParseObject。所以ypur问题应该扩展ParseObject。 然后Parse sdk为每个请求提供两种方法。一个是asynk,另一个是synk。所以,你不应该使用AsynkTask。我在下面举了一个来自api参考的例子:

ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
query.whereEqualTo("playerName", "Dan Stemkoski");
query.findInBackground(new FindCallback<ParseObject>() {
    public void done(List<ParseObject> scoreList, ParseException e) {
        if (e == null) {
            Log.d("score", "Retrieved " + scoreList.size() + " scores");
        } else {
            Log.d("score", "Error: " + e.getMessage());
        }
    }
});

方法完成是一个回调,所有这些代码都在不同的线程上运行。

Parse向您提供简单的适配器(是的,您可以扩展它),以便在listview上显示数据。 请记住,Parse将在明年关闭。