我对这个问题有点挣扎。所以我有可扩展的列表,我想制作用于输入数据的编辑文本。我创建了一个按钮,因此我可以在按钮激活(On Click)上使用输入的数据。我还创建了一个编辑文本视图。但是当我想要接收这些数据时,没有任何反应,我使用edittex.getText().toString()
。我使用Log.i进行测试,但logger甚至没有出现在logcat上。
ExpandableListAdapter
package lt.whiteGroup.ultimateshoppinglist;
import java.util.logging.Logger;
import android.app.Activity;
import android.util.Log;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private final SparseArray<Group> groups;
public LayoutInflater inflater;
public Activity activity;
private Button but;
private EditText edittex;
public MyExpandableListAdapter(Activity act, SparseArray<Group> groups) {
activity = act;
this.groups = groups;
inflater = act.getLayoutInflater();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return groups.get(groupPosition).children.get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String children = (String) getChild(groupPosition, childPosition);
TextView text = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_details, null);
}
text = (TextView) convertView.findViewById(R.id.textView1);
text.setText(children);
// convertView.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(activity, children,
// Toast.LENGTH_SHORT).show();
// }
// });
but = (Button) convertView.findViewById(R.id.Button);
edittex = (EditText) convertView.findViewById(R.id.ET);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText et = ((ViewGroup)v.getParent()).getChildAt(0);
System.out.println(et.getText().toString());
// Log.i("dsds", c);
}
});
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return groups.get(groupPosition).children.size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public void onGroupExpanded(int groupPosition) {
super.onGroupExpanded(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.listrow_group, null);
}
Group group = (Group) getGroup(groupPosition);
((CheckedTextView) convertView).setText(group.string);
((CheckedTextView) convertView).setChecked(isExpanded);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
LOG
09-10 21:54:37.851: E/AndroidRuntime(1025): FATAL EXCEPTION: main
09-10 21:54:37.851: E/AndroidRuntime(1025): java.lang.ClassCastException: android.widget.TextView
09-10 21:54:37.851: E/AndroidRuntime(1025): at lt.whiteGroup.ultimateshoppinglist.MyExpandableListAdapter$1.onClick(MyExpandableListAdapter.java:70)
09-10 21:54:37.851: E/AndroidRuntime(1025): at android.view.View.performClick(View.java:2485)
09-10 21:54:37.851: E/AndroidRuntime(1025): at android.view.View$PerformClick.run(View.java:9080)
09-10 21:54:37.851: E/AndroidRuntime(1025): at android.os.Handler.handleCallback(Handler.java:587)
09-10 21:54:37.851: E/AndroidRuntime(1025): at android.os.Handler.dispatchMessage(Handler.java:92)
09-10 21:54:37.851: E/AndroidRuntime(1025): at android.os.Looper.loop(Looper.java:130)
09-10 21:54:37.851: E/AndroidRuntime(1025): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-10 21:54:37.851: E/AndroidRuntime(1025): at java.lang.reflect.Method.invokeNative(Native Method)
09-10 21:54:37.851: E/AndroidRuntime(1025): at java.lang.reflect.Method.invoke(Method.java:507)
09-10 21:54:37.851: E/AndroidRuntime(1025): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-10 21:54:37.851: E/AndroidRuntime(1025): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-10 21:54:37.851: E/AndroidRuntime(1025): at dalvik.system.NativeStart.main(Native Method)
ListRowDetails布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="40dp"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_launcher"
android:drawablePadding="5dp"
android:gravity="center_vertical"
android:text="@string/hello_world"
android:textSize="14sp"
android:textStyle="bold"
>
</TextView>
<View
android:layout_below="@id/textView1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<Button
android:id="@+id/Button"
android:layout_marginTop="1dp"
android:layout_below="@id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to list"
android:onClick="ButtonFunction"
/>
<EditText
android:id="@+id/ET"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/textView1"
android:inputType="text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Quantity"
/>
</RelativeLayout>
答案 0 :(得分:1)
您无法获取任意创建/添加的视图的值。在onClick事件中,edittex
指的是类范围EditText edittex
变量(从未初始化/使用BTW)。由于onClick侦听器将在未确定的时间被调用,因此这不是您应该引用EditText字段的方式。假设您有以下布局:
<LinearLayout>
<EditText />
<Button />
</LinearLayout>
你应该做一些不依赖于在onClickListener之外初始化的变量的东西,比如:
// Make sure to that you are referencing the button variable and not the Button class here
yourButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
EditText et = (EditText) ((ViewGroup)v.getParent()).getChildAt(0);
System.out.println(et.getText().toString());
// You can use the logger if you want, but I always find it easier to use System.out if I'm just printing out one value.
}
});
<强> Explination:强>
通常,当使用ExpandableListView,ListView或任何其他允许向其添加任意数量视图的视图时,我发现最简单的方法是转到所单击视图的父级以查找任何同级视图。您不能引用已在其他方法中声明的局部变量。因此,这一行:String c = edittex.getText().toString();
实际上是指具有类范围的EditText edittex
变量(您在类顶部声明的那个)。该变量是:a)从未初始化,或b)该值被后续方法调用覆盖。因此,当您引用它时,您不会引用您认为自己的变量。
为了解决这个问题,您需要做的就是使用作为参数传递的View
来获取包含它的父级(如果您的布局与我的布局相似,则它是ViewGroup的子类)向你展示了一个例子)。完成后,您可以通过getChildAt()
方法访问父级的任何子视图。在这种情况下,我假设EditText
视图是父视图的第一个子视图(第0个索引处的视图)。根据需要调整索引。
另外,这一行:Button.setOnClickListener(new OnClickListener() {
应该是Button
变量的名称,而不是Button
类。