1

自定义列表视图项目在滚动或单击编辑文本时消失。

自定义 adater 值绑定到列表的主类

package com.example.customlistview;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
    ArrayList<String> nameList;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            ListView list = (ListView)findViewById(R.id.listView1);
            nameList = new ArrayList<String>(); 
            nameList.add("Iphone");
            nameList.add("Android");
            nameList.add("Samsung");
            nameList.add("BlackBery");
            nameList.add("Lava");
            nameList.add("T Mobile");
            nameList.add("Lg Mobile");
            CustomAdapter adapter = new CustomAdapter(this,nameList);
            list.setAdapter(adapter);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }      
    }
}

自定义适配器类。该类创建要绑定到列表的视图

        package com.example.customlistview;
        import java.util.ArrayList;
        import android.content.Context;
        import android.view.LayoutInflater;
        import android.view.View;
        import android.view.ViewGroup;
        import android.widget.ArrayAdapter;
        import android.widget.TextView;

        public class CustomAdapter extends  ArrayAdapter<String> {
            TextView name;
            TextView add;
            TextView state;
            public Context context;
            ArrayList<String> List;
            View rowView;
            String str1,str2,str3;
            public CustomAdapter(Context context, ArrayList<String> nameList) {
                super(context, 0,nameList);
                List = nameList;
                this.context = context;
            }
            @SuppressWarnings("null")
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                try {
                    str1 = List.get(position);
                    str2 = List.get(position);
                    str3 = List.get(position);
                } catch (Exception e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                if( convertView==null){
                    LayoutInflater inflater = (LayoutInflater) context
                            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                     rowView = inflater.inflate(R.layout.customlayout, parent, false);
                     TextView name = (TextView) rowView.findViewById(R.id.nameT);   
                     TextView add= (TextView) rowView.findViewById(R.id.addT);          
                     TextView state = (TextView) rowView.findViewById(R.id.stateT);
                    if(name != null) {
                        try {
                            name.setVisibility(View.VISIBLE);  
                            name.setText(str1);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }   
                    }else {

                        name.setVisibility(View.INVISIBLE); 
                    }
                    if(add != null) {
                        add.setVisibility(View.VISIBLE); 
                        add.setText(str2);;

                    }else {
                        add.setVisibility(View.INVISIBLE); 
                    }
                    if(state != null) {
                        state.setVisibility(View.VISIBLE);
                        state.setText(str3);;
                    }else {
                        state.setVisibility(View.INVISIBLE); 
                    }
                }
                return rowView;

            }

        }

主要 Xml 是

<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="match_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:ems="10" >

            <requestFocus />
        </EditText>

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="10dp"
            android:layout_weight="1.67"
            android:cacheColorHint="#00000000" >
        </ListView>
    </LinearLayout>

</RelativeLayout>

自定义 XML 是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="80dp" >
            <TextView
            android:id="@+id/nameT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="TextView" />

        <TextView
            android:id="@+id/addT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nameT"
            android:layout_marginLeft="60dp"
            android:layout_toRightOf="@+id/nameT"
            android:text="TextView" />



        <TextView
            android:id="@+id/stateT"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="40dp"
            android:layout_marginRight="38dp"
            android:text="TextView" />

    </LinearLayout>

点击编辑文本之前--->第一张图片。点击编辑文本之后--->第二张图片

最后,当虚拟键盘按下时,列表中只显示一项

4

2 回答 2

1
public View getView(int position, View convertView, ViewGroup parent) {

             System.out.println("position"+position);

   View     rowView=convertView;
        if( rowView==null){

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             rowView = inflater.inflate(R.layout.row, parent, false);
        }
             TextView name = (TextView) rowView.findViewById(R.id.nameT);   
             TextView add= (TextView) rowView.findViewById(R.id.addT);          
             TextView state = (TextView) rowView.findViewById(R.id.stateT);

            if(name != null&&add != null&&state != null) {

              str1 = List.get(position);
              str2 = List.get(position);
              str3 = List.get(position);
                  //  name.setVisibility(View.VISIBLE);  
                    name.setText(str1);
                 // add.setVisibility(View.VISIBLE); 
                  add.setText(str2);
               //  state.setVisibility(View.VISIBLE);
                state.setText(str3);

            }else {

                name.setVisibility(View.INVISIBLE); 
                add.setVisibility(View.INVISIBLE); 
                state.setVisibility(View.INVISIBLE); 
            }



        System.out.println("pos"+position);
        return rowView;

    }
于 2012-09-11T08:45:30.343 回答
0

就我而言,关键部分是:

public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    if (rowView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.row, parent, false);
    }
//...
}
于 2014-08-01T15:35:16.977 回答