我已经尝试了一段时间,用来自sql lite数据库的数据填充图形视图。我相信我拥有代码中所需的一切。但我不知道如何制定onItemClick方法。我有一个组的列表视图,我想从选定的组中获取数据并将其显示在图形上。这都是在同一屏幕上发生的
public class Main_graph_activity extends AppCompatActivity {
PieChart pieChart;
Long k;
int j;
FloatingActionButton fb;
String j1;
TextView noDet;
SQLiteDatabase dbgroup=null;
FloatingActionButton f;
ArrayList<String> notes = new ArrayList<String>();
ArrayList<String> list_item = new ArrayList<>();
Vector<Integer> vector = new Vector<>();
Vector<Integer> vec = new Vector<>();
int count=0;
add_adapter adapter;
List<additem> niitemlist;
ArrayAdapter adapter1;
ListView listView;
ListView listView1;
private ArrayList<PieEntry> yValues;
String p;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_graph_activity);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
fillActivity();
setTitle("Graphs");
listView=(ListView) findViewById(R.id.list_below_2);
pieChart = (PieChart) findViewById(R.id.piechart);
Legend legend = pieChart.getLegend();
legend.setTextColor(Color.WHITE);
legend.setTextSize(15f);
pieChart.setUsePercentValues(true);
pieChart.getDescription().setEnabled(false);
pieChart.setExtraOffsets(5,10,5,5);
pieChart.setDragDecelerationFrictionCoef(0.15f);
pieChart.setDrawHoleEnabled(true);
pieChart.setHoleColor(Color.WHITE);
pieChart.setTransparentCircleRadius(61f);
//ArrayList<PieEntry> yValues = new ArrayList<>();
yValues = new ArrayList<>();
//plotter();
// Description description = new Description();
//description.setText(" This graph shows all bills");
//description.setTextSize(15);
//pieChart.setDescription(description);
pieChart.animateY(1000, Easing.EasingOption.EaseInOutCubic);
PieDataSet dataSet = new PieDataSet(yValues, ": Countries");
dataSet.setSliceSpace(3f);
dataSet.setSelectionShift(5f);
dataSet.setColors(ColorTemplate.VORDIPLOM_COLORS);
PieData data = new PieData((dataSet));
data.setValueTextSize(20f);
data.setValueTextColor(Color.YELLOW);
pieChart.setData(data);
//this is code to click into different groups
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView temp = (TextView) view.findViewById(R.id.name);
String str = temp.getText().toString();
Intent in = new Intent(getApplicationContext(), Main_graph_activity.class);
in.putExtra("id", str);
startActivity(in);
}
}
);
}
//method to populate graph
public void plotter() {
dbgroup=this.openOrCreateDatabase("grouptwo.db", Context.MODE_PRIVATE, null);
// Intent in=Main_graph_activity.this.getIntent();
// Intent in = getIntent();
// Bundle bundle = in.getExtras();
// p = bundle.getString("id");
//Bundle p = getIntent().getExtras();
// if (p != null)
//{
//bundle.getString("id");
// }
dbgroup.execSQL("create table if not exists " + p + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,friend_name TEXT,note TEXT,amount TEXT)");
Cursor c = dbgroup.rawQuery("SELECT * FROM " + p + ";", null);
if (c != null) {
int i = 0;
if (c.moveToFirst()) {
do {
//create list view from table group_name
String d2 = c.getString(c.getColumnIndex("friend_name"));
String d1 = c.getString(c.getColumnIndex("note"));
String d4 = c.getString(c.getColumnIndex("amount"));
vec.add(1);
yValues.add(new PieEntry(Integer.parseInt(d4), d2));
//niitemlist.add(new item(d1,d4,d2));
pieChart = (PieChart) findViewById(R.id.piechart);
//add to chart...
} while (c.moveToNext());
pieChart.getData();
}
}
}
//function to fill up the activitys list view to show groups to click
void fillActivity() {
dbgroup = openOrCreateDatabase("grouptwo.db", Context.MODE_PRIVATE, null);
niitemlist = new ArrayList<>();
listView =(ListView) findViewById(R.id.list_below_2);
dbgroup.execSQL("CREATE TABLE IF NOT EXISTS Group_details (id INTEGER PRIMARY KEY AUTOINCREMENT ,group_name TEXT NOT NULL, date_go DATE NOT NULL, " + "friend_no INTEGER NOT NULL DEFAULT 0)");
Cursor c = dbgroup.rawQuery("SELECT * FROM Group_details ORDER BY date_go DESC;", null);
if (c != null) {
if (c.moveToFirst())
do {
String d4 = c.getString(c.getColumnIndex("group_name"));
final Long d1 = c.getLong((c.getColumnIndex("date_go")));
String d2 = c.getString(c.getColumnIndex("date_go"));
String temp = d4;
niitemlist.add(new additem(d4, d2));
vec.add(1);
j++;
} while (c.moveToNext());
}
adapter = new add_adapter(getApplicationContext(), R.layout.add_text, niitemlist);
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
}