尝试使用带有 Parse.com 的 ParseQuery 的 RecyclerView 从数据库中提取数据时出现两个编译错误
Getting two compilation errors when trying to use a RecyclerView with Parse.com's ParseQuery to pull data from database
我在使用 RecyclerView
和 ParseQuery
时遇到问题;我的 MainActvity 中的 .get(i)
一直给我这些错误:
Error:(102, 67) error: incompatible types: int cannot be converted to String
和:
non-static method 'get(java.lang.String)' cannot be referenced from a static context
和:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
我做了很多研究,但关于 RecyclerView
与 ParseQuery
一起使用的信息并不多
这是我的 MainActivity:
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class MainActivity extends AppCompatActivity implements WeatherServiceCallback {
// Defining Variables
private Toolbar toolbar;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
protected Button mButtonHeader;
private TextView mDayOfTheWeek;
private TextView mDate;
// protected Context context;
// private long pageId = 137317903012386L;
// private List<Data> data;
private RecyclerView mRecyclerView;
// New Variables
private CustomAdapter mAdapter;
private Context context;
// Weather variables
private ImageView weatherIconImageView;
private TextView temperatureTextView;
private TextView conditionTextView;
private TextView locationTextView;
private YahooWeatherService service;
private ProgressDialog dialog;
final List<Information> data = new ArrayList<>();
Information current = new Information();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new CustomAdapter(getApplicationContext(), data);
//RecyclerView with Parse
Parse.initialize(this, "9eLoLJ1NvoDiW8kv9VIWspFrukz5imtvASpxLRwV", "ThOQN59Uulim5YgFfYQ9LwH5NTX6bKahbbfjTcuv");
ParseQuery<ParseObject> query = ParseQuery.getQuery("news");
query.findInBackground( new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
Information information = new Information();
information.mNewstTitle = ParseObject.get(i).getString("name");
information.mNewsStory = ParseObject.get(i).getString("shortinfo");
//information.partyCost = parseObject.getString("partyName");
//information.flyerPic = parseObject.getParseFile("partyFlyerImage");
//information.partyPromoterPic = parseObject.getParseFile("partyPromoterImage");
//information.mNewsPhotoIcon = R.drawable.newsitem));
data.add(information);
}
} else {
// something went wrong
}
mRecyclerView.setAdapter(mAdapter);
}
});
}
}
这是我的 Class 以及我的适配器的数据:
public class Information {
String mNewstTitle;
String mNewsStory;
int mNewsPhotoIcon;
/*
* Information(String mNewstTitle, String mNewsStory, int mNewsPhotoIcon) {
* this.mNewstTitle = mNewstTitle; this.mNewsStory = mNewsStory;
* this.mNewsPhotoIcon = mNewsPhotoIcon;
* }
*/
}
您正在静态调用方法:
ParseObject.get(i)...
If you don't know what type of data you're getting out, you can call get(key), but then you probably have to cast it right away anyways. In most situations you should use the typed accessors like getString.
正确:
<instance>.get()
不正确:
<class>.get()
我在使用 RecyclerView
和 ParseQuery
时遇到问题;我的 MainActvity 中的 .get(i)
一直给我这些错误:
Error:(102, 67) error: incompatible types: int cannot be converted to String
和:
non-static method 'get(java.lang.String)' cannot be referenced from a static context
和:
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
我做了很多研究,但关于 RecyclerView
与 ParseQuery
这是我的 MainActivity:
import com.parse.FindCallback;
import com.parse.GetCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
public class MainActivity extends AppCompatActivity implements WeatherServiceCallback {
// Defining Variables
private Toolbar toolbar;
private NavigationView navigationView;
private DrawerLayout drawerLayout;
protected Button mButtonHeader;
private TextView mDayOfTheWeek;
private TextView mDate;
// protected Context context;
// private long pageId = 137317903012386L;
// private List<Data> data;
private RecyclerView mRecyclerView;
// New Variables
private CustomAdapter mAdapter;
private Context context;
// Weather variables
private ImageView weatherIconImageView;
private TextView temperatureTextView;
private TextView conditionTextView;
private TextView locationTextView;
private YahooWeatherService service;
private ProgressDialog dialog;
final List<Information> data = new ArrayList<>();
Information current = new Information();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new CustomAdapter(getApplicationContext(), data);
//RecyclerView with Parse
Parse.initialize(this, "9eLoLJ1NvoDiW8kv9VIWspFrukz5imtvASpxLRwV", "ThOQN59Uulim5YgFfYQ9LwH5NTX6bKahbbfjTcuv");
ParseQuery<ParseObject> query = ParseQuery.getQuery("news");
query.findInBackground( new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (int i = 0; i < objects.size(); i++) {
Information information = new Information();
information.mNewstTitle = ParseObject.get(i).getString("name");
information.mNewsStory = ParseObject.get(i).getString("shortinfo");
//information.partyCost = parseObject.getString("partyName");
//information.flyerPic = parseObject.getParseFile("partyFlyerImage");
//information.partyPromoterPic = parseObject.getParseFile("partyPromoterImage");
//information.mNewsPhotoIcon = R.drawable.newsitem));
data.add(information);
}
} else {
// something went wrong
}
mRecyclerView.setAdapter(mAdapter);
}
});
}
}
这是我的 Class 以及我的适配器的数据:
public class Information {
String mNewstTitle;
String mNewsStory;
int mNewsPhotoIcon;
/*
* Information(String mNewstTitle, String mNewsStory, int mNewsPhotoIcon) {
* this.mNewstTitle = mNewstTitle; this.mNewsStory = mNewsStory;
* this.mNewsPhotoIcon = mNewsPhotoIcon;
* }
*/
}
您正在静态调用方法:
ParseObject.get(i)...
If you don't know what type of data you're getting out, you can call get(key), but then you probably have to cast it right away anyways. In most situations you should use the typed accessors like getString.
正确:
<instance>.get()
不正确:
<class>.get()