无法从父布局中获取子计数

时间:2014-05-21 05:10:40

标签: android

MainActivity

 public class MainActivity extends Activity {
    CustomAutoCompleteTextView cat;
    SimpleAdapter adapter;
    ArrayList<HashMap<String, String>> values = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name();

        cat = (CustomAutoCompleteTextView) findViewById(R.id.a_no);
        String[] from = { "NAME", "ID" };
        int[] to = { R.id.auto_id, R.id.auto_name };
        adapter = new SimpleAdapter(MainActivity.this, values, R.layout.row, from,
                to);
        cat.setAdapter(adapter);
        cat.setThreshold(1);
        cat.setOnItemClickListener(autoItemSelectedListner);


    }


    private OnItemClickListener autoItemSelectedListner = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            HashMap<String, String> hm = (HashMap<String, String>) arg0
                    .getAdapter().getItem(arg2);

             LinearLayout v1 = (LinearLayout) arg1.getParent();
             Log.i("tag",v1.getChildCount() +"");


        }

    };

CustomAutoCompleteTextView

public class CustomAutoCompleteTextView extends AutoCompleteTextView {

        public CustomAutoCompleteTextView(Context context,AttributeSet attrs) {
            super(context, attrs);
        }
        @Override
        protected CharSequence convertSelectionToString(Object selectedItem) {

            HashMap<String, String> hm = (HashMap<String, String>) selectedItem;
            return hm.get("NAME");
        }
    }


        }

row.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:orientation="vertical"
    android:tag="kasun" >

    <TextView
        android:id="@+id/auto_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:clickable="false"
        android:focusable="false"
        android:paddingLeft="5dp"
        android:textSize="22sp" >
    </TextView>

    <TextView
        android:id="@+id/auto_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:clickable="false"
        android:focusable="false"
        android:paddingLeft="5dp"
        android:textSize="22sp" >
    </TextView>


</LinearLayout>

activity_main.xml中

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPassword" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <com.example.autocomplete.CustomAutoCompleteTextView
            android:id="@+id/a_no"
            android:layout_width="wrap_content"
            android:layout_height="100dip"
            android:ems="10"
            android:hint="Account No" >
        </com.example.autocomplete.CustomAutoCompleteTextView>

    </LinearLayout>

在上面的例子中,我尝试使用自定义自动完成填充一些数据。这个代码正如我所期望的那样。但是当从自动完成中选择一个值时,我需要计算父布局中的子窗口小部件。这个父布局是线性布局,它包含3个小部件。(1个自定义自动完成和2个编辑文本)。你可以在main_activity.xml上看到它。它给出了以下错误

05-21 10:34:01.821: E/AndroidRuntime(4118): java.lang.ClassCastException: android.widget.ListPopupWindow$DropDownListView cannot be cast to android.widget.LinearLayout

3 个答案:

答案 0 :(得分:0)

此处arg1视图表示您的自动完成文本视图,您编写arg1.getParent()表示您获得CustomAutoCompleteTextView。然后将其转换为线性布局,以便生成错误。你必须编写arg1.getParent()。getParent()然后你得到LinearLayout。试试吧......

OR

你也可以写arg0.getParent()然后你得到LinearLayout。

答案 1 :(得分:0)

arg0是您点击某个项目的弹出式视图。并且arg1是您单击的弹出视图的特定项目。这样arg1.getParent()就会为你提供popupview。并且arg0为您提供了popupView,并且您尝试将其转换为linearlayout。这样你就得到了ClassCastException。

<强>更新

There were no reference found in the onItemClickListener(). So I get the parent from the instance of autocomplettextview as,

ll = (LinearLayout) cat.getParent(); 
Log.i("bbbbbbbbbbbbbbbbb", ll.getChildCount() + "");

如果要创建autocompletetextview,只需使用linearlayout数组并在linearlayout数组中添加autocompleteview。

答案 2 :(得分:0)

所以如果我理解你的话,请尝试这个课程:

class Row extends LinearLayout implements OnItemClickListener {

    private EditText et0;
    private EditText et1;
    private AutoCompleteTextView actv;

    public Row(Context context) {
        super(context);
        et0 = new EditText(context);
        et1 = new EditText(context);
        actv = new AutoCompleteTextView(context);
        actv.setHint("enter D...");
        actv.setThreshold(1);
        actv.setOnItemClickListener(this);

        LayoutParams p = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
        addView(et0, p);
        addView(et1, p);
        addView(actv, p);
        String[] columnNames = {BaseColumns._ID, "name0", "name1"};
        MatrixCursor c = new MatrixCursor(columnNames);
        for (int i = 0; i < 10; i++) {
            c.newRow().add(i).add("DOG " + i).add("CAT " + i);
        }
        String[] from = {"name0"};
        int[] to = {android.R.id.text1};
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, android.R.layout.simple_dropdown_item_1line, c, from, to, 0);
        adapter.setStringConversionColumn(1);
        actv.setAdapter(adapter);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        CursorAdapter a = (CursorAdapter) parent.getAdapter();
        Cursor c = a.getCursor();
        et0.setText(c.getString(1));
        et1.setText(c.getString(2));
    }
}

测试代码如下(在onCreate中添加):

LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i < 5; i++) {
    ll.addView(new Row(this));
}
setContentView(ll);