Pie Chart Billing Case Development

Video URL: http://www.imooc.com/learn/756

 

Successfully entered the pit Android Studio

 

1. Implement the main interface layout

2.json data acquisition and parsing

3. Introduction to MPAndroidChart

 

 

 

1. Implement the main interface layout

activity_main.xml

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v4.view.ViewPager>
</RelativeLayout>

 

The first problem encountered:

android.support.v4.view.ViewPager

what is it?

After searching, it is known that it is used to display the interface of left and right sliding.

The following blog link explains how to use ViewPager in detail, as well as some good code exercises.

http://blog.csdn.net/wangjinyu501/article/details/8169924

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ViewPager vpMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        vpMain= (ViewPager) findViewById(R.id.vp_main);
        initView();
    }

    private void initView() {
        vpMain.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return PieFragment.newInstance(position+"");
            }

            @Override
            public int getCount() {
                return 4;//Return 4 means there are 4 interfaces
            }
        });
    }
}

PieFragment.java

public class PieFragment extends Fragment {
    private static final String DATA_KEY ="piefragment_data_key";
    private String mData;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        Bundle arguments = getArguments();
        if (arguments != null) {
            mData=arguments.getString(DATA_KEY);
        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
        TextView textView=new TextView(getContext());
        textView.setText(mData);
        return textView;
    }

    public static PieFragment newInstance(String data) {
        
        Bundle args = new Bundle();
        args.putString(DATA_KEY,data);
        PieFragment fragment = new PieFragment();
        fragment.setArguments(args);
        return fragment;
    }
}

These codes are really difficult for me at present, and I should understand them when I encounter them more in the future.

 

Hey, the compiler reported an error unfortunately, but I felt that my code was fine. After many setbacks, I finally found out that the Fragment package was referenced wrongly and should be referenced

import android.support.v4.app.Fragment;
The program runs successfully to the mobile phone, and the interface of android studio is very beautiful.

 

2.json data acquisition and parsing

Obtain:

MainActivity.java

private String mJson="[{\"date\":\"2016年5月\",\"obj\":
[{\"title\":\"外卖\",\"value\":34}," +
            "{\"title\":\"娱乐\",\"value\":21},
{\"title\":\"其他\",\"value\":45}]}," +
            "{\"date\":\"2016年6月\",\"obj\":
[{\"title\":\"外卖\",\"value\":32}," +
            "{\"title\":\"娱乐\",\"value\":22},
{\"title\":\"其他\",\"value\":42}]}," +
            "{\"date\":\"2016年7月\",\"obj\":
[{\"title\":\"外卖\",\"value\":34}," +
            "{\"title\":\"娱乐\",\"value\":123},
{\"title\":\"其他\",\"value\":24}]}," +
            "{\"date\":\"2016年8月\",\"obj\":
[{\"title\":\"外卖\",\"value\":145}," +
            "{\"title\":\"娱乐\",\"value\":123},
{\"title\":\"其他\",\"value\":124}]}]";

Parse:

Download gson-2.2.4.jar, copy and paste it into the libs folder, right-click gson-2.2.4.jar, click Add As Library and select OK.

New MonthBean.java

 

{"date":"2016年5月","obj":[{"title":"外卖","value":34}
 */
public class MonthBean {

    public String date;
    public ArrayList<PieBean> obj;

    @Override
    public String toString() {
        return "MonthBean{" +
                "date='" + date + '\'' +
                ", obj=" + obj.toString() +
                '}';
    }

    class PieBean {
        public String title;
        public int value;

        @Override
        public String toString() {
            return "PieBean{" +
                    "title='" + title + '\'' +
                    ", value=" + value +
                    '}';
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public ArrayList<PieBean> getObj() {
        return obj;
    }

    public void setObj(ArrayList<PieBean> obj) {
        this.obj = obj;
    }
}
get set toString shortcut right click and select Generate.

 

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ArrayList<MonthBean> mData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        initData ();
    }

    private void initData() {
        Gson gson = new Gson ();
        mData=gson.fromJson(mJson,new TypeToken<ArrayList<MonthBean>>(){}.getType());
    }

    private void initView() {
        vpMain.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return PieFragment.newInstance(mData.get(position).toString());
            }
            @Override
            public int getCount() {
                return mData.size();
            }
        });
    }
}

run successfully



 

3. Introduction to MPAndroidChart

https://github.com/PhilJay/MPAndroidChart

Usage: In Android Stidio

Add the following to your project level build.gradle:

allprojects {
    repositories {
        jcenter ()
        maven { url "https://jitpack.io" }
    }
}

Add this to your app build.gradle

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
    compile files('libs/gson-2.2.4.jar')

    compile 'com.github.PhilJay: MPAndroidChart: v3.0.2'
}

MPAndroidChart feels very good, and should do more of the above practice in the future.

There is also a shortcut key to remember alt+ctrl+l to organize the code.

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326123098&siteId=291194637