如何从ListView测试应用程序获取TextView?

时间:2012-03-10 08:49:54

标签: android listview textview

我需要在strings.xml文件中使用字符串数组资源测试ListView应用程序并使用以下布局文件:

<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

在我的ListViewTest应用程序中,如何将TextView小部件(可能)与SpinnerTest应用程序相似?如下面的陈述:

TextView resultView =                 (TextView)mActivity.findViewById(com.android.example.spinner.R.id.SpinnerResult);

我正在从SpinnerTest示例中学习编写ListViewTest。感谢您的指导/建议!


更新

以下是测试目标:

package com.mypackage;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MyListView extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String[] countries = getResources().getStringArray(R.array.countries_array);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
            this,                   // Application context
            R.layout.list_item,     // layout description for each list item
            countries);         // String array of countries defined
        setListAdapter(arrayAdapter);


        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // When clicked, show a toast with the TextView text
            Toast.makeText(getApplicationContext(), "((TextView) view).getText() + 
            "is selected " + Toast.LENGTH_SHORT).show();
        }
    });

       // The setTextFilterEnabled(boolean) method turns on text filtering for the 
       // ListView, so that when the user begins typing, the list will be filtered.
       lv.setTextFilterEnabled(true);

    }

}


Strings.xml of the app under test:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">adapter_ListView_StringArray_Resource</string>
<string-array name="countries_array">
    <item>Algeria</item>        
    <item>Cameroon</item>       
    <item>Ghana</item>      
    <item>Ivory Coast</item>
    <item>Nigeria</item>        
    <item>South Africa</item>       
</string-array>
</resources>

0 个答案:

没有答案