2016年8月31日 星期三

Android 不同類別傳Activity

Still being relatively new to this I'm having issues in finding a view in a non-activity class MyLocation which I'm using in my activity class MainActivity. I'm using MyLocation to get longitude and latitude. I want to highlight a textview when either GPS or Network has been used. To do so I need to find the textviews in the non-activity class MyLocation.
Here is how I'm calling it in MainActivity:
public class MainActivity extends ActionBarActivity implements LocationListener {

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

            MyLocation myLocation = new MyLocation();
            myLocation.getLocation(this, locationResult);

}
And here what I tried in MyLocation to find the textviews:
public class MyLocation {

LocationManager lm;
LocationResult locationResult;
private Context context;
TextView tvnetwork, tvgps;
private int defaultTextColor;

LocationListener locationListenerNetwork = new LocationListener() {
    public void onLocationChanged(Location location) {

        locationResult.gotLocation(location);

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.main, null);

        tvnetwork = (TextView) v.findViewById(R.id.tvnetwork);
        tvgps = (TextView) v.findViewById(R.id.tvgps);
        defaultTextColor = tvgps.getTextColors().getDefaultColor();

        tvnetwork.setTextColor(context.getResources().getColor(
                R.color.green));
        tvgps.setTextColor(defaultTextColor);

        lm.removeUpdates(this);
        lm.removeUpdates(locationListenerGps);
    }

    public void onProviderDisabled(String provider) {
    }

    public void onProviderEnabled(String provider) {
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
    }
};
But the views are not found. I already get a NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);. What am I doing wrong?
shareimprove this question
  
Where you have defined your getLocation method ? – Spring Breaker Sep 12 '14 at 11:27 
  
very simple...when you are calling MyLocation class..just add context as parameter to it and thus you can get all the things from there on. – GvSharma Sep 12 '14 at 11:32

3 Answers


get a NPE @ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);. What am I doing wrong?
Because context is null in MyLocation class. use MyLocation class constructor to pass MainActivity context in MyLocation to access system services as:
Activity activity;
public MyLocation(Context context,Activity activity){
this.context=context;
this.activity=activity;
}
and in MainActivity create MyLocation class object by passing MainActivity context as:
MyLocation myLocation = new MyLocation(MainActivity.this,this);
Now use context to access system services in MyLocation class
EDIT: Instead of inflating main layout again in onLocationChanged use Activity context to access view from Activity Layout as:
 public void onLocationChanged(Location location) {

       ....
        tvnetwork = (TextView) activity.findViewById(R.id.tvnetwork);
        tvgps = (TextView) activity.findViewById(R.id.tvgps);
        defaultTextColor = tvgps.getTextColors().getDefaultColor();

        tvnetwork.setTextColor(context.getResources().getColor(
                R.color.green));
        tvgps.setTextColor(defaultTextColor);

       ....
    }

Android 多個view寫法

void initOnClickListener() {

        int my_ids[] = {
                R.id.button01, R.id.button02, R.id.button03, ...
        };

        Button b = null;
        for( int i=0 ; i< ids.length ; ++i )
                if( ( b = (Button)findViewById( my_ids[i]) ) != null )
                        b.setOnClickListener(this);
}

public void onClick(View v) {
        switch( v.getId() ) {
                case R.id.button01:
                        break;
                case R.id.button02:
                        break;
                // ...
        }
}

2016年8月29日 星期一

C# 用Linq+匿名物件寫法 直接組JSON


用Linq+匿名物件寫法 直接組JSON
這招在Json.net的官方文件有範例(用JObject.FromObject的那個):http://james.newtonking.com/projects/json/help/html/CreatingLINQtoJSON.htm
但只有範例,沒寫為什麼Linq要那樣寫,誰看得懂阿XD
要用Linq直接組JSON
大概把握幾點:
Json Object:Json字串用大括號{}表示,Linq也是用大括號{}表示
Json Object的Name(Key、Property):Json字串在兩個雙引號””裡寫一個英文單字,Linq就直接寫英文單字即可
Json Array:Json字串用中括號[]表示,Linq就用from o in XXX select o,這種回傳IEnumerable的寫法(如果JSON字串是物件陣列的話就用from o in XXX select new {欄位1=YYY}這種形式)
Json Value:Linq就看Json字串填什麼值就跟著填什麼值

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)//Get Method
        {
            //準備資料
            List<string> registration_ids = new List<string>() { "4", "8", "15", "16", "23", "42" };

            //用Linq直接組
            var result = new
                            {
                                collapse_key = "score_update",
                                time_to_live = 108,
                                delay_while_idle = true,
                                data = new{
                                    score ="4x8",
                                    time = "15:16.2342"
                                },
                                registration_ids = from s in registration_ids
                                                            select s

                            };

            //序列化為JSON字串並輸出結果
            Response.Write(JsonConvert.SerializeObject(result));
        }
    }

2016年8月14日 星期日

Android map fragment 顯示按鈕

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.LocationChooser">


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|top"
        android:text="Demo Button" 
        android:padding="10dp"
        android:layout_marginTop="20dp"
        android:paddingRight="10dp"/>

</fragment>