1

我在导航抽屉中的列表视图中实现了搜索功能。但目前它只有一个文本视图。我希望能够在同一行添加两个文本视图,而不需要不同的类或适配器。话虽如此,我仍然希望能够搜索第一个 textview。所以基本上我的问题是:如何在不使用任何其他类的情况下将两个文本视图添加到列表视图中的一行?

这是我的代码:listitem_row.xml

<!-- This is the xml which i use to display one textview on a list row-->

<?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="50dp"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical">

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>

</LinearLayout>

main_activity.xml

<!--This is my main activity xml where I have the navigation drawer and the  
listview inside the drawer-->

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:background="#000000"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Swipe from the left to open the drawer"
        android:id="@+id/textView"
        android:layout_gravity="center" />
</FrameLayout>

<LinearLayout
    android:id="@+id/left_drawer_layout"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_marginLeft="-65dp"
    android:background="#111"
    android:orientation="vertical"
    android:layout_gravity="start" >

    <EditText
        android:id="@+id/searchbar"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:textColor="#bfc2d1"
        android:singleLine="true"
        android:drawableLeft="@drawable/search"
        android:padding="10dp"
        android:background="@drawable/search_bar"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:imeOptions="flagNoExtractUi"
        android:hint="Search" >
    </EditText>

<RelativeLayout
    android:id="@+id/empty"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:layout_marginLeft="-65dp"
    android:background="#111"
    android:orientation="vertical"
    android:layout_gravity="start" >
    <TextView
        android:id="@+id/empty_text"
        android:text="@string/notfound"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textColor="#ffababab"
        android:textSize="15dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:gravity="center">
    </TextView>
    </RelativeLayout>

    <ListView android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</LinearLayout>

</android.support.v4.widget.DrawerLayout>

MainActivity.java

//This is my mainactivity
public class MainActivity extends Activity {
final String[] elename ={"Hydrogen","Helium","Lithium","Beryllium","Boron","Carbon","Nitrogen","Oxygen","Fluorine","Neon","Sodium","Magnesium","Aluminium","Silicon","Phosphorous","Sulphur","Chlorine","Argon","Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron","Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton","Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium","Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon","Caesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium","gadoliium","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium","Hafnium","Tantalum","Tungsten","Rhenium","Osmium","Iridium","Platinum","Gold","Mercury","Thallium","Lead","Bismuth","Polonium","Astatine","Radon","Francium","Radium","Actinium","Thorium","Protactinium","Uranium","Neptunium","Plutonium","Americium","Curium","Berkelium","Californium","Einsteinium","Fermium","Mendelevium","Nobelium","Lawrencium","Rutherfordium","Dubnium","Seaborgium","Bohrium","Hassium","Meitnerium","Darmstadtium","Roentgenium","Copernicium","Ununtrium","Ununquadium","Ununpentium","Ununhexium","Ununseptium","Ununoctium"};
final String[] nos = {"1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52","53","54","55","56","57","58","59","60","61","62","63","64","65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","82","83","84","85","86","87","88","89","90","91","92","93","94","95","96","97","98","99","100","101","102","103","104","105","106","107","108","109","110","111","112","113","114","115","116","117","118"};

//I have already added the String elename to my listview
//and I want to add String nos to it without using any other class. 
ArrayAdapter<String> adapter;

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

    //The line below adds the string elename to the listview
    //but using the same line i want to add the string nos if possible.
    adapter = new ArrayAdapter<String>(this, R.layout.listitem_row,R.id.textView1, elename);
    final EditText searchBar = (EditText) findViewById(R.id.searchbar);
    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    final ListView navList = (ListView) findViewById(R.id.left_drawer);
    final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
    navList.setAdapter(adapter);


    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
            //Change view according to numbers

            drawer.closeDrawer(linearLayout);
        }
    });

    //filter list view after search instantly
    searchBar.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            //The line below removes all the spaces while searching
            String searchedquery = cs.toString().replaceAll(" ","");
            //The line below searches the listview and shows the results
            MainActivity.this.adapter.getFilter().filter(searchedquery);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {

        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

    //hide the keyboard after search on touch list view
    navList.setOnScrollListener(new AbsListView.OnScrollListener() {
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            //The code below hides the keyboard when the user touches the listview
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(navList.getWindowToken(), 0);
        }

        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        }
    });

    navList.setEmptyView(findViewById(R.id.empty));
  }
}
4

2 回答 2

0
<?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="50dp"
android:orientation="horizontal"
android:padding="10dp"
android:gravity="center_vertical">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"/>

</LinearLayout>
于 2014-01-28T10:27:17.150 回答