我想最初在列表视图中隐藏编辑文本和按钮字段,并在单击该行时仅在列表视图中为特定行(单击的行)显示编辑文本和按钮。
我使用以下自定义布局来创建列表视图
<?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);
...
提前致谢!