列表视图从一行移动到另一行时,编辑文本行已清除

时间:2012-01-05 04:09:30

标签: android

我有一个带有一个textveiw和一个edittext的xml。我使用这个xml在另一个xml文件的列表视图中获取不同的行,以列出该页面中的数据。

用于在listview中列出数据的xml文件如下所示

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="2dip"
    android:paddingBottom="0dip">

    <TextView 
    android:id="@+id/tv_Sort_Address"
    android:layout_width="0dip"
    style="@style/ListTextViewStyle"
    android:layout_height="wrap_content"
    android:text="No"
     android:layout_weight="2"/>

    <EditText 
    android:textColor="#000000" 
    android:layout_height="wrap_content" 
   android:layout_width="0dip"
    android:inputType="number" 
    style="@style/EditTextStyle"
    android:background="@android:drawable/editbox_background" 
    android:id="@+id/et_Sort_Order"
     android:layout_weight="1">

    </EditText>


      </LinearLayout>

我在Listview中填写了下面给出的另一个xml文件的数据

simpleAdapter = new SimpleAdapter(this, alist,
                R.layout.sort_order_list_row, new String[] { "Sort_Address",
                        "Sort_FKDeliveryStatus", "Sort_PKDelivery",
                        "Sort_DeliveryOrder" }, new int[] {
                        R.id.tv_Sort_Address, R.id.tv_Sort_FKDeliveryStatus,
                        R.id.tv_Sort_PKDelivery, R.id.et_Sort_Order });
        listSortOrder.setAdapter(simpleAdapter);

我的问题是,如果我在列表视图中有两行,那么我 在编辑文本行之一中放置一个值并移到另一行编辑文本,第一行中放置的值就消失了。

 <ListView android:id="@+id/lv_Sort_Order"
       style="@style/ListTextBackGround"
     android:layout_width="match_parent"  
     android:layout_height="match_parent"
     android:layout_weight="1" 
     android:layout_marginLeft="5dip"      
     android:descendantFocusability="afterDescendants"     
     android:layout_marginRight="5dip" >
     </ListView>

要解决此问题,我将android:descendantFocusability="afterDescendants"放在列表视图页面中,以便将焦点放在合并页面上。但它并没有解决问题。请有人帮帮我。

我是否需要在列表视图或合并页面中放置任何属性?

2 个答案:

答案 0 :(得分:0)

你有没有理由使用SimpleAdapter?您可以尝试使用ArrayAdapter或BaseAdapter。 检查this和任何BaseAdapter示例。

答案 1 :(得分:0)

Use this code :   

public class ListViewDemo extends Activity {

    private ListView listViewScore = null;
    private ListViewAdapter listViewAdapter = null;
    private String[] usernameArr = null;
    private String[] scoreArr = null;
    //ArrayList<String>  usernameArrLst = null ,scoreArrLst = null;
    //private Helper helper = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        listViewScore=(ListView)findViewById(R.id.listViewScore);

        usernameArr = new String[]{"Alan","Bob","Carl","Doaniel","Evan","Fred","Gram"};
        scoreArr = new String[usernameArr.length];

        //usernameArrLst = new ArrayList<String>(Arrays.asList(usernameArr));
        //scoreArrLst = new ArrayList<String>(Arrays.asList(scoreArr));

        listViewAdapter = new ListViewAdapter();
        listViewScore.setAdapter(listViewAdapter);
    }

    class ListViewAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if(usernameArr==null){
                return 0;
            }

            return usernameArr.length;
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return usernameArr[position];
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            // TODO Auto-generated method stub
            final ViewHolder viewHolder ;
            View rowView=view;

            if(rowView==null){
                LayoutInflater layoutinflate =LayoutInflater.from(ListViewDemo.this);
                rowView=layoutinflate.inflate(R.layout.listviewnewtext, parent, false);

                viewHolder = new ViewHolder();

                viewHolder.tv_Sort_Address = (TextView)rowView.findViewById(R.id.tv_Sort_Address);
                viewHolder.et_Sort_Order = (EditText)rowView.findViewById(R.id.et_Sort_Order);

                viewHolder.et_Sort_Order.addTextChangedListener(new TextWatcher() {

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count,
                            int after) {
                        // TODO Auto-generated method stub
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        // TODO Auto-generated method stub
                        scoreArr[viewHolder.ref] = s.toString();

                    }
                });

                rowView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) rowView.getTag();
            }

            viewHolder.ref = position;

            viewHolder.tv_Sort_Address.setText(usernameArr[position]);
            viewHolder.et_Sort_Order.setText(scoreArr[position]);

            return rowView;
        }

    }//class ends

    class ViewHolder{
        TextView tv_Sort_Address;
        EditText et_Sort_Order;
        int ref;
    }
}