ローカルサーバーからJSONで予約を取得し、それらをに表示するアプリケーションを作成していますListView
。
データをプルして基本List
形式で表示することができましたが、見栄えの良いものを作成することにしましたList
。にさらに要素を追加するezzylearningのガイドに従っていますListView
。
コードを確認してグーグルで検索しましたがListView
、何らかの形でエラーが発生します。この問題を解決またはデバッグする方法がわからない。
Ignition.java(mainActivity):
package com.systematix.sxConcept;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
public class Ignition extends Activity {
// url to make request
private static String url = "http://10.0.0.103/android/data";
public String company;
public String course;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ignition);
new ReadJSONFeedTask().execute(url);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_ignition, menu);
return true;
}
public String readJSONFeed(String URL){
StringBuilder stringBuilder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpget = new HttpGet(URL);
try{
HttpResponse response = client.execute(httpget);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == 200){
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null){
stringBuilder.append(line);
}
}else{
Log.e("JSON", "Failed to download list");
}
} catch (ClientProtocolException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
return stringBuilder.toString();
}
private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls){
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result){
try{
JSONArray jsonArray = new JSONArray(result);
Log.i("JSON", "Number of surveys in feed :" + jsonArray.length());
ListView listView = (ListView) findViewById(R.id.listView1);
/*String[] responseList;
responseList = new String[jsonArray.length()];*/
Bookings[] booking_data;
booking_data = new Bookings[jsonArray.length()];
for (int i =0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
booking_data[i] = new Bookings(jsonObject.getString("Company"),jsonObject.getString("CourseTitle"));
//responseList[i] = jsonObject.getString("bookdate") + " - " + jsonObject.getString("Company");
}
BookingsAdapter adapter = new BookingsAdapter(getBaseContext(),
R.layout.listview_item_row, booking_data); //export the data to a custom adapter
/* View header = (View)getLayoutInflater().inflate(R.layout.listview_header_row, null);
listView.addHeaderView(header);*/
listView.setAdapter(adapter);
/*ArrayAdapter<String> newAdapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.test_list_item, responseList);
listView.setAdapter(newAdapter);*/
} catch (Exception e){
e.printStackTrace();
}
}
}
}
BookingsAdapter.java:
package com.systematix.sxConcept;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class BookingsAdapter extends ArrayAdapter<Bookings>{
Context context;
int layoutResourceId;
Bookings data[] = null;
public BookingsAdapter(Context context, int layoutResourceId, Bookings[] data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
BookingHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new BookingHolder();
holder.company = (TextView)row.findViewById(R.id.rowCompany);
holder.course = (TextView)row.findViewById(R.id.rowCourse);
row.setTag(holder);
}
else
{
holder = (BookingHolder)row.getTag();
}
Bookings booking = data[position];
holder.company.setText(booking.company);
holder.course.setText(booking.course);
return row;
}
static class BookingHolder
{
TextView company;
TextView course;
}
}
Bookings.java:
package com.systematix.sxConcept;
public class Bookings {
public String company;
public String course;
public Bookings(){
super();
}
public Bookings(String company, String course) {
super();
this.company = company;
this.course = course;
}
}
XMLレイアウトファイルは、チュートリアルで見られる1のコピーであり、アイコンがに置き換えられていTextview
ます。
これらはエラーです:
07-24 12:36:10.154: E/AndroidRuntime(331): FATAL EXCEPTION: main
07-24 12:36:10.154: E/AndroidRuntime(331): java.lang.ClassCastException: android.app.ContextImpl
07-24 12:36:10.154: E/AndroidRuntime(331): at com.systematix.sxConcept.BookingsAdapter.getView(BookingsAdapter.java:31)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.AbsListView.obtainView(AbsListView.java:1430)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.ListView.makeAndAddView(ListView.java:1745)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.ListView.fillDown(ListView.java:670)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.ListView.fillFromTop(ListView.java:727)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.ListView.layoutChildren(ListView.java:1598)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.AbsListView.onLayout(AbsListView.java:1260)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.view.View.layout(View.java:7175)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.view.View.layout(View.java:7175)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.view.View.layout(View.java:7175)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.view.View.layout(View.java:7175)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.view.View.layout(View.java:7175)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.os.Looper.loop(Looper.java:123)
07-24 12:36:10.154: E/AndroidRuntime(331): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-24 12:36:10.154: E/AndroidRuntime(331): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 12:36:10.154: E/AndroidRuntime(331): at java.lang.reflect.Method.invoke(Method.java:507)
07-24 12:36:10.154: E/AndroidRuntime(331): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-24 12:36:10.154: E/AndroidRuntime(331): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-24 12:36:10.154: E/AndroidRuntime(331): at dalvik.system.NativeStart.main(Native Method)