我知道这与link question
我也遵循了相同的规则但是我收到了以下错误:
@Override
public void onValueSelected(Entry e, Highlight h) {
String xAxisValue = mChart.getData().getXVals().get(e.getXIndex());
}
错误:无法解析getXVals()而无法解析getXIndex()
请帮助:我有以下x轴值:印度,澳大利亚,美国,中国
点击条形图中的栏我需要获得上述字符串之一
答案 0 :(得分:3)
在MPAndroidChart 3.0.0+这应该有效:
chart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, Highlight h) {
chart.getXAxis().getValueFormatter().getFormattedValue(e.getX(), chart.getXAxis());
}
@Override
public void onNothingSelected() {
}
});
答案 1 :(得分:0)
如果您使用模型类绘制图表,当您单击条形图时,想要获得更多值而不仅仅是Y轴点或x轴名称,那么下面是解决方案。
首先你的活动或片段应该实现OnChartValueSelectedListener。然后实现onValueSelected()
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
String xAxisValue = chart.getData().getXVals().get(e.getXIndex());
//yourList (model class or pojo class type), first get the full list
for (int i=0; i<yourList.size(); i++){
YourModelClass cym = yourList.get(i);
String year =cym.getYear();
// compare the xAxisValue with one of your Model class attribute. which is written on x-axis of the graph
if (year.equals(xAxisValue)){
// now you got the index number of your list which is matched with xAxis Bar value.
//so do your stuff here. Something like setting text or showing toast etc etc
// if done everything then break the loop
break;
}
}
}
@Override
public void onNothingSelected() {
}
答案 2 :(得分:0)
#include <iostream>
#include <windows.h>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <string>
using namespace std;
//int rand_0toN1(int F);
int main()
{
bool gameloop = true;
int F = 10, W = 10; //F = Food Level -- W = Water Level
int BegS, PG; //BegS = Begging Skills -- PG = People Giving choices
double thieveN, thieveI; //Thieve = Thievery -- thieveI = items you steal
int D = 0; // Number of days survived
int MC = 0; // Money checker, if you earn money then additional commands will be added so you can use money
int M = 0; // Amount of money
string c, sc, thieve; //C = Choice -- SC = Seacond Choice -- thieves = thievery strings
srand(time(NULL)); //set seed for random numberss
BegS = (rand() % 20) + 1;
thieveN = (rand() % 20) + 1;
thieveI = (rand() % 4) + 1;
PG = (rand() % 2) + 1;
cout << "Hello There, Lets get straight to the point. You are homeless in the streets of New York\n";
cout << "Yeah it sucks I know\n";
cout << "You will have to survive the streets\n";
cout << "If you ever need help with the commands of this game please type help\n";
cout << "\n";
cin >> c;
while (gameloop = true) {
if (c == "help") {
cout << "\n";
cout << "Here are the basic commands of the Game\n";
Sleep(50);
cout << "beg--To Beg what did you expect\n";
Sleep(50);
cout << "levels--To Check your hunger and water levels (if any of your levels reaches 0 you DIE!!!)\n";
Sleep(50);
cout << "steal--To Steal (will open a window for more commands)\n";
Sleep(50);
cout << "die--this command will kill your character \n";
cin >> c;
}
if (c == "die") {
F = 1;
}
if (c == "levels") {
cout << "\n";
cout << "Your Water level is " << W << endl;
cout << "Your Hunger level is " << F << endl;
cout << "You Have currently survived for " << D << " days\n";
cout << "\n";
cin >> c;
}
if (c == "levels" && MC == 1) {
cout << "\n";
cout << "Your Water level is " << W << endl;
cout << "Your Hunger level is " << F << endl;
cout << "You Have currently survived for " << D << " days\n";
cout << "Your money totals up to " << M << endl;
cout << "\n";
cin >> c;
}
if (c == "beg" && BegS >= 18) { //10% chance
cout << "Your Beggging Payed of and A Kind Person Gave You Water and Food\n";
cout << "\n";
BegS = (rand() % 10) + 1;
D++;
cin >> c;
}
if (c == "beg" && BegS >= 13 && BegS <= 17 && PG == 1) { // 20% chance
cout << "Your Beggging Payed of and A Kind Person Gave You Food\n";
cout << "\n";
BegS = (rand() % 20) + 1;
PG = (rand() % 2) + 1;
D++;
W--;
cin >> c;
}
if (c == "beg" && BegS >= 13 && BegS <= 17 && PG == 2) { // 20% chance
cout << "Your Beggging Payed of and A Kind Person Gave You Water\n";
cout << "\n";
BegS = (rand() % 20) + 1;
PG = (rand() % 2) + 1;
D++;
F--;
cin >> c;
}
if (c == "beg" && BegS >= 14 && BegS <= 17) { // 15% chance
cout << "A Kind Person came by you and game you some money\n";
cout << "(you can use money to buy either food or water by using the \"buy water/food\" command\n";
cout << "\n";
BegS = (rand() % 20) + 1;
PG = (rand() % 2) + 1;
D++;
F--;
W--;
MC = 1;
M++;
cin >> c;
}
if (c == "beg" && BegS >= 3 && BegS <= 13) { // 50% chance
cout << "Sadly your begging did not get you anything\n";
cout << "\n";
BegS = (rand() % 20) + 1;
PG = (rand() % 2) + 1;
D++;
F--;
W--;
cin >> c;
}
//-------------Specialty Items----------------
if (c == "beg" && BegS == 2) { // 5% chance
cout << "You get noticed by some person who gives you a soda\n";
cout << "(Items like Soda gives you +2 Water)\n";
cout << "\n";
BegS = (rand() % 20) + 1;
PG = (rand() % 2) + 1;
D++;
F--;
W += 2;
cin >> c;
}
if (c == "beg" && BegS == 1) { // 5% chance
cout << "You get noticed by some person who gives you a Pizza\n";
cout << "(Items like Pizza gives you +2 Food)\n";
cout << "\n";
BegS = (rand() % 20) + 1;
PG = (rand() % 2) + 1;
D++;
F += 2;
W--;
cin >> c;
}
//-----------------Thievery--------------------
if (c == "steal") {
cout << "What would you like to steal from? (gas station-1)\n";
cin >> thieve;
if (thieve == "1" && thieveN >= 12 && thieveN <= 16) { // 30% chance
cout << "You have chosen the gas station\n";
cout << "\n";
cout << "You manage to sneak a waterbottle into your pocket\n";
thieveN = (rand() % 20) + 1;
cout << "\n";
cout << "Would you Like to test your chances and steal something else?\n";
cin >> thieve;
}
if (thieve == "1" && thieveN >= 17 && thieveN <= 20) { // 30% chance
cout << "You manage to sneak a sandwich into your pocket\n";
thieveN = (rand() % 20) + 1;
cout << "\n";
cout << "Would you Like to test your chances and steal something else?\n";
cin >> thieve;
}
if (thieve == "1" && thieveN >= 1 && thieveN <= 11) { // 40% chance
cout << "You get noticed trying to sneak food into your pocket and get arrested\n";
cout << "\n";
cout << "GAME OVER\n";
cout << "You Survived " << D << " Days\n";
Sleep(1250);
cout << "Would you like to start over? (yes/ no)\n";
cin >> sc;
if (sc == "no" || "n") {
cout << "Thankyou For Playing Hobo Life\n";
gameloop = false;
break;
}
if (sc == "yes" || "n") {
cout << "You find yourself on a park bench, its early morning. Better start begging\n";
cin >> c;
}
else {
cout << "Invalid Input\n";
cin >> sc;
}
}
}
//-------------Invalid Input-------------------
if (c != "beg" && c != "levels" && c != "steal" && thieve != "1" && c != "die") {
cout << "Invalid Input\n";
cout << "\n";
cin >> c;
}
//-------------------Death---------------------
if (F == 1 || W == 1) {
cout << "You died\n";
cout << "You Survived " << D << " Days\n";
Sleep(1250);
cout << "Would you like to start over? (yes/ no)\n";
cin >> sc;
if (sc == "no" || "n") {
cout << "Thankyou For Playing Hobo Life\n";
gameloop = false;
}
if (sc == "yes" || "y"){
gameloop = true;
break;
}
else {
cout << "Invalid Input\n";
cout << "\n";
cin >> sc;
}
}
}
return 0;
}