向下或向上滚动列表视图时,编辑文本值变为空

时间:2013-03-11 07:05:59

标签: android android-listview

我的listview有问题。在edittext中输入文本时,edittext(我在其中键入)的焦点转到listview中的另一个edittext。此外,当我向下滚动列表视图时,最顶层的edittext中的值将下降到屏幕中可见的最后一个edittext。帮助我。

我的single_row.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >

<TextView
    android:id="@+id/textViewSingleRowAddEditCategory"
    android:layout_width="250dp"
    android:layout_marginTop="10dp"
    android:layout_height="wrap_content"
    style="@style/font_color_and_type"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="TextView" />

<EditText
    android:id="@+id/editTextSingleRowAddEditCategory"
    android:layout_width="250dp"
    android:layout_height="40dp"
    android:layout_below="@+id/textViewSingleRowAddEditCategory"
    android:layout_centerHorizontal="true" >
</EditText>

<ImageView
    android:id="@+id/imageViewAddEditCategoryEraseData"
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:layout_alignTop="@+id/editTextSingleRowAddEditCategory"
    android:layout_toRightOf="@+id/editTextSingleRowAddEditCategory"
    android:src="@drawable/delete4" />

</RelativeLayout>

我的main_layout.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"
android:background="#ffffff"
tools:context=".AddEditCategory" >

<RelativeLayout
    android:id="@+id/relativeLayoutAddEditCategoryTopBar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >

    <TextView
        android:id="@+id/textViewAddEditScreenTitle"
        android:text="Screen Title"
        style="@style/screen_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/imageViewAddEditCategoryDelete"
        android:layout_width="22dp"
        android:layout_height="22dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="24dp"
        android:src="@drawable/set_delete" />

</RelativeLayout>

<View
    android:id="@+id/view5"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_below="@+id/relativeLayoutAddEditCategoryTopBar"
    android:background="@drawable/shadow" />

<ListView
    android:id="@+id/listViewAddEditCategory"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/view5"
    android:divider="@android:color/transparent"
    android:dividerHeight="1dp" >
</ListView>

<Button
    android:id="@+id/buttonAddEditCategorySave"
    android:layout_width="250dp"
    android:layout_height="40dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:text="Save" />

</RelativeLayout>

Main_activity.java的代码如下:

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

    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    Intent intent = getIntent();
    final String operation = intent.getStringExtra("operation");

    tv_screen_title = (TextView) findViewById(R.id.textViewAddEditScreenTitle);
    tv_screen_title.setText(operation);

    iv_delete = (ImageView) findViewById(R.id.imageViewAddEditCategoryDelete);

    al_items_from_editText = new ArrayList<String>();

    btn_save = (Button) findViewById(R.id.buttonAddEditCategorySave);
    btn_save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (operation.equals("Add Category")) {
                loadDatabase();

                View view1 = listview_add_edit_category.getChildAt(0);
                EditText et = (EditText) view1
                        .findViewById(R.id.editTextSingleRowAddEditCategory);
                String category_name = et.getText() + "";
                Log.i("category_name", category_name);

                for (int i = 1; i < listview_add_edit_category
                        .getChildCount(); i++) {
                    View view = listview_add_edit_category.getChildAt(i);
                    EditText editText = (EditText) view
                            .findViewById(R.id.editTextSingleRowAddEditCategory);
                    al_items_from_editText.add(editText.getText()+"");
                    Log.i(i+"", editText.getText()+"");
            }
        }
    });

    listview_add_edit_category = (ListView) findViewById(R.id.listViewAddEditCategory);

    if (operation.equals("Add Category")) {
        iv_delete.setVisibility(View.GONE);
        manageAddCategoryList();
        mPLAdapter = new PlacesListAdapter(AddEditCategory.this,
                mPlacesData1, mPlacesData2);
        listview_add_edit_category.setAdapter(mPLAdapter);
    }
}

private void manageAddCategoryList() {

    mPlacesData1.clear();
    mPlacesData2.clear();

    mPlacesData1.add("Category Name");
    mPlacesData2.add("Name");
    mPlacesData1.add("# Field 1");
    mPlacesData2.add("Field 1");
    mPlacesData1.add("# Field 2");
    mPlacesData2.add("Field 2");
    mPlacesData1.add("# Field 3");
    mPlacesData2.add("Field 3");
    mPlacesData1.add("# Field 4");
    mPlacesData2.add("Field 4");
    mPlacesData1.add("# Field 5");
    mPlacesData2.add("Field 5");
    mPlacesData1.add("# Field 6");
    mPlacesData2.add("Field 6");
    mPlacesData1.add("# Field 7");
    mPlacesData2.add("Field 7");
    mPlacesData1.add("# Field 8");
    mPlacesData2.add("Field 8");
    mPlacesData1.add("# Field 9");
    mPlacesData2.add("Field 9");
    mPlacesData1.add("# Field 10");
    mPlacesData2.add("Field 10");
}

private void loadDatabase() {
    database = openOrCreateDatabase(database_name,
            SQLiteDatabase.OPEN_READWRITE, null);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        finish();
        startActivity(new Intent(AddEditCategory.this,
                CategoryManagement.class));
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

这就是PlaceListAdapter.java代码:

package com.walletapp;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class PlacesListAdapter extends BaseAdapter {
// private Context mContext;
private LayoutInflater mInflater;
private ArrayList<String> al_caption = new ArrayList<String>();
private ArrayList<String> al_hint = new ArrayList<String>();

public PlacesListAdapter(Context c, ArrayList<String> al_caption,
        ArrayList<String> al_hint) {
    mInflater = LayoutInflater.from(c);
    // mContext = c;
    this.al_caption = al_caption;
    this.al_hint = al_hint;
}

public int getCount() {
    return al_caption.size();
}

public Object getItem(int position) {
    return al_caption.get(position);
}

public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {
        convertView = mInflater.inflate(
                R.layout.single_row_add_edit_category, null);
        holder = new ViewHolder();

        holder.tv_caption = (TextView) convertView
                .findViewById(R.id.textViewSingleRowAddEditCategory);
        holder.et_value = (EditText) convertView
                .findViewById(R.id.editTextSingleRowAddEditCategory);
        holder.erase_data = (ImageView) convertView
                .findViewById(R.id.imageViewAddEditCategoryEraseData);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.tv_caption.setText(al_caption.get(position));
    holder.et_value.setHint(al_hint.get(position));
    holder.erase_data.setVisibility(View.GONE);

    return convertView;
}

static class ViewHolder {
    TextView tv_caption;
    EditText et_value;
    ImageView erase_data;
}
}

请帮助我。

1 个答案:

答案 0 :(得分:0)

  

holder.et_value.setHint(al_hint.get(position));

将再次设置提示,这将导致文本丢失。

如果阻止,请将此行移到内部。

>  if (convertView == null) {
>         convertView = mInflater.inflate(
>                 R.layout.single_row_add_edit_category, null);
>         holder = new ViewHolder();
> 
>         holder.tv_caption = (TextView) convertView
>                 .findViewById(R.id.textViewSingleRowAddEditCategory);
>         holder.et_value = (EditText) convertView
>                 .findViewById(R.id.editTextSingleRowAddEditCategory);
>         holder.erase_data = (ImageView) convertView
>                 .findViewById(R.id.imageViewAddEditCategoryEraseData);
>         holder.et_value.setHint(al_hint.get(position));
> 
>         convertView.setTag(holder);
> 
>     } else {
>         holder = (ViewHolder) convertView.getTag();
>     }
> 
>     holder.tv_caption.setText(al_caption.get(position))