2016-03-27 37 views
1

我是新來的android和我正在從一個項目,從互聯網加載數據到列表視圖。我得到了我的原型在這裏:kaleidosblog.com/android-listview-load-data-from-jsonAndroid從JSON填充列表視圖(從edittext鏈接)

所以這將是JSON鏈接:

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

  • Editext將得到的網址。
  • 按鈕將被用於加載JSON鏈接(URL從)到ListView控件
  • 列表視圖顯示DATAS

在我目前的計劃,它只能在按鈕的第一次點擊。所以一旦我進入第一個JSON,它會顯示正確的數據。但是當我嘗試更改EditText上的json時,ListView仍然由第一個json填充。簡而言之,我每次更改鏈接並單擊按鈕時,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

看來,國家名單正在增加每次在GET_DATA() ,但從未清除。在GET_DATA開始時,你很可能希望通過以下調用來清除國家名單:

countries.clear(); 

然後在國家列表中的數據將被清空,新下載的數據將被添加到國家名單,然後在適配器被通知數據更改時在視圖中更新。