2012-10-24 82 views
0

我有過一箇舊的輔導工作我的方式,基本上我想拉數據問題返回JSON格式從www.amazingjobs.co.uk/app/marc/api/candidates/到一個列表視圖。錯誤在Android項目

我真的開始學習的android/java的24小時之前所以它很有可能會是很簡單,但我只是不能完全似乎弄明白,有錯誤的線是大膽!

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.HashMap; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ListAdapter; 
import android.widget.ListView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 

import com.google.gson.Gson; 


public class Searchjobs extends Activity { 
    // url to make request 
    private static String url = "http://www.amazingjobs.co.uk/app/marc/api/candidates/"; 

    // JSON Node names 
    private static final String TAG_RESULTS = "results"; 
    // Jobs JSONArray 
    JSONArray jobs = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_searchjobs); 
     Button Searchbutton = (Button) findViewById(R.id.Searchbutton); 
     Searchbutton.setOnClickListener(searchClick); 
    } 

    private OnClickListener searchClick = new OnClickListener() { 
     public void onClick(View v) { 

      // Hashmap for ListView 
       ArrayList<HashMap<String, String>> JobList = new ArrayList<HashMap<String, String>>(); 
       // Creating JSON Parser instance 
       JSONParser jParser = new JSONParser(); 
       // getting JSON string from URL 
       JSONObject json = jParser.getJSONFromUrl("http://www.amazingjobs.co.uk/app/marc/api/candidates/"); 
       // Getting Array of Contacts 
      try { 
       jobs = json.getJSONArray(TAG_RESULTS); 

       for(int i = 0; i < jobs.length();i++){ 
        JSONObject j = jobs.getJSONObject(i); 

        String title = j.getString("job_title"); 

        HashMap<String, String> map = new HashMap<String, String>(); 

        map.put("Title", title); 

        JobList.add(map); 


       } 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      **ListAdapter adapter = new SimpleAdapter(this, JobList,R.layout.list_item, 
         new String[] { "Title" }, new int[] {R.id.title});** 
      **ERROR: The constructor SimpleAdapter(new View.OnClickListener(){}, ArrayList<HashMap<String,String>>, int, String[], int[]) is undefined** 

       **setListAdapter(adapter); 
The method setListAdapter(ListAdapter) is undefined for the type new View.OnClickListener(){}** 

       // selecting single ListView item 
       **ListView lv = getListView();** 
        **ERROR: The method getListView() is undefined for the type new View.OnClickListener(){}** 


       lv.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 

       } 
      }); 

     }; 
    }; 

編輯好了新的代碼如下,錯誤是,因爲活動的錯誤涉及到的肯定存在的活動似乎很奇怪,試圖重啓/清除在日食了。

描述資源路徑位置類型 activity_searchjobs不能得到解決或無法在現場Searchjobs.java/AmazingJobs/src目錄/應用/安卓/ amazingjobs 47個的Java問題 activity_searchjobs不能得到解決或無法在現場Searchjobs.java/AmazingJobs/SRC /應用程序/機器人/ amazingjobs線105的Java問題 搜索按鈕不能得到解決或不是一個場Searchjobs.java/AmazingJobs/SRC /應用程序/機器人/ amazingjobs線48的Java問題 LIST_ITEM不能得到解決或不是一個場Searchjobs.java/AmazingJobs/src/app/android/amazingjobs line 83 Java問題

public class Searchjobs extends ListActivity { 
    // url to make request 
    private static String url = "http://www.amazingjobs.co.uk/app/marc/api/candidates/"; 

    // JSON Node names 
    private static final String TAG_RESULTS = "results"; 
    // Jobs JSONArray 
    JSONArray jobs = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_searchjobs); 
     Button Searchbutton = (Button) findViewById(R.id.Searchbutton); 
     Searchbutton.setOnClickListener(searchClick); 
    } 

    private OnClickListener searchClick = new OnClickListener() { 
     public void onClick(View v) { 

      // Hashmap for ListView 
       ArrayList<HashMap<String, String>> JobList = new ArrayList<HashMap<String, String>>(); 
       // Creating JSON Parser instance 
       JSONParser jParser = new JSONParser(); 
       // getting JSON string from URL 
       JSONObject json = jParser.getJSONFromUrl("http://www.amazingjobs.co.uk/app/marc/api/candidates/"); 
       // Getting Array of Contacts 
      try { 
       jobs = json.getJSONArray(TAG_RESULTS); 

       for(int i = 0; i < jobs.length();i++){ 
        JSONObject j = jobs.getJSONObject(i); 

        String title = j.getString("job_title"); 

        HashMap<String, String> map = new HashMap<String, String>(); 

        map.put("Title", title); 

        JobList.add(map); 


       } 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      ListAdapter adapter = new SimpleAdapter(getApplication(), JobList,R.layout.list_item, 
        new String[] { "Title" }, new int[] {R.id.title}); 

       setListAdapter(adapter); 

       // selecting single ListView item 
       ListView lv = getListView(); 

       lv.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 

       } 
      }); 

     }; 
    }; 
+0

把你的錯誤日誌放在這裏,所以我可以找到什麼問題? –

回答

1
ListAdapter adapter = new SimpleAdapter(this, JobList,R.layout.list_item, 
         new String[] { "Title" }, new int[] {R.id.title}); 

應改爲:

ListAdapter adapter = new SimpleAdapter(getApplicationContext(), JobList,R.layout.list_item, 
         new String[] { "Title" }, new int[] {R.id.title}); 

其次,

ListView的LV = getListView();如果你擴展ListActivity而不是Activity

將得到解決。

+0

啊,非常感謝,不是我忘恩負義,但其他兩個錯誤呢? –

+0

輝煌的,我已經從PHP移動,所以Java讓我完全的大腦疼痛得到處理它,非常感謝你的幫助theres我沒有辦法分裂你之間的點? –

+0

你只能接受一個答案,但是如果你有足夠的聲望,你可以同時接受兩個答案。 – kosa

2

您需要延長ListActivityActivitygetListView() avaialable做出方法或只是findViewById()取代它,如果你不希望使用ListActivity(這不是強制性的)。