如何使用从在线SQL DB检索的数据实现List视图

时间:2016-03-22 12:28:50

标签: android listview

  JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);


JSONObject collegeData;      
for(int i = 0; i<result.length(); i++){
     collegeData = result.getJSONObject(i);
     name = collegeData.getString(Config.KEY_NAME);
     address = collegeData.getString(Config.KEY_ADDRESS);
     vc = collegeData.getString(Config.KEY_VC);

     // listview logic

} 

嘿所有,基本上我已经从在线SQL数据库中使用凌空检索了数据。我之前使用过列表视图和微调器,但只使用了预设定义的数组。

在这种情况下,我对于如何将上述变量放入列表视图感到困惑。非常感谢任何正确方向的帮助/点

谢谢

1 个答案:

答案 0 :(得分:0)

布局xml文件。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

         <ListView
              android:id="@+id/list"
              android:layout_height="wrap_content"
              android:layout_width="match_parent">
         </ListView>

</LinearLayout>

您的班级文件,

public class ListViewAndroidExample extends Activity {
        ListView listView ;

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

            // Get ListView object from xml
            listView = (ListView) findViewById(R.id.list);

            // Defined Array values to show in ListView
            String[] values = new String[] { "Android List View", 
                                             "Adapter implementation",
                                             "Simple List View In Android",
                                             "Create List View Android", 
                                             "Android Example", 
                                             "List View Source Code", 
                                             "List View Array Adapter", 
                                             "Android Example List View" 
                                            };

            // Define a new Adapter
            // First parameter - Context
            // Second parameter - Layout for the row
            // Third parameter - ID of the TextView to which the data is written
            // Forth - the Array of data

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_1, android.R.id.text1, values);


            // Assign adapter to ListView
            listView.setAdapter(adapter); 

            // ListView Item Click Listener
            listView.setOnItemClickListener(new OnItemClickListener() {

                  @Override
                  public void onItemClick(AdapterView<?> parent, View view,
                     int position, long id) {

                   // ListView Clicked item index
                   int itemPosition     = position;

                   // ListView Clicked item value
                   String  itemValue    = (String) listView.getItemAtPosition(position);

                    // Show Alert 
                    Toast.makeText(getApplicationContext(),
                      "Position :"+itemPosition+"  ListItem : " +itemValue , Toast.LENGTH_LONG)
                      .show();

                  }

             }); 
        }

    }

您需要使用自己的数据,这些数据来自于volly,需要传递给listview适配器类。