如何禁用我刚刚在android的listview中点击的列表项?

时间:2014-04-28 14:31:36

标签: android listview android-listview

我在列表视图中显示学生列表(姓名,代号)的活动。 它实际上是为了让学生参加。用户点击listview项目(显示他的名字),然后打开一个对话框,学生输入密码并点击" present"按钮。每当学生点击"出现"按钮我想禁用刚刚单击的列表视图项。这意味着如果学生再次点击相同的列表视图项目,那么他就不应该被允许这样做。 如果有人帮助我禁用列表视图项,那将是一件非常愉快的事。

这是studname_listview.xml。这是listview的内容。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView
    android:id="@+id/rollnumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:text="11093"
    android:textStyle="bold" />

<TextView
    android:id="@+id/firstname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10sp"
    android:layout_marginLeft="20dp"
    android:text="roger"
    android:textSize="16sp"
    android:textStyle="bold" >
</TextView>


<TextView
    android:id="@+id/lastname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="federrer"
    android:textSize="16sp"
    android:textStyle="bold" />

</LinearLayout>

这是student_main.xml:这是listview所在的位置。在此列表视图中,我显示了studmain_list_xml的内容          

    <ListView
        android:id="@+id/id_student_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp"
        android:listSelector="@drawable/list_selector" />
</LinearLayout>

我发布了迄今为止已成功编写的onclicklistener代码,除了禁用listview项目外,它还执行了我提到的所有内容。

这是DisplayStudentActivity.java,每当我点击名为&#34; id_student_list&#34;的列表视图的任何项目时都会调用它。

public class DisplayStudentActivity extends Activity   {

private ListView listView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.student_main);  // listview file name

ArrayList<HashMap<String, String>> Items = new ArrayList<HashMap<String, String>>();

 // Adding Items to ListView
   OverlordSimpleAdapter adapter = new OverlordSimpleAdapter(this, Items,
            R.layout.studname_listview, new String[] { "firstname", "lastname", "roll" },
          new int[] {R.id.firstname, R.id.lastname, R.id.rollnumber });

    listView = (ListView) findViewById(R.id.id_student_list);   // list view cha naav
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            // TODO Auto-generated method stub
            { 
AlertDialog.Builder alertDialog = new AlertDialog.Builder(DisplayStudentActivity.this);
                alertDialog.setTitle(db_fname + " "+ db_lname);
                alertDialog.setMessage("Enter your Password");

                final EditText input = new EditText(DisplayStudentActivity.this);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                                        LinearLayout.LayoutParams.MATCH_PARENT,
                                        LinearLayout.LayoutParams.MATCH_PARENT);
                  input.setLayoutParams(lp);
                  alertDialog.setView(input);

                 alertDialog.setPositiveButton("PRESENT",new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int which)
                    {
// When the control comes here i.e. "PRESENT" button is clicked, I wish to
 //disable only the item of listview which was clicked to reach here.
//an anyone come up with a working code?
                    }
                });

                alertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        dialog.cancel();
                    }
                });

                alertDialog.show();
            }

        // inside this 
        }
        });

    db.close();
}





public class OverlordSimpleAdapter extends SimpleAdapter {

public OverlordSimpleAdapter(Context context,
        List<? extends Map<String, String>> Items, int resource, String[] from,
        int[] to) {
    super(context, Items, resource, from, to);
    // TODO Auto-generated constructor stub
}

@Override public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
   boolean wasClicked =listView.isItemChecked(position);
    view.setEnabled(!wasClicked);
    return view; 
}

} }

1 个答案:

答案 0 :(得分:0)

您可能想要尝试名为setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);的ListView功能 (android:choiceMode = xml中的“multipleChoice”)

见这里:http://developer.android.com/reference/android/widget/AbsListView.html#setChoiceMode(int)

这将存储每个列表项选择/未选择的状态,并在滚动列表时正确刷新它。

然后根据该项目的getView状态,在适配器的setEnabled电话isItemChecked(int)上发送电子邮件{/ 1}}。

修改

@Override public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position, convertView, parent);
    boolean wasClicked = mListView.isItemChecked(position);
    view.setEnabled(!wasClicked);
    return view; 
}