2012-12-05 4 views
0

안녕하세요 친구 textview 및 라디오 그룹이 포함 된 listview를 사용하고 있습니다. 이제 Listview에서 컨텍스트 메뉴를 사용해야합니다. Radio Group을 사용하지 않을 때는 작동하지만 지금은 목록 버튼에 클릭 할 수없는 라디오 버튼을 추가했습니다. 누구나 내게 해결책을 줄 수 있습니까?라디오 그룹이 포함 된 listview에서 상황에 맞는 메뉴를 사용하는 방법

이 내 메인 레이아웃

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" 
    android:orientation="vertical" > 

    <include layout="@layout/search_bar" /> 

    <ListView 
     android:id="@+id/listview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@+id/rel_my_search" 
     android:cacheColorHint="#00000000" 
     android:choiceMode="singleChoice" 
     android:clickable="true" 
     android:fastScrollEnabled="true" > 
    </ListView> 

</RelativeLayout> 

이며,이 목록보기

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativelay" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/list_bg" > 

    <TextView 
     android:id="@+id/tvname" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:maxLines="1" 
     android:padding="10dp" 
     android:textColor="#000000" 
     android:textSize="18dp" 
     android:textStyle="bold" /> 

    <RadioGroup 
     android:id="@+id/radioGroup1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:orientation="horizontal" > 

     <RadioButton 
      android:id="@+id/check_present" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_marginRight="20dp" 
      android:onClick="onPresentClick" /> 

     <RadioButton 
      android:id="@+id/check_absent" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toLeftOf="@id/check_present" 
      android:onClick="onPresentClick" /> 
    </RadioGroup> 

</RelativeLayout> 

의 행이며,이

public class MainActivity extends Activity implements TextWatcher, 
     OnItemClickListener/*, OnItemLongClickListener*/ { 



    ListView listView; 
    List<Items> items; 
    List<Items> filterArray = new ArrayList<Items>(); 

    ArrayList<Item> itemsSection = new ArrayList<Item>(); 

    NamesAdapter objAdapter = null; 

    EditText mySearch; 
    String searchString; 
    AlertDialog alert = null; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     listView = (ListView) findViewById(R.id.listview); 
     listView.setOnItemClickListener(this); 
     //listView.setOnItemLongClickListener(this); 
     registerForContextMenu(listView); 
     mySearch = (EditText) findViewById(R.id.input_search_query); 
     mySearch.addTextChangedListener(this); 

     // XML Parsing Using AsyncTask... 
     if (isNetworkAvailable()) { 
      new MyTask().execute(); 
     } else { 
      showToast("No Netwrok Connection!!!"); 
      this.finish(); 
     } 
    } 

    public void onPresentClick(View view) { 
     switch (view.getId()) { 
     case R.id.check_present: 
      Toast.makeText(MainActivity.this, "Student is Present", 
        Toast.LENGTH_LONG).show(); 
      break; 
     case R.id.check_absent: 
      Toast.makeText(MainActivity.this, "Student is Absent", 
        Toast.LENGTH_LONG).show(); 
      break; 
     } 
    } 

    // My AsyncTask start... 

    class MyTask extends AsyncTask<Void, Void, Void> { 

     ProgressDialog pDialog; 

     @Override 
     protected void onPreExecute() { 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Loading..."); 
      pDialog.show(); 
      super.onPreExecute(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      items = new NamesParser().getData(rssFeed); 

      return null; 

     } 

     @Override 
     protected void onPostExecute(Void result) { 
      if (null != pDialog && pDialog.isShowing()) { 
       pDialog.dismiss(); 
      } 

      if (null == items || items.size() == 0) { 
       showToast("No data found from web!!!"); 
       MainActivity.this.finish(); 
      } else { 
       setAdapterToListview(items); 
      } 

      super.onPostExecute(result); 
     } 
    } 

    // Textwatcher's ovveride methods 

    @Override 
    public void afterTextChanged(Editable s) { 
     filterArray.clear(); 
     searchString = mySearch.getText().toString().trim() 
       .replaceAll("\\s", ""); 

     if (items.size() > 0 && searchString.length() > 0) { 
      for (Items name : items) { 
       if (name.getName().toLowerCase() 
         .startsWith(searchString.toLowerCase())) { 

        filterArray.add(name); 
       } 
      } 
      setAdapterToListview(filterArray); 
     } else { 
      filterArray.clear(); 
      setAdapterToListview(items); 
     } 
    } 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, 
      int after) { 
    } 

    // Here Data is Filtered!!! 
    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
    } 

    // setAdapter Here.... 

    public void setAdapterToListview(List<Items> listForAdapter) { 

     itemsSection.clear(); 

     if (null != listForAdapter && listForAdapter.size() != 0) { 

      Collections.sort(listForAdapter); 

      char checkChar = ' '; 

      for (int index = 0; index < listForAdapter.size(); index++) { 

       Items objItem = (Items) listForAdapter.get(index); 

       char firstChar = objItem.getName().charAt(0); 

       if (' ' != checkChar) { 
        if (checkChar != firstChar) { 
         ItemsSections objSectionItem = new ItemsSections(); 
         objSectionItem.setSectionLetter(firstChar); 
         itemsSection.add(objSectionItem); 
        } 
       } else { 
        ItemsSections objSectionItem = new ItemsSections(); 
        objSectionItem.setSectionLetter(firstChar); 
        itemsSection.add(objSectionItem); 
       } 

       checkChar = firstChar; 

       itemsSection.add(objItem); 
      } 
     } else { 
      showAlertView(); 
     } 

     if (null == objAdapter) { 
      objAdapter = new NamesAdapter(MainActivity.this, itemsSection); 
      listView.setAdapter(objAdapter); 
      registerForContextMenu(listView); 
     } else { 
      objAdapter.notifyDataSetChanged(); 
     } 

    } 

    // Toast is here... 
    private void showToast(String msg) { 
     Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 
    } 

    // OnListClick,Get Name... 
    /*@Override 
    public boolean onItemLongClick(AdapterView<?> parent, View view, 
      int position, long id) { 
     // TODO Auto-generated method stub 
     deleteStudentAlert(id); 

     return false; 
    }*/ 

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

     Item item = itemsSection.get(position); 

     if (view.getTag().getClass().getSimpleName().equals("ViewHolderName")) { 
      Items objSchoolname = (Items) item; 
      showToast(objSchoolname.getName()); 
     } else { 
      ItemsSections objSectionsName = (ItemsSections) item; 
      showToast("Section :: " 
        + String.valueOf(objSectionsName.getSectionLetter())); 
     } 

    } 

    // Check Internet Connection!!! 

    public boolean isNetworkAvailable() { 
     ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); 
     if (connectivity == null) { 
      return false; 
     } else { 
      NetworkInfo[] info = connectivity.getAllNetworkInfo(); 
      if (info != null) { 
       for (int i = 0; i < info.length; i++) { 
        if (info[i].getState() == NetworkInfo.State.CONNECTED) { 
         return true; 
        } 
       } 
      } 
     } 
     return false; 
    } 

    // OnBackPressed... 

    @Override 
    public void onBackPressed() { 
     AlertDialog alert_back = new AlertDialog.Builder(this).create(); 
     alert_back.setTitle("Quit?"); 
     alert_back.setMessage("Are you sure want to Quit?"); 

     alert_back.setButton("No", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 

     alert_back.setButton2("Yes", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       MainActivity.this.finish(); 
      } 
     }); 
     alert_back.show(); 
    } 

    private void showAlertView() { 

     if (null == alert) 
      alert = new AlertDialog.Builder(this).create(); 

     if (alert.isShowing()) { 
      return; 
     } 

     alert.setTitle("Not Found!!!"); 
     alert.setMessage("Can not find name Like '" + searchString + "'"); 
     alert.setButton("Ok", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       dialog.dismiss(); 
      } 
     }); 
     alert.show(); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     // TODO Auto-generated method stub 
     if (v.getId() == R.id.listview) { 
      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; 
      menu.setHeaderTitle("Details"); 
      menu.add(0, 0, 0, "Delete Student"); 
      menu.add(1, 1, 1, "Add Student Below Current Student"); 
      menu.add(2, 2, 2, "Get Details of this Student"); 
      menu.add(3, 3, 3, "Bonafied or Marksheet of This Student"); 
     } 
     super.onCreateContextMenu(menu, v, menuInfo); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
     // TODO Auto-generated method stub 

     if (item.getItemId() == 0) { 
      deleteStudentAlert(0); 
     } else if (item.getItemId() == 1) { 
      deleteStudentAlert(1); 
     } else if (item.getItemId() == 2) { 
      deleteStudentAlert(2); 
     } else if (item.getItemId() == 3) { 
      deleteStudentAlert(3); 
     } 

     return super.onContextItemSelected(item); 
    } 

    private void deleteStudentAlert(long id) { 

     // TODO Auto-generated method stub 
     Log.v("Reaching ", "Into Delete???"); 
     AlertDialog alertDialogialog = new AlertDialog.Builder(
       MainActivity.this).create(); 
     alertDialogialog.setTitle(""); 
     alertDialogialog.setMessage(""); 

     alertDialogialog.setButton("Ok", new DialogInterface.OnClickListener() { 

      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       // TODO Auto-generated method stub 
       Toast.makeText(MainActivity.this, "Check working of ok", 
         Toast.LENGTH_LONG).show(); 
       final Dialog pwdDialog = new Dialog(MainActivity.this); 
       pwdDialog.setContentView(R.layout.password_dialog_layout); 
       pwdDialog.show(); 
       pwdDialog.setTitle("Are You Sure?"); 
       Button ok = (Button) pwdDialog.findViewById(R.id.btnPwdOk); 
       ok.setOnClickListener(new OnClickListener() { 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         pwdDialog.dismiss(); 
        } 
       }); 
       pwdDialog.show(); 
      } 
     }); 
     alertDialogialog.setButton("Cancel", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // TODO Auto-generated method stub 
         Toast.makeText(MainActivity.this, 
           "Check working of Cancel", Toast.LENGTH_LONG) 
           .show(); 

        } 
       }); 

    } 

} 

컨텍스트 메뉴 내가 제거하면 잘 작동 내가

을 수행하는 코드입니다 라디오 버튼하지만 라디오 버튼도 필요합니다 .. 미리 감사드립니다 ....

답변

0

이 게시물에 당신을 도울 수있다, 또한

ListView items are not clickable. why?

Custom ListView and context menu. How to get it?

이 한 번 봐 가지고이

https://github.com/gitextensions/gitextensions/issues/795

빠른 rewply 그러나이 대답은에 대한
+0

안녕하세요 QAMAR 덕분위한 것이 아닙니다 나를. –

+0

이 대답 중 하나가 도움이되었습니다. 지금은 적어도 컨텍스트 메뉴를 얻고 있지만 onContextMenuSelected() 또는 다른 기능을 호출 할 수 없습니다. –

관련 문제