来自JSON的Android Populate Listview(来自edittext的链接)

时间:2016-03-27 12:55:15

标签: android json listview

我是android新手,我正在使用一个项目,将数据从互联网加载到listview中。我在这里得到了我的原型:kaleidosblog.com/android-listview-load-data-from-json

所以这些将是json链接:

在我的活动中,我有EditText,Button和ListView。

  • Editext将获取网址。
  • 按钮用于将json链接(从url)加载到listview
  • Listview将显示数据

在我目前的程序中,它只适用于第一次点击按钮。所以一旦我进入第一个json,它将显示正确的数据。但是当我尝试在EditText上更改json时,仍然会由第一个json填充ListView。简而言之,每次我更改链接并单击按钮时,我的ListView都不会刷新。

这有什么问题?

主要活动:

protected void onCreate(Bundle savedInstanceState) {
    final Button searchButton = (Button) findViewById(R.id.searchButton);
    final EditText searchForm = (EditText) findViewById(R.id.searchForm);
    searchButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            list = (ListView) findViewById(R.id.list);
            adapter = new ListAdapter(MainActivity.this);
            list.setAdapter(adapter);
            search = searchForm.getText().toString();
            Download_data download_data = new Download_data((download_complete) MainActivity.this);
            download_data.download_data_from_link(search);
            adapter.notifyDataSetChanged();
            Toast.makeText(getApplicationContext(), search, Toast.LENGTH_LONG).show();
        }
    });
    }

    public void get_data(String data){
        try {
            JSONObject jsonObj = new JSONObject(data);
            JSONArray data_array = jsonObj.getJSONArray("announcements");

            for (int i = 0 ; i < data_array.length() ; i++)
            {
                JSONObject obj=new JSONObject(data_array.get(i).toString());

                Countries add=new Countries();
                add.name = obj.getString("message");
                add.code = obj.getString("date");

                countries.add(add);

            }

            adapter.notifyDataSetChanged();

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

Download_data.java

public class Download_data implements Runnable  {

    public download_complete caller;

    public interface download_complete{
        public void get_data(String data);
    }   

    Download_data(download_complete caller) {         
        this.caller = caller;
    }

    private String link;
    public void download_data_from_link(String link){
        this.link = link;
        Thread t = new Thread(this);
        t.start();
    }

     public void run() {
        threadMsg(download(this.link));

     }

     private void threadMsg(String msg) {

         if (!msg.equals(null) && !msg.equals("")) {
             Message msgObj = handler.obtainMessage();
             Bundle b = new Bundle();
             b.putString("message", msg);
             msgObj.setData(b);
             handler.sendMessage(msgObj);
         }
     }


     private final Handler handler = new Handler() {

         public void handleMessage(Message msg) {

             String Response = msg.getData().getString("message");

             caller.get_data(Response);

         }
     };
    public static String download(String url) {
         URL website;
         StringBuilder response = null;
        try {
            website = new URL(url);

            HttpURLConnection connection = (HttpURLConnection) website.openConnection();
            connection.setRequestProperty("charset", "utf-8");

            BufferedReader in = new BufferedReader(
                 new InputStreamReader(
                     connection.getInputStream()));

            response = new StringBuilder();
            String inputLine;

            while ((inputLine = in.readLine()) != null) 
                response.append(inputLine);

            in.close();

        } catch (Exception  e) {
            return "";
        }
         return response.toString();
     }

ListAdapter.java

MainActivity main;

ListAdapter(MainActivity main)
{
    this.main = main;
}

@Override
public int getCount() {
    return  main.countries.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

static class ViewHolderItem {
    TextView name;
    TextView code;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){
    ViewHolderItem holder = new ViewHolderItem();
    if (convertView == null) {
     LayoutInflater inflater = (LayoutInflater) main.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.cell, null);

        holder.name = (TextView) convertView.findViewById(R.id.name);
        holder.code = (TextView) convertView.findViewById(R.id.code);

        convertView.setTag(holder);
    }
    else
    {
         holder = (ViewHolderItem) convertView.getTag();
    }


    holder.name.setText(this.main.countries.get(position).name);
    holder.code.setText(this.main.countries.get(position).code);

    return convertView;
}

1 个答案:

答案 0 :(得分:1)

似乎每次都在get_data()中添加国家/地区列表,但从未清除。在get_data开头,您很可能希望通过以下调用清除国家/地区列表:

countries.clear();

然后将清除国家/地区列表中的数据,新下载的数据将添加到国家/地区列表中,然后在向适配器通知数据更改时在视图中更新。