2016-03-03 81 views
0

我想通過它的位置顯示列表視圖中的項目。例如:如果我點擊第三位,我需要顯示第三項。這是我的代碼。我想在單擊項目按鈕時顯示。如何在Android中按位置顯示列表視圖中的項目

for (int k = 0; k < jsonarrays.length(); k++) { 
      JSONObject main_menus = jsonarrays.getJSONObject(k); 
      SUB_MENU_ID = main_menus.getString(TAG_SUB_MENU_ID); 
      SUB_MENU_NAME = main_menus.getString(TAG_SUB_MENU_NAME); 
      HashMap<String, String> submenu = new HashMap<String, String>(); 
      submenu.put(TAG_SUB_MENU_ID, SUB_MENU_ID); 
      submenu.put(TAG_SUB_MENU_NAME, SUB_MENU_NAME); 
      submenus.add(submenu); 
} 
@Override 
protected void onPostExecute(String result) { 
     submenuadapter = new SubmenufoodCategoryAdapter(submenus, getActivity()); 
     sb_list.setAdapter(submenuadapter); 
     sb_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

     } 
     });} 

這是我的適配器類別:

public class SubmenufoodCategoryAdapter extends BaseAdapter { 
    ArrayList<HashMap<String, String>> mainmenu; 
    HashMap<String, String> mainmenumap = new HashMap<String, String>(); 
    Context con; 
    public SubmenufoodCategoryAdapter(ArrayList<HashMap<String, String>> submenu, Context con) { 
     super(); 
     this.mainmenu = submenu; 
     this.con = con; 
    } 
    @Override 
    public int getCount() { 
     return mainmenu.size(); 
    } 
    @Override 
    public Object getItem(int position) { 
     return position; 
    } 
    @Override 
    public long getItemId(int position) { 
     return position; 
    } 
    @SuppressLint("ViewHolder") 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     final Holder holder = new Holder(); 
     LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     mainmenumap = mainmenu.get(position); 
     View rowview; 
     rowview = layoutInflater.inflate(R.layout.submenu_button, null); 
     holder.btn = (Button) rowview.findViewById(R.id.submenu_layout_button); 
     holder.btn.setText(mainmenumap.get(FoodMenuItemFragment.TAG_SUB_MENU_NAME)); 
     return rowview; 
    } 
    public class Holder { 
     Button btn; 
    } 
} 

回答

1

不要比較此代碼與您的要求..我只是給你想法..

我認爲你正試圖從充氣適配器的所有數據,以列表視圖..我做了我在哪裏顯示來自適配器的所有細節,以列表視圖的對話,以便

builder.setAdapter(notes_second_adapter, 
      new DialogInterface.OnClickListener() { 

       @Override 
       public void onClick(DialogInterface dialog, int position) { 

        notes_child_category_second.setText(notes_second_list 
          .get(position).get(KIDZDAY_TAG_DESCRIPTION)); 
        acti_id_to_save = notes_second_list.get(position).get(
          KIDZDAY_TAG_REF_ID); 
        //sampel_1 = "Nihal Srivastava"; 

       } 
      }); 

    dialog = builder.create(); 
    dialog.show(); 
內的樣本

這裏你可以看到。在獲得數據到適配器後,您可以撥打ArrayList.get(position).value該字段 ..通過點擊它可以根據位置獲取數據

0

發送這些數據你可以給嘗試下面的代碼片段:

sb_list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    //save data form here to some variables and on button click send data to where you want to show whether in new activity or something else 

      sbmenus.get(2).get(FoodMenuItemFragment.TAG_SUB_MENU_NAME); 

       } 
      });} 
0

您可以使用AlertDialog。 Here就是一個例子,回答2.

public void dialogBox(String message) { 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); 
    alertDialogBuilder.setMessage(message); 
    alertDialogBuilder.setPositiveButton("Ok", 
     new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface arg0, int arg1) { 
     } 
    }); 

    alertDialogBuilder.setNegativeButton("cancel", 
     new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface arg0, int arg1) { 

     } 
    }); 

    AlertDialog alertDialog = alertDialogBuilder.create(); 
    alertDialog.show(); 
} 

請撥打itemClick在()的對話框(String)方法,你要顯示的數據。

相關問題