我正在尝试将字符串放入解析中,然后将它们放入CardView项目中。 它没有用......
错误:您必须使用ParseObject.create()或正确的子类创建此类型的ParseObject。
ListViewAdapter:
public class ListViewAdapter extends ArrayAdapter<Post> {
Context context;
int layoutResourceId;
ArrayList<Post> data = new ArrayList<Post>();
public ListViewAdapter(Context context, int layoutResourceId,
ArrayList<Post> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RecordHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RecordHolder();
holder.airport_name = (TextView) row.findViewById(R.id.item_airport);
holder.imageItem = (ParseImageView) row.findViewById(R.id.item_image);
holder.date = (TextView) row.findViewById(R.id.item_date);
holder.time = (TextView) row.findViewById(R.id.item_time);
holder.username = (TextView) row.findViewById(R.id.item_username);
row.setTag(holder);
} else { // recycle view if any
holder = (RecordHolder) row.getTag();
}
Post item = data.get(position);
holder.airport_name.setText(item.getAirportParse());
holder.date.setText(item.getDateParse());
holder.time.setText(item.getTimeParse());
holder.username.setText(item.getUsername());
if (item.getImageFile() != null) {
holder.imageItem.setParseFile(item.getImageFile());
} else {
holder.imageItem.setImageResource(R.drawable.rsz_add_post2);
holder.imageItem.setScaleType(ImageView.ScaleType.FIT_CENTER);
}
return row;
}
static class RecordHolder {
TextView airport_name;
TextView date;
TextView time;
TextView username;
ParseImageView imageItem;
}
}
发表:
@ParseClassName("Post")
public class Post extends ParseObject implements Serializable {
public Post(){
}
public String getId(){
return getString("objectId");
}
public void setId(String id){
put("objectId", id);
}
//////////
public String getUsername(){
return getString("username");
}
public String getAirportParse(){
return getString("airport");
}
public void setAirportParse(String airport){
put("airport", airport);
}
//////////
public String getDateParse(){
return getString("date");
}
public void setDateParse(String date){
put("date", date);
}
//////////
public String getTimeParse(){
return getString("time");
}
public void setTimeParse(String time){
put("time", time);
}
//////////
public String getPersonsParse(){
return getString("persons");
}
public void setPersonsParse(String persons){
put("persons", persons);
}
//////////
public String getAddressParse(){
return getString("address");
}
public void setAddressParse(String address){
put("address", address);
}
public String getFlightnrParse(){
return getString("flightnr");
}
public void setFlightnrParse(String flightnr){
put("flightnr", flightnr);
}
public ParseFile getImageFile(){
return getParseFile("image");
}
public void setImageFile(ParseFile file) {
if (file != null) {
put("image", file);
}
}
public List<String> getPosts(){
return getList("Post");
}
public void setIngredients(List<String> postsLists){
if (postsLists != null)
put("Post", postsLists);
}
}
FinishPostFragment:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_post_finish:
// User chose the "Favorite" action, mark the current item
// as a favorite...
final FragmentManager fm = getActivity().getFragmentManager();
final Fragment fragment = new DemandFragment();
// put strings to listview in DemandFragment
ParseObject post = new ParseObject("Post");
post.put("airport", airport_finish.getText().toString());
post.put("date", date_finish.getText().toString());
post.put("time", time_finish.getText().toString());
post.put("persons", persons_finish.getText().toString());
post.put("address", address_finish.getText().toString());
post.put("flightnr", flightnr_finish.getText().toString());
post.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
fm.beginTransaction().replace(R.id.content_main, fragment).commit();
}
});
return true;
DemandFragment:
public class DemandFragment extends Fragment {
ListView lv;
ArrayAdapter adapter;
ArrayList<Post> mijncode;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_demand, container, false);
if(rootView != null){
lv = (ListView) rootView.findViewById(R.id.listDemand);
adapter = new ListViewAdapter(getActivity(), R.layout.item_cardview, mijncode);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// clicked on item show post
FragmentManager fm = getActivity().getFragmentManager();
Fragment fragment = new ViewPostFragment();
fm.beginTransaction().replace(R.id.content_main, fragment).commit();
}
});
}
return rootView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Post");
mijncode = new ArrayList<Post>();
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> postList, ParseException e) {
if (e == null) {
for (ParseObject object : postList) {
Post newPost = new Post();
newPost.setAirportParse((String) object.get("airport"));
newPost.setDateParse((String) object.get("date"));
newPost.setTimeParse((String) object.get("time"));
newPost.setPersonsParse((String) object.get("persons"));
newPost.setAddressParse((String) object.get("address"));
newPost.setFlightnrParse((String) object.get("address"));
mijncode.add(newPost);
}
}
}
});
}
}
答案 0 :(得分:1)
根据您编写的错误,您应该通过调用ParseObject
初始化ParseObject.create()
,但您从不调用该方法。在所有情况下(在FinishPostFragment中发布,在DemandFragment中发布newPost),您调用构造函数而不是ParseObject.create()
...如果您可以提供此错误所在的行,则更容易说出哪一个导致它。< / p>
编辑:我对Parse并不熟悉,我从来不需要创建子类,Parse docs很好地解释了创建子类所需要的东西,它似乎与超级构造函数无关,我的错误。