如何在包含RadioGroup的listview上使用上下文菜单

时间:2012-12-05 14:27:14

标签: android listview radio-button

Hello Friends我正在使用包含textview和radio组的listview。 现在我需要在Listview上使用上下文菜单。当我没有使用Radio Group它工作但现在当我在我的列表行中添加Radio Buttons时它不可点击。 谁能给我解决方案?

这是我的主要布局

<?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>

这是listview的一行

<?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();

                    }
                });

    }

}

上下文菜单工作正常,如果我删除单选按钮,但我也需要单选按钮.. 在此先感谢....

1 个答案:

答案 0 :(得分:0)