Android列表视图在单击项目时显示隐藏的编辑文本

时间:2013-10-03 15:06:21

标签: android android-layout android-listview android-emulator android-edittext

我想在列表视图中隐藏编辑文本和按钮字段,并在单击该行时显示仅列表视图中特定行(单击行)的编辑文本和按钮。

我使用以下自定义布局来创建列表视图

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

<ImageView
android:id="@+id/emp_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="18dp"
android:adjustViewBounds="true"
android:maxHeight="80dp"
android:maxWidth="80dp"
android:src="@drawable/person" />

<TextView
android:id="@+id/emp_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/emp_avatar"
android:layout_toRightOf="@+id/emp_avatar"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
android:id="@+id/empPin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/emp_avatar"
android:layout_alignRight="@+id/emp_number"
android:layout_toRightOf="@+id/emp_avatar"
android:ems="10"
android:inputType="number" 
android:focusable="false"
android:focusableInTouchMode="false"
android:visibility="invisible">

<requestFocus />
</EditText>

在这个布局中,我使用了android:visibility="invisible"

以下是Activity.java的代码

public class MainPortal extends Activity {
private List<Employee> employees = new ArrayList<Employee>();
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_portal);
populateEmployeeList();
//populsteListView();

ListView list = (ListView) findViewById(R.id.employeeListView);
ArrayAdapter<Employee> adapter = new MylistAdapter();
list = (ListView) findViewById(R.id.employeeListView);
list.setAdapter(adapter);

 list.setOnItemClickListener(new OnItemClickListener() {
      public void onItemClick(AdapterView<?> parent, View view,
              int position, long id) {
              et.setVisibility(View.VISIBLE);
              adapter.notifyDataSetChanged();

          }
        });


private void populateEmployeeList() {
  ...
}



private class MylistAdapter extends ArrayAdapter<Employee>{


public MylistAdapter(){
    super(MainPortal.this,R.layout.item_view,employees);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View itemView = convertView;
    if(itemView==null){
        itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
    }
    Employee currentEmployee = employees.get(position);
    //Avatar
    ImageView imageView = (ImageView) itemView.findViewById(R.id.emp_avatar);
    imageView.setImageResource(currentEmployee.getAvatarId());
    //Employee number
    TextView emp_id = (TextView) itemView.findViewById(R.id.emp_number);
    emp_id.setText(currentEmployee.getId());

    et = (EditText) itemView.findViewById(R.id.empPin);
    return itemView;
}
}
}

我在列表视图的点击侦听器中使用了以下代码,以便在项目点击时显示编辑文本。

editText.setVisibility(View.VISIBLE);     
adapter.notifyDataSetChanged();

但是在单击行之后,编辑文本字段不会出现。我在这里做错了什么?有没有人知道如何做到这一点?

EditText的初始化:

View itemView = convertView;
        if(itemView==null){
            itemView = getLayoutInflater().inflate(R.layout.item_view, parent,false);
        }

        et = (EditText) itemView.findViewById(R.id.editText1);
...

提前致谢!

1 个答案:

答案 0 :(得分:2)

item_view.xml中的EdiTtext不在activity_main_portal中。您正在getView

中对自定义布局进行充气

您需要初始化edittext

list.setOnItemClickListener(new OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,
          int position, long id) {
          View v = (View)view.getParent();
          EditText ed1 = (EditText) v.findViewById(R.id.empPin);  
          ed1.setVisibility(View.VISIBLE);
          adapter.notifyDataSetChanged();

      }
    });