2012-11-19 44 views
1

我已經創建了帶有標題的列表視圖,但是我無法對屬於同一標題的項目進行分組。帶有標題和組項目的列表視圖

有人能告訴我我應該做什麼嗎?

下面是顯示的項目到列表視圖代碼:

public class OrganizationList extends Activity implements OnClickListener { 

    ListView lv; 
    SimpleAdapter simpleAdapter; 
    ArrayList<HashMap<String, String>> videosList; 

    // Progress Dialog 
    private ProgressDialog pDialog; 

    // Creating JSON Parser object 
    JSONParser jParser = new JSONParser(); 
    JSONArray videos = null; 

    // url to get all videos 
    private static String url_video = "http://10.0.2.2/android_connect/get_organization.php"; 

    //JSON Nodes 
    private static final String TAG_POSITION = "position"; 
    private static final String TAG_URL = "URL"; 
    private static final String TAG_MEMBER = "name"; 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_NAME = "members"; 
    protected static final int GET_INTENT_CODE = 0; 


    public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.video); 

      Button refresh = (Button)findViewById(R.id.btn_refresh); 
      refresh.setOnClickListener(this); 

      TextView header = (TextView)findViewById(R.id.text_header); 
      header.setText("Videos (視頻)"); 

      ImageButton home = (ImageButton)findViewById(R.id.btn_home); 
      home.setOnClickListener(this); 

      // Hashmap for ListView 
      videosList = new ArrayList<HashMap<String, String>>(); 

      //Get adapter 
      simpleAdapter = new SimpleAdapter(OrganizationList.this, videosList, R.layout.list_date_item, new String[] {TAG_POSITION, TAG_MEMBER}, 
        new int[] { R.id.text_header, R.id.name}); 

      //Get Listview 
      lv = (ListView) findViewById(R.id.lst_name); 
      lv.setOnItemClickListener(new OnItemClickListener() { 

       @Override 
       public void onItemClick(AdapterView<?> parent, View view, 
         int position, long id) { 
        // getting values from selected ListItem 

        String videoName = ((TextView) view.findViewById(R.id.name)).getText() 
        .toString(); 
        Log.d("row click:", videoName); 

       } 
      }); 

      // Loading videos in Background Thread 
      new LoadAllVideos().execute(); 
    } 

    /** 
     * Background Async Task to Load all product by making HTTP Request 
     * */ 
     class LoadAllVideos extends AsyncTask<String, String, String> { 

      /** 
      * Before starting background thread Show Progress Dialog 
      * */ 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       pDialog = new ProgressDialog(OrganizationList.this); 
       pDialog.setMessage("Loading" + "\n" + "Please wait..."); 
       pDialog.setIndeterminate(false); 
       pDialog.setCancelable(false); 
       pDialog.show(); 
      } 

      /** 
      * getting All videos from url 
      * */ 
      protected String doInBackground(String... args) { 
       // Building Parameters 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       // getting JSON string from URL 
       JSONObject json = jParser.makeHttpRequest(url_video, "GET", params); 

       // Check your log cat for JSON reponse 
       Log.d("All videos: ", json.toString()); 

       try { 
        // Checking for SUCCESS TAG 
        int success = json.getInt(TAG_SUCCESS); 

        if (success == 1) { 
         // videos found 
         // Getting Array of videos 
         videos = json.getJSONArray(TAG_NAME); 

         // looping through All videos 
         for (int i = 0; i < videos.length(); i++) { 
          JSONObject c = videos.getJSONObject(i); 

          // Storing each json item in variable 
          String post = c.getString(TAG_POSITION); 
          String name = c.getString(TAG_MEMBER); 
          //videosList.add(c.getString(TAG_COMPANY)); 

          //Log.d("item Name:", name); 
          //Log.d("companyNAme", videosList.get(0)); 

          // creating new HashMap 
          HashMap<String, String> map = new HashMap<String, String>(); 

          // adding each child node to HashMap key => value 
          map.put(TAG_POSITION, post); 
          map.put(TAG_MEMBER, name); 

          // adding HashList to ArrayList 
          videosList.add(map); 

          //System.out.println(map);     
         }   

        } 
        else { 

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

       return null; 
      } 


     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog after getting all videos 
      pDialog.dismiss(); 

      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
        /** 
        * Updating parsed JSON data into ListView 
        * */ 

         // ListView Adapter 

        lv = (ListView) findViewById(R.id.lst_name); 
        lv.setAdapter(simpleAdapter); 
       } 
      }); 


     } 
     } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if (v.getId() == R.id.btn_refresh) 
     { 
      videosList.clear(); 
      new LoadAllVideos().execute(); 
     } 
     else if(v.getId() == R.id.btn_home) 
     { 

      Intent intent = new Intent(); 
      intent.setClassName("com.scba", "com.scba.Menu"); 
      finish(); 

     } 
    } 
} 

截圖:

enter image description here

+0

你想要做什麼不能使用像'SimpleAdapter'默認適配器來完成。您需要使用兩種類型的行(位置/員工姓名)創建自定義適配器,並將數據分爲兩類。 – Luksprog

+0

你知道任何教程或例子可以幫助嗎?我對此很新穎。 – Jolene

+1

在google上搜索分區適配器應該會提供一些結果。檢查此鏈接http://android.cyrilmottier.com/?p=440。 – Luksprog

回答

0

只是改變你的itemxml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<FrameLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/grayimage" 
> 

<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="president" 
android:id="@+id/headerText" 
/> 

</FrameLayout> 

<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="Prof. Dr. Khanuka" 
android:id="@+id/DiscriptionText" 
/> 

</LinearLayout> 

,然後再製作此處的Custem列表custom list

,並根據您編輯代碼

感謝

相關問題