2013-03-21 90 views
0

我從Web服務中獲取JSON數據,然後將數據放入自定義ListViewnotifyDataSetChanged不起作用

點擊ListView的項目,我打開一個新的Activity。現在,我點擊「提交」這個活動我回到同一ListView的按鈕,

當我回到ListView,我想改變列表項,其中我

之前單擊文本我只想刷新或重新調用活動時,它再次來到listticket.java

現在數據是靜態的。我使用網絡服務。

listticket.java

public class listticket extends Activity { 

    static ListView listView; 

    ArrayList<String> aryTicket = new ArrayList<String>(); 

    private String[] listArr = { "A", "B" }; 

    private String[] listStatus = { "Alert", "Alert" }; 
    private String[] listTicketid = { "1", "2" }; 

    ArrayList<String> ary_ticket_code = new ArrayList<String>(); 

    ArrayList<String> ary_address = new ArrayList<String>(); 
    ArrayList<String> ary_ticketid = new ArrayList<String>(); 

    public static ArrayList<String> ary_status = new ArrayList<String>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.listticket); 

     listView = (ListView) findViewById(R.id.itemList); 

     // filllistdata(); 

     listArr = new String[ary_ticket_code.size()]; 
     listArr = ary_ticket_code.toArray(listArr); 

     listStatus = new String[ary_status.size()]; 
     listStatus = ary_status.toArray(listStatus); 

     listTicketid = new String[ary_ticketid.size()]; 
     listTicketid = ary_ticketid.toArray(listTicketid); 

     MyArrayAdapter adapter = new MyArrayAdapter(this, listArr); 
     listView.setAdapter(adapter); 

    } 

    @Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 

     ary_status.set(0, "Work Done"); 
     ((MyArrayAdapter) listView.getAdapter()).notifyDataSetChanged(); 

    } 

    public class MyArrayAdapter extends ArrayAdapter<String> { 

     Activity context; 
     String[] listArr; 

     private TextView btnchkout; 

     // private final integer[] image; 

     public MyArrayAdapter(Activity context, String[] objects) { 
      super(context, R.layout.ticket, objects); 
      // TODO Auto-generated constructor stub 
      this.context = context; 
      listArr = objects; 

     } 

     @Override 
     public View getView(final int position, View convertView, 
       ViewGroup parent) { 
      // TODO Auto-generated method stub 

      LayoutInflater inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 
      View view = inflater.inflate(R.layout.ticket, null, true); 

      TextView textView = (TextView) view.findViewById(R.id.txtTicketNo); 
      textView.setText(listArr[position]); 

      final TextView txtAlert = (TextView) view 
        .findViewById(R.id.txtAlert1); 
      // Toast.makeText(getApplicationContext(), listStatus[position], 
      // Toast.LENGTH_LONG).show(); 
      String statusCheck = listStatus[position].toString().trim(); 

      if (statusCheck.equals("Alert") == true) { 
       txtAlert.setText(statusCheck); 
       txtAlert.setTextColor(getResources().getColor(R.color.red)); 

      } else if (statusCheck.equals("In Progress") == true) { 
       txtAlert.setText(statusCheck); 
       txtAlert.setTextColor(getResources().getColor(R.color.view)); 

      } else if (statusCheck.equals("Work Complete Pending paperwork") == true) { 
       txtAlert.setText("Work Done"); 
       txtAlert.setTextColor(getResources().getColor(R.color.green)); 

      } else { 
       txtAlert.setText(statusCheck); 
       // txtAlert.setTextColor(getResources().getColor(R.color.defaulttextcolor)); 
      } 

      final TextView btntextView = (TextView) view 
        .findViewById(R.id.btnCheckin); 

      if (statusCheck.equals("In Progress")) { 
       Drawable d = getResources().getDrawable(R.drawable.btncheckin); 
       btntextView.setBackgroundDrawable(d); 
      } 

      btnchkout = (TextView) view.findViewById(R.id.btnCheckout); 

      btnchkout.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 

        Intent intent = new Intent(listticket.this, 
          ticket_detail.class); 
        startActivity(intent); 

       } 
      }); 

      return view; 
     } 
    } 

} 

在當列表項

點擊

ticketdetail.java

protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.ticket_detail); 



     btnSubmit = (TextView) findViewById(R.id.btnSubmit); 


     btnSubmit.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

        new AsyncAction().execute(null,null,null); 


       //onBackPressed(); 
      } 
     }); 
    } 

    private class AsyncAction extends AsyncTask<String, Void, String> 
    { 
     public boolean status=false; 
     private ProgressDialog pd; 
     @Override 
     protected String doInBackground(String... arg0) 
     { 
      // TODO Auto-generated method stub 
      try 
       { 

      } 
       catch (Exception e) 
       { 
       // TODO: handle exception 
      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) 
     { 

      pd.dismiss(); 
      Intent intent = new Intent(ticket_detail.this,listticket.class); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
      startActivity(intent); 
      //finish(); 

     } 

     protected void onPreExecute() 
     { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
      pd = new ProgressDialog(ticket_detail.this); 
      pd.setMessage("Please Wait ..."); 
      pd.setIndeterminate(true); 
      pd.setCancelable(false); 
      pd.show(); 
     } 

    } 

     @Override 
     public boolean onKeyDown(int keyCode, KeyEvent event) { 
      if (keyCode == KeyEvent.KEYCODE_BACK) { 
       Intent a = new Intent(ticket_detail.this,listticket.class); 
       a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(a); 
       finish(); 

       return true; 
      } 
      return super.onKeyDown(keyCode, event); 
     } 
+0

您正在更改'getView()'中未使用的'onResume()'中'ary_status'的值。 – 2013-03-21 07:19:26

回答

0

製作適配器全球任務的其他佈局和內onCreate和內部初始化一次onResume()通知t Ø適配器如下圖所示:

Delcaration

MyArrayAdapter適配器;

onCreate

adapter = new MyArrayAdapter(this, listArr); 
listView.setAdapter(adapter); 

onResume()

adapter.notifyDataSetChanged();

0

試試這個,添加,清除和刪除調用notifyDataSetChanged本身,你不必在每次刷新你的信息時更改你的適配器,其代價很大。

adapter.clear() 

for (Object o : object) 
    adapter.add(o) 
相關問題