我从parse.com Android开发者指南中获取了此代码:
@Override
public View getItemView(ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.adapter_item, null);
}
// Take advantage of ParseQueryAdapter's getItemView logic for
// populating the main TextView/ImageView.
// The IDs in your custom layout must match what ParseQueryAdapter expects
// if it will be populating a TextView or ImageView for you.
super.getItemView(object, v, parent);
// Do additional configuration before returning the View.
TextView descriptionView = (TextView) v.findViewById(R.id.description);
descriptionView.setText(object.getString("description"));
return v;
}
但是,(ParseObject object, View v, ViewGroup parent)
有三个错误:
Cannot resolve symbol "Object"
Cannot resolve symbol "v"
Cannot resolve symbol"parent"
当然,我知道我必须在某处定义object,v和parent。但是哪里?什么是PaseObject object
,View v
,ViewGroup parent
?我如何以及在何处定义它们?
我在我的应用程序中正在做的是:
description
textView来自定义它,并使字体大小更大。我添加了布局和一切。现在我想要代码。那么,基本上,我在哪里以及如何定义(ParseObject object, View v, ViewGroup parent)
?
注意:我有解析库和所有内容;我可以使用ParseObject等
编辑:Build.Gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.alibdeir.signupactivity"
minSdkVersion 17
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:23.0.+"
compile 'com.parse.bolts:bolts-android:1.+'
compile 'com.parse:parse-android:1.+'
compile 'com.android.support:design:23.+'
compile 'com.baoyz.swipemenulistview:library:1.3.0'
compile 'com.parse:parseui-widget-android:0.0.1'
compile 'com.android.support:recyclerview-v7:23.0.0-rc1'
}
另一个编辑:我知道我犯的是一个非常蹩脚的错误,但是,如果你有答案,请回答。即使你必须详细说明。这将是伟大的如果我可以完成我正在努力的应用程序的这个屏幕 完整来源:
package com.alibdeir.signupactivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.parse.FindCallback;
import com.parse.GetDataCallback;
import com.parse.LogOutCallback;
import com.parse.ParseClassName;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseImageView;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseQueryAdapter;
import com.parse.ParseUser;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ParseUser user = ParseUser.getCurrentUser();
private Dialog progressDialog;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//region emailNotVerified
if (user.getBoolean("emailVerified") == false) {
Intent goBackToLogin = new Intent(MainActivity.this, LoginActivity.class);
MainActivity.this.startActivity(goBackToLogin);
Toast.makeText(getApplicationContext(), "Please verify your email", Toast.LENGTH_SHORT).show();
finish();
}
//endregion
else {
setContentView(R.layout.activity_main);
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
list = (ListView) findViewById(R.id.list);
ParseQueryAdapter<ParseObject> titles = new ParseQueryAdapter<ParseObject>(this, "Campaign");
titles.setTextKey("Title");
titles.setImageKey("Picture");
}
@Override
public View getItemView(ParseObject object, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_single, null);
}
super.getItemView(meal, v, parent);
ParseImageView mealImage = (ParseImageView) v.findViewById(R.id.icon);
ParseFile photoFile = meal.getParseFile("photo");
if (photoFile != null) {
mealImage.setParseFile(photoFile);
mealImage.loadInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
// nothing to do
}
});
}
TextView titleTextView = (TextView) v.findViewById(R.id.text1);
titleTextView.setText(meal.getTitle());
TextView ratingTextView = (TextView) v
.findViewById(R.id.favorite_meal_rating);
ratingTextView.setText(meal.getRating());
return v;
}
// Take advantage of ParseQueryAdapter's getItemView logic for
// populating the main TextView/ImageView.
}
// Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
// setSupportActionBar(myToolbar);
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// MenuInflater menuInflater = getMenuInflater();
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.action_logout:
openAlert();
break;
}
return super.onOptionsItemSelected(item);
}
private void openAlert() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle("Confirm your logout")
.setMessage("Are you sure you want to log out?");
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Positive OnClick to log out
showProgressBar("Logging Out");
ParseUser.logOutInBackground(new LogOutCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
//Success
dismissProgressBar();
Intent startLogin = new Intent(MainActivity.this, LoginActivity.class);
MainActivity.this.startActivity(startLogin);
finish();
} else {
//Sign up did not succeed.
dismissProgressBar();
Toast.makeText(getApplicationContext(), "An unknown error occurred while logging out", Toast.LENGTH_SHORT).show();
}
}
});
ParseUser currentUser = ParseUser.getCurrentUser();
}
});
alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Cancel
}
});
alertDialogBuilder.setCancelable(false);
AlertDialog alertDialog = alertDialogBuilder.create();
// show alert
alertDialog.show();
}
public void showProgressBar(String msg) {
progressDialog = ProgressDialog.show(this, "Logging out", "Loading", true);
}
public void dismissProgressBar() {
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
}
}
**Don't worry about the file names and IDs, they are all correct**
答案 0 :(得分:0)
在方法定义中已经定义了变量,例如这样:
public View getItemView(ParseObject object, View v, ViewGroup parent)
是声明这些变量的内容。如果您收到的错误是未定义的,则必须是因为您尝试从getItemView()方法外部访问它们,或者在实际代码中重命名它们。
如果您的更新显示了定义此功能的类的完整源代码,则问题很明显:
您尝试覆盖的getItemView
方法不是AppCompatActivity
定义的方法 - 它是ParseQueryAdapter
的方法。我不知道你是如何在这种情况下结束的,但这种方法在该课程中无法奏效。必须将其移至继承自ParseQueryAdapter
。
所以,你需要有一个类,让我们说它是一个Campaign,你需要用你的ParseQueryAdapter
子类化这样的东西:
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.parse.GetDataCallback;
import com.parse.ParseClassName;
import com.parse.ParseException;
import com.parse.ParseFile;
import com.parse.ParseImageView;
import com.parse.ParseObject;
import com.parse.ParseUser;
/**
* Created by AbAppletic on 12/15/2015.
*/
@ParseClassName("Campaign")
public class Campaign extends ParseObject {
public Campaign() {
// default Constructor
}
public String getTitle() {
return getString("Title");
//get the title
}
public String getDescription(){
//get the description
return getString("Description");
}
public ParseUser getAuthor() {
// get the author
return getParseUser("Owner");
}
public ParseFile getImage() {
//get the image
return getParseFile("Picture");
}
}
而且,现在,你必须创建一个新的类
ParseQueryAdapter<Campaign>
// Campaign is the class we previously created
因此,在您的情况下,您将在此处设置自定义布局中的图像,说明和标题:
public class mainAdapter extends ParseQueryAdapter<Campaign> {
public mainAdapter(Context context){
super(context,new ParseQueryAdapter.QueryFactory<Campaign>(){
public ParseQuery<Campaign> create(){
ParseQuery query = new ParseQuery("Campaign");
query.orderByDescending("updatedAt");
return query;
}
});
}
@Override
public View getItemView(Campaign campaign, View v, ViewGroup parent) {
if (v == null) {
v = View.inflate(getContext(), R.layout.list_single, null);
}
super.getItemView(campaign, v, parent);
ParseImageView campaignImage = (ParseImageView) v.findViewById(R.id.icon);
ParseFile photoFile = campaign.getParseFile("Picture");
if (photoFile != null) {
campaignImage.setParseFile(photoFile);
campaignImage.loadInBackground(new GetDataCallback() {
@Override
public void done(byte[] data, ParseException e) {
// nothing to do
}
});
}
TextView titleTextView = (TextView) v.findViewById(R.id.title);
titleTextView.setText(campaign.getTitle());
TextView descriptionTextView = (TextView) v
.findViewById(R.id.description);
descriptionTextView.setText(campaign.getDescription());
return v;
}
} 现在,在您的MainActivity中: 定义这个:
private mainAdapter adapter;
// mainAdapter is the name of the class we just created that extends ParseQueryAdapter
然后,在onCreate()
中,只需将适配器设置为listView:
adapter = new mainAdapter(this);
list.setAdapter(adapter);
在Parse.initialize
之上:
ParseObject.registerSubclass(Campaign.class);
Campaign是我们刚刚创建的第一个类。