在主屏幕中显示JSON数据

时间:2017-06-11 05:28:17

标签: android json listview floating-action-button

只有单击浮动操作按钮才会出现json数据,我想直接在主屏幕上显示我的json数据而不是通过浮动操作按钮,该怎么做?

继承我的代码

  

main_activity.java

Donations
  .joins(category: :crops)
  .joins(<<-SQL)
    LEFT JOIN (
      SELECT ca2.name as parent_category_name, ca3.category_id as child_category_id
      FROM categories_crops cc2
      JOIN categories ca2 ON ca2.id = cc2.category_id
      JOIN crops cr2 ON cr2.id = cc2.crop_id
      JOIN categories ca3 ON ca3.name = cr2.name
    ) cp ON cp.child_category_id = categories.id
  SQL
  .where('categories.name LIKE :term OR crops.name LIKE :term OR cp.parent_category_name LIKE :term', term: "%#{search_term}%")

activity_main.xml中

public class MainActivity extends AppCompatActivity {  

private ListView listView;  
private ArrayList<MyDataModel> list;  
private MyArrayAdapter adapter;  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
 super.onCreate(savedInstanceState);  
 setContentView(R.layout.activity_main);  

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);  
 setSupportActionBar(toolbar);  

  /**  
  * Array List for Binding Data from JSON to this List  
  */  
   list = new ArrayList<>();  
  /**  
  * Binding that List to Adapter  
  */  
 adapter = new MyArrayAdapter(this, list);  

 /**  
  * Getting List and Setting List Adapter  
  */  
 listView = (ListView) findViewById(R.id.listView);  
 listView.setAdapter(adapter);  
 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
   @Override  
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {  
     Snackbar.make(findViewById(R.id.parentLayout), list.get(position).getName() + " => " + list.get(position).getPhone(), Snackbar.LENGTH_LONG).show();  
   }  
 });  

 /**  
  * Just to know onClick and Printing Hello Toast in Center.  
  */  
 Toast toast = Toast.makeText(getApplicationContext(), "Click on         

 FloatingActionButton to Load JSON", Toast.LENGTH_LONG);  
 toast.setGravity(Gravity.CENTER, 0, 0);  
 toast.show();  
 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);  
 fab.setOnClickListener(new View.OnClickListener() {  
   @Override  
   public void onClick(@NonNull View view) {  

     /**  
      * Checking Internet Connection  
      */  
     if (InternetConnection.checkConnection(getApplicationContext())) {  
       new GetDataTask().execute();  
     } else {  
       Snackbar.make(view, "Internet Connection Not Available", Snackbar.LENGTH_LONG).show();  
     }  
   }  
 });  
 }  

 /**  
* Creating Get Data Task for Getting Data From Web  
*/  
 class GetDataTask extends AsyncTask<Void, Void, Void> {  

 ProgressDialog dialog; 

  @Override  
 protected void onPostExecute(Void aVoid) {  
   super.onPostExecute(aVoid);  
   dialog.dismiss();  
   /**  
    * Checking if List size if more than zero then  
    * Update ListView  
    */  
   if(list.size() > 0) {  
     adapter.notifyDataSetChanged();  
   } else {  
     Snackbar.make(findViewById(R.id.parentLayout), "No Data Found", Snackbar.LENGTH_LONG).show();  
   }  
  }  
 }  
}
谢谢。

1 个答案:

答案 0 :(得分:3)

将以下代码从onClick()移至onCreate()方法:

 if (InternetConnection.checkConnection(getApplicationContext())) {  
   new GetDataTask().execute();  
 } else {  
   Snackbar.make(view, "Internet Connection Not Available", Snackbar.LENGTH_LONG).show();  
 }

如下所示:点击MainActivity

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private ArrayList<MyDataModel> list;
    private MyArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        /**
         * Array List for Binding Data from JSON to this List  
         */
        list = new ArrayList<>();
        /**
         * Binding that List to Adapter  
         */
        adapter = new MyArrayAdapter(this, list);

        /**
         * Getting List and Setting List Adapter  
         */
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Snackbar.make(findViewById(R.id.parentLayout), list.get(position).getName() + " => " + list.get(position).getPhone(), Snackbar.LENGTH_LONG).show();
            }
        });

        /**
         * Just to know onClick and Printing Hello Toast in Center.  
         */
        Toast toast = Toast.makeText(getApplicationContext(), "Click on FloatingActionButton to Load JSON", Toast.LENGTH_LONG);  
                toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();

        /**
         * Checking Internet Connection  
         */
        if (InternetConnection.checkConnection(getApplicationContext())) {
            new GetDataTask().execute();
        } else {
            Snackbar.make(view, "Internet Connection Not Available", Snackbar.LENGTH_LONG).show();
        }

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(@NonNull View view) {

                // Do something...
            }
        });
    }

    /**
     * Creating Get Data Task for Getting Data From Web  
     */
    class GetDataTask extends AsyncTask<Void, Void, Void> {

        ProgressDialog dialog;

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            dialog.dismiss();
            /**
             * Checking if List size if more than zero then  
             * Update ListView  
             */
            if(list.size() > 0) {
                adapter.notifyDataSetChanged();
            } else {
                Snackbar.make(findViewById(R.id.parentLayout), "No Data Found", Snackbar.LENGTH_LONG).show();
            }
        }
    }
}