我正在创建一个包含editText的可扩展列表。一切正常,但我想在页面的最后添加一个按钮进行搜索,并希望得到我输入的所有字段值,但我不知道该代码中该怎么做。我在互联网上找到了这个代码。
这是我的班级: -
import java.util.ArrayList;
import java.util.List;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.TextView;
public class SearchProduct extends ExpandableListActivity {
MyExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setList();
}
public void setList() {
final int LIST_SIZE = 3;
List<String> groupList = new ArrayList<String>();
List<List<String>> childList1 = new ArrayList<List<String>>();
List<List<String>> childList2 = new ArrayList<List<String>>();
List<String> childData = new ArrayList<String>();
childData.add("");
//for (int i = 0; i < LIST_SIZE; i++)
{
groupList.add("Category Name:");
childList1.add(childData);
childList2.add(childData);
groupList.add("Product Name: ");
childList1.add(childData);
childList2.add(childData);
groupList.add("Colour: ");
childList1.add(childData);
childList2.add(childData);
groupList.add("Brand: ");
childList1.add(childData);
childList2.add(childData);
}
mAdapter = new MyExpandableListAdapter(groupList, childList1,
childList2);
setListAdapter(mAdapter);
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
private List<String> groups;
private List<List<String>> childs1;
private List<List<String>> childs2;
private boolean isFocusEditText1 = true;
private LayoutInflater inflater;
public MyExpandableListAdapter(List<String> groups,
List<List<String>> childs1, List<List<String>> childs2) {
this.groups = groups;
this.childs1 = childs1;
this.childs2 = childs2;
this.inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getRowId(int groupPosition) {
return groupPosition;
}
public Object getChild(int groupPosition, int childPosition) {
return childs1.get(groupPosition).get(childPosition);
}
public Object getChild2(int groupPosition, int childPosition) {
return childs2.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return childs1.get(groupPosition).size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
Log.d("getChildView", "groupPosition = " + groupPosition
+ ", childPosition = " + childPosition + ", isLastChild = "
+ isLastChild + ", convertView = " + convertView
+ ", ViewGroup = " + parent);
if (convertView == null) {
convertView = inflater.inflate(R.layout.search_product, parent,
false);
}
EditText editText1 = (EditText) convertView
.findViewById(R.id.edittext1);
editText1
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d("onFocusChanged1", "View : " + v
+ ", hasFocus : " + hasFocus);
if (hasFocus)
isFocusEditText1 = true;
}
});
/* EditText editText2 = (EditText) convertView
.findViewById(R.id.edittext2);
editText2
.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d("onFocusChanged2", "View : " + v
+ ", hasFocus : " + hasFocus);
if (hasFocus)
isFocusEditText1 = false;
}
});
if (isFocusEditText1)
editText1.requestFocus();
else
editText2.requestFocus();
*/
return convertView;
}
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Log.d("getGroupView", "groupPosition = " + groupPosition
+ ", isExpanded = " + isExpanded + ", convertView = "
+ convertView + ", ViewGroup = " + parent);
if (convertView == null) {
convertView = inflater.inflate(R.layout.search, parent,
false);
}
if (convertView instanceof TextView) {
((TextView) convertView).setText(getGroup(groupPosition)
.toString());
}
return convertView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
这是我的search.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textColor="#fff"
android:background="@android:color/transparent"
android:paddingLeft="48dip" />
这是我的search_product.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="vertical"
android:paddingLeft="64dip"
android:background="@drawable/drop_shadow"
android:paddingRight="12dip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="@+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#C0C0C0"
android:inputType="text"
android:textColor="#000"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>