您好,登錄后才能下訂單哦!
package com.chencheng.meituan.view;
import java.util.ArrayList;
import java.util.List;
import com.android.volley.VolleyError;
import com.chencheng.meituan.util.MeituanConstants;
import com.chencheng.model.detail.XComment;
import com.chencheng.model.detail.XItem;
import com.chencheng.model.detail.Xiangqing;
import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.utils.Legend;
import com.github.mikephil.charting.utils.Legend.LegendPosition;
import com.github.mikephil.charting.utils.XLabels;
import com.github.mikephil.charting.utils.XLabels.XLabelPosition;
import com.github.mikephil.charting.utils.YLabels;
import com.github.mikephil.charting.utils.YLabels.YLabelPosition;
import com.github.volley.MyVolley;
import com.github.volley.VolleyListener;
import com.xinbo.utils.GsonUtils;
import com.xinbo.widget.ListView4ScrollView;
import com.yuchen.meituan.R;
import com.yuchen.meituan.R.id;
import com.yuchen.meituan.R.layout;
import com.yuchen.meituan.R.menu;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.SyncStateContract.Constants;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CommentActivity extends Activity implements OnClickListener {
protected String[] mLevels = new String[] { "1星", "2星", "3星", "4星", "5星", };
private List<XItem> mCommetnList = new ArrayList<XItem>();
private HorizontalBarChart mChart;
private MyListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comment);
initUI();
initChart();
getJson();
}
private void getJson() {
MyVolley.get(this, MeituanConstants.UrlPath.XIANGQING, new VolleyListener() {
@Override
public void onErrorResponse(VolleyError arg0) {
arg0.getMessage();
}
@Override
public void onResponse(String arg0) {
parseJson(arg0);
}
});
}
protected void parseJson(String arg0) {
Xiangqing xiangqingInfo = GsonUtils.parseJSON(arg0, Xiangqing.class);
XComment comment = xiangqingInfo.getComment();
mCommetnList.addAll(comment.getItems());
mAdapter.notifyDataSetChanged();
}
private void initUI() {
findViewById(R.id.back_btn).setOnClickListener(this);
ListView4ScrollView mListView = (ListView4ScrollView) findViewById(R.id.listView1);
mAdapter = new MyListAdapter();
mListView.setAdapter(mAdapter);
}
class MyListAdapter extends BaseAdapter{
@Override
public View getView(int position, View convertView, ViewGroup parent) {
XItem item = mCommetnList.get(position);
View view = getLayoutInflater().inflate(
R.layout.listview_comment_item, null);
TextView tvUser = (TextView) view.findViewById(R.id.tv_user);
TextView tvTime = (TextView) view.findViewById(R.id.tv_time);
TextView tvContent = (TextView) view.findViewById(R.id.tv_content);
tvUser.setText(item.getUser_id());
tvTime.setText(item.getAdd_time());
tvContent.setText(item.getContent());
return view;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mCommetnList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
private void initChart() {
mChart = (HorizontalBarChart) findViewById(R.id.chart1);
mChart.setDrawBarShadow(false);
mChart.setDrawYValues(true);
mChart.setDrawValueAboveBar(false);
mChart.setDescription("");
mChart.setMaxVisibleValueCount(60);
mChart.set3DEnabled(false);
mChart.setPinchZoom(false);
mChart.setUnit(" ");
mChart.setDrawGridBackground(false);
mChart.setDrawHorizontalGrid(true);
mChart.setDrawVerticalGrid(false);
mChart.setValueTextSize(10f);
mChart.setDrawBorder(false);
//設置是否可以觸摸,如為false,則不能拖動,縮放等
mChart.setTouchEnabled(false);
// Typeface tf = Typeface.createFromAsset(getAssets(),
// "OpenSans-Regular.ttf");
XLabels xl = mChart.getXLabels();
xl.setPosition(XLabelPosition.BOTTOM);
xl.setCenterXLabelText(true);
// xl.setSpaceBetweenLabels(30); // 設置數據之間的間距
// xl.setTypeface(tf);
YLabels yl = mChart.getYLabels();
// yl.setTypeface(tf);
yl.setLabelCount(8);
yl.setPosition(YLabelPosition.BOTH_SIDED);
// yl.setLabelCount(5); // 設置Y軸最多顯示的數據個數
// mChart.setValueTypeface(tf);
setData(5, 10);
Legend l = mChart.getLegend();
l.setPosition(LegendPosition.BELOW_CHART_LEFT);
l.setFormSize(8f);
l.setXEntrySpace(4f);
}
private void setData(int count, float range) {
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < count; i++) {
xVals.add(mLevels[i]);
}
ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
for (int i = 0; i < count; i++) {
float mult = (range + 1);
float val = (float) (Math.random() * 40);
yVals1.add(new BarEntry(val, i));
}
BarDataSet set1 = new BarDataSet(yVals1, "水平分布圖");
set1.setBarSpacePercent(35f);
set1.setColor(Color.rgb(252, 131, 2));//設置曲線的顏色
ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
dataSets.add(set1);
BarData data = new BarData(xVals, dataSets);
mChart.setData(data);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.back_btn:
finish();
break;
default:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.comment, menu);
return false;
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。