0

我在完成旧教程时遇到问题,基本上我正在尝试以 JSON 格式返回的数据从 www.amazingjobs.co.uk/app/marc/api/candidates/ 提取到列表视图中。

我实际上是在 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 第 105 行 Java 问题 Searchbutton 无法解析或不是字段 Searchjobs.java /AmazingJobs/src/app/android/amazingjobs 第 48 行 Java 问题 list_item 无法解析或不是字段 Searchjobs.java /AmazingJobs/src/ app/android/amazingjobs 第 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) {

                 }
             });

        };
    };
4

2 回答 2

2

如果您不想使用 ListActivity (这不是强制性的),您需要扩展ListActivityActivity使方法getListView()可用,或者只需将其替换为。findViewById()

于 2012-10-24T16:22:27.740 回答
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});

第二,

列表视图 lv = getListView();

如果您扩展ListActivity而不是Activity.

于 2012-10-24T16:21:07.197 回答