我还是Android新手。我想在我创建的ListView中访问TextEdit视图。每个ListView项都包含一个线性布局,而线性布局又包含一个EditText。
我正在尝试这样做而且它不起作用。
View v = MyListView.getChildAt(2);
EditText et = (EditText) v.findViewById(R.id.edittext_1);
其中edittext_1位于定义ListView中每个LinearLayout项的xml中。
编辑:更多代码供参考。这在我的activity的片段中调用以创建ListView。
Resources res = getResources();
GradeTypeListView = (ListView) rootView.findViewById(R.id.gtListView);
// Applies the adapter to populate the ListView
gtAdapter = new GradeTypeListAdapter(this.getActivity(), gradeTypeList, res, this);
GradeTypeListView.setAdapter(gtAdapter);
我的适配器使用LinearLayout模板为每个ListView项目充气。适配器的getView()代码是
@Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
ViewHolder holder;
// If the view doesn't yet exist, assign views in templates to views in holder.
if(convertView == null){
v = inflater.inflate(R.layout.course_list_item, null);
holder = new ViewHolder();
holder.title = (TextView) v.findViewById(R.id.title);
holder.comment = (TextView) v.findViewById(R.id.comment);
holder.letterGrade = (TextView) v.findViewById(R.id.letterGrade);
holder.percentage = (TextView) v.findViewById(R.id.percentage);
v.setTag(holder);
// If the view exists, then holder equals the object returned by getTag(). getTag is a method
// that allows a view to basically hold an object.
} else
holder = (ViewHolder) v.getTag();
if(data.size() <= 0 ) {
holder.title.setText("No Data");
System.out.println("Empty I guess");
// Set the views' contents and onClickListener
} else {
course = null;
course = (CourseWithGrade) data.get(position);
holder.title.setText(course.getTitle());
holder.comment.setText("Hard-coded comment!");
holder.letterGrade.setText(course.letterGrade);
holder.percentage.setText(course.percentage);
v.setOnClickListener(new OnItemClickListener(position));
}
return v;
}
R.layout.course_list_item XML
<?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="wrap_content"
android:layout_margin="15dp"
android:paddingRight="10dp" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:maxWidth="110dp"
android:text="TextView" />
<TextView
android:id="@+id/letterGrade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginRight="35dp"
android:layout_toLeftOf="@+id/percentage"
android:text="TextView" />
<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/title"
android:maxWidth="110dp"
android:text="TextView"
android:textSize="12sp" />
<TextView
android:id="@+id/percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="TextView" />
</RelativeLayout>