2013-03-11 45 views
0

我有我的列表視圖的問題。當在edittext中輸入文本時,edittext(我在其中輸入)的焦點會轉到listview中的另一個編輯文本。另外當我滾動我的列表視圖的最上面的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; 
} 
} 

請幫助我的人。

+0

都都在您的ListView中的EditText項目可見,還是有不可見的物品?第二個問題是由適配器中的回收造成的。 – Neil 2013-03-11 07:11:48

+0

@Neil no ...我沒有設置任何視圖在列表視圖中不可見,事實上,我在另一種情況下以編程方式隱形,因爲我使用相同的活動和xml來完成兩個不同的任務。 – 2013-03-11 07:17:07

+0

這個解決方案可能對你有用。 http://stackoverflow.com/questions/15491949/checkbox-and-edittext-not-work-in-listview/15498744#15498744 – 2013-03-19 12:04:48

回答

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)) 
+0

這並沒有解決我的問題,事實上,現在我沒有得到正確的提示在EditTexts因爲它應該顯示。此外前面提到的問題仍然存在。你能以另一種方式讓我看看嗎? – 2013-03-11 07:20:39

+0

表示這個捷徑不起作用。所以你需要自己維護edittext條目。像這樣http://vikaskanani.wordpress.com/2011/07/27/android-focusable-edittext-inside-listview/ – 2013-03-11 07:57:03

相關問題