2015-09-14 2 views
0

Мое listview предлагается импортировать данные json в listview, но это не так. Вот мой rooster.xml файл:ListView не показывает данные JSON

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context="com.nijdeken.ccapp.rooster"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Les" 
     android:id="@+id/lesson" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Lokaal" 
     android:id="@+id/room" 
     android:layout_above="@android:id/list" 
     android:layout_toLeftOf="@+id/teacher" 
     android:layout_toStartOf="@+id/teacher" 
     android:layout_marginRight="57dp" 
     android:layout_marginEnd="57dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Docent" 
     android:id="@+id/teacher" 
     android:layout_above="@android:id/list" 
     android:layout_toLeftOf="@+id/start" 
     android:layout_toStartOf="@+id/start" 
     android:layout_marginRight="92dp" 
     android:layout_marginEnd="92dp" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:text="Tijd" 
     android:id="@+id/start" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" /> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:fontFamily="sans-serif-thin" 
     android:id="@android:id/list" 
     android:layout_below="@+id/lesson" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

И мой Rooster.java файл:

public class rooster extends ListActivity { 

private ProgressDialog pDialog; 

// URL to get contacts JSON 
private static String url = "http://api.ccapp.it/v2/zportal/schedule/37?token=df679ovlka5urmajmd7tg28lc0"; 

String apiUrl = "http://api.ccapp.it"; 

// JSON Node names 
private static final String TAG_TIME = "start"; 
private static final String TAG_ROOM = "locations"; 
private static final String TAG_TEACHER = "teachers"; 
private static final String TAG_LESSON = "subjects"; 

// contacts JSONArray 
JSONArray contacts = null; 

// Hashmap for ListView 
ArrayList<HashMap<String, String>> contactList; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_rooste); 

// ListView listView = (ListView) findViewById(android.R.id.list); 
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
// fab.attachToListView(listView); 

    contactList = new ArrayList<HashMap<String, String>>(); 

    ListView lv = getListView(); 

    // Listview on item click listener 
    lv.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> parent, View view, 
           int position, long id) { 
      // getting values from selected ListItem 
      String name = ((TextView) view.findViewById(R.id.lesson)) 
        .getText().toString(); 
      String cost = ((TextView) view.findViewById(R.id.room)) 
        .getText().toString(); 
      String description = ((TextView) view.findViewById(R.id.teacher)) 
        .getText().toString(); 

      // Starting single contact activity 
//   Intent in = new Intent(getApplicationContext(), 
//     SingleContactActivity.class); 
//    in.putExtra(TAG_NAME, name); 
//    in.putExtra(TAG_EMAIL, cost); 
//    in.putExtra(TAG_PHONE_MOBILE, description); 
//    startActivity(in); 

     } 
    }); 

    // Calling async task to get json 
    new GetContacts().execute(); 
} 

/** 
* Async task class to get json by making HTTP call 
* */ 
private class GetContacts extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Showing progress dialog 


    } 


    @Override 
    protected Void doInBackground(Void... arg0) { 
     // Creating service handler class instance 
     Servicehandler sh = new Servicehandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url, Servicehandler.GET); 

     Log.d("Schedule: ", "> " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 

       // Getting JSON Array node 
       contacts = jsonObj.getJSONArray(""); 


       // looping through All Contacts 
       for (int i = 0; i < contacts.length(); i++) { 
        JSONObject c = contacts.getJSONObject(i); 

        String subject = c.getString(TAG_LESSON); 
        String teachers = c.getString(TAG_TEACHER); 
        String location = c.getString(TAG_ROOM); 
        String start = c.getString(TAG_TIME); 


        // tmp hashmap for single contact 
        HashMap<String, String> contact = new HashMap<String, String>(); 

        // adding each child node to HashMap key => value 
        contact.put(TAG_LESSON, subject); 
        contact.put(TAG_TEACHER, teachers); 
        contact.put(TAG_ROOM, location); 
        contact.put(TAG_TIME, start); 

        // adding contact to contact list 
        contactList.add(contact); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } else { 
      Log.e("ServiceHandler", "Couldn't get any data from the url"); 
     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 

     /** 
     * Updating parsed JSON data into ListView 
     * */ 
     ListAdapter adapter = new SimpleAdapter(
       rooster.this, contactList, 
       R.layout.list_item, new String[] { TAG_LESSON, TAG_ROOM, 
       TAG_TEACHER, TAG_TIME }, new int[] { R.id.lesson, 
       R.id.room, R.id.teacher, R.id.time }); 

     setListAdapter(adapter); 
    } 

} 
public static void getRooster(String appcode, int week){ 
//  url="http://api.ccapp.it/v2/zportal/schedule/"+week+"?token="+appcode; 
    url="http://nijdeken.com/json/schedule.json"; 
} 

} 
+0

У вас есть разрешение ИНТЕРНЕТ в вашем 'AndroidManifest'? Сбой вашего приложения? Когда вы отлаживаете приложение, есть ли какие-то исключения? – akodiakson

+0

Я сделал запрос на http://api.ccapp.it/v2/zportal/schedule/37?token=df679ovlka5urmajmd7tg28lc0 возврат "[]". Я имею в виду, что это пустой массив. –

+0

ИНТЕРНЕТ-разрешение? : Да – User456476534

ответ

0

Ваш JSON URL дает нулевой выход. Я попробовал в браузере, тогда я получил значение как пустое [] ....

Просьба указать ввод вашему JSON. http://api.ccapp.it/v2/zportal/schedule/37?token=df679ovlka5urmajmd7tg28lc0

+0

Я отредактировал 37 до 39, но он все равно ничего не возвращает – User456476534

+0

Но ваш URL-адрес JSON возвращает нулевое значение. –

Смежные вопросы