Android项目中的错误

时间:2012-10-24 16:19:50

标签: java android json listview

我在使用旧教程时遇到了问题,基本上我正试图将JSON格式返回的数据从www.amazingjobs.co.uk/app/marc/api/candidates/拉到listview中。

我真的开始在24小时前学习android / java,所以它可能很简单,但我似乎无法弄明白,有错误的行是粗体!

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) {

                 }
             });

        };
    };

编辑好的新代码如下,错误,这看起来很奇怪,因为活动错误与肯定存在的活动有关,也试图在eclipse中重启/清理。

描述资源路径位置类型 activity_searchjobs无法解析或者不是字段Searchjobs.java / AmazingJobs / src / app / android / amazingjobs第47行Java问题 activity_searchjobs无法解析或者不是字段Searchjobs.java / AmazingJobs / src / app / android / amazingjobs line 105 Java问题 Searchbutton无法解析或者不是字段Searchjobs.java / AmazingJobs / src / app / android / amazingjobs line 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) {

                 }
             });

        };
    };

2 个答案:

答案 0 :(得分:2)

如果您不想使用ListActivity,则需要扩展ListActivity而不是Activity以使getListView()等方法可用或仅将其替换为findViewById()(这不是必需的)。

答案 1 :(得分: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

将会得到解决。