在我的MainActivity中,我为id getclients和getemployees的按钮设置了监听器。 如果单击一个按钮,我会使用HttpGet从我的服务器接收json数据(这很好)。
但是,在onPostExecute中,您可以看到我创建了一个getEmployees(class)实例。 该类应该评估来自JSON的数据并创建员工视图。
MainActivity.java:
package learn2crack.listview;
import java.util.ArrayList;
import java.util.HashMap;
import learn2crack.listview.library.JSONParser;
import ontime.employees.getEmployees;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
ListView list;
TextView name;
TextView address;
TextView status;
Button BtnGetClients;
Button BtnGetEmployees;
getEmployees employees;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
JSONArray android = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oslist = new ArrayList<HashMap<String, String>>();
BtnGetClients = (Button)findViewById(R.id.getclients);
BtnGetClients.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://portal.ontimebemanning.no/api/json/clients/";
new JSONParse(url, "getClients", "clients").execute();
}
});
BtnGetEmployees = (Button)findViewById(R.id.getemployees);
BtnGetEmployees.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://portal.ontimebemanning.no/api/json/employees/";
new JSONParse(url, "getEmployees", "employees").execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
public String get_url;
public String get_view;
public String get_tag;
public JSONParse(String Url, String view, String tag){
get_url = Url;
get_tag = tag;
get_view = view;
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(get_url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
employees = new getEmployees();
try {
// Getting JSON Array from URL
android = json.getJSONArray(get_tag);
for(int i = 0; i < android.length(); i++){
JSONObject c = android.getJSONObject(i);
Log.d( "current", "Initiating getEmployees");
employees.createView( c );
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
getEmployees.java:
package ontime.employees;
import java.util.ArrayList;
import java.util.HashMap;
import learn2crack.listview.MainActivity;
import learn2crack.listview.R;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class getEmployees extends MainActivity {
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
ListView list;
TextView name;
TextView address;
TextView status;
public String putname, putaddress, putstatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("current", "OnCreate...");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (TextView)findViewById(R.id.name);
address = (TextView)findViewById(R.id.address);
status = (TextView)findViewById(R.id.status);
list=(ListView)findViewById(R.id.list);
oslist = new ArrayList<HashMap<String, String>>();
}
public getEmployees() {
super();
}
public void createView( JSONObject jSon ){
try {
String putname = jSon.getString("firm");
String putaddress = jSon.getString("address");
String putstatus = jSon.getString("status");
} catch (JSONException e) {
Log.e( "Current", e.getMessage());
}
Log.d("current", "HashMap");
HashMap<String, String> map = new HashMap<String, String>();
map.put("firstname", putname);
map.put("address", putaddress);
map.put("status", putstatus);
oslist.add(map);
Log.d("current", "ListAdapter");
try{
ListAdapter adapter = new SimpleAdapter(getEmployees.this, oslist,
R.layout.list_v,
new String[] { "firstname","address", "status" }, new int[] {
R.id.name,R.id.address, R.id.status});
list.setAdapter(adapter);
}
catch(Exception e){
Log.e("current",e.getMessage());
}
Log.d("current", "ListAdapter");
}
}
LogCat的输出:
10-23 08:14:57.690: D/current(14936): Initiating getEmployees
10-23 08:14:57.690: D/current(14936): HashMap
10-23 08:14:57.690: D/current(14936): ListAdapter
10-23 08:14:57.690: E/current(14936): System services not available to Activities before onCreate()
所以,我认为我的代码在ListAdapter / SimpleAdapter上破坏了,它试图修改R对象/视图。我也试过在getEmployees中的oncreate函数中添加一个Log.d,但这不会被触发,所以我认为这可能是我的问题的一部分,也就是说它没有设置当前类的视图。
非常感谢任何帮助。