2013-02-05 165 views
1

我使用自定義列表視圖,並且我在listview_row.xml中使用EditText。我想對EditText值進行一些操作,但是我如何找到聚焦的edittext,並且想要顯示那個焦點所在的行。我是如何找到並EDITTEXT,做執行我的文字更改操作,請幫幫我,我在這很困惑,在此先感謝....查找edittext自定義列表視圖

QuckAcess.java

public class QuickAcess extends Activity { 

private ListViewAdapter mAdapter; 
    private ArrayList<String> Header; 
    private ArrayList<Integer> close; 
    private ArrayList<String> From; 
    private ArrayList<String> To; 
    private ArrayList<String> Value; 
    private ListView listView; 
    EditText editText; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.quickaccess); 


     fillSave(); 
     mAdapter = new ListViewAdapter(this, Header, From, To, Value, close); 
     listView = (ListView) findViewById(R.id.SavedList); 
     listView.setAdapter(mAdapter); 

     listView.setOnItemClickListener(new OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
        long arg3) { 



      } 
     });  
} 
public void fillSave() { 
    // TODO Auto-generated method stub 


    close = new ArrayList<Integer>(); 
    close.add(R.drawable.close); 
    close.add(R.drawable.close); 
    close.add(R.drawable.close); 

    Header = new ArrayList<String>(); 
    Header.add("Temperature" + " :"); 
    Header.add("Length"); 
    Header.add("Mass"); 

    From = new ArrayList<String>(); 
    From.add("a" + " "); 
    From.add("b" + " "); 
    From.add("c" + " "); 


    To = new ArrayList<String>(); 
    To.add("x"); 
    To.add("y"); 
    To.add("z"); 

    Value = new ArrayList<String>(); 
    Value.add("1"); 
    Value.add("2"); 
    Value.add("3"); 

} 

listview_row.xml

<?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="wrap_content" 
android:orientation="vertical" > 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

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

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

    <ImageView 
     android:id="@+id/imgExchange" 
     android:layout_width="44dp" 
     android:layout_height="36dp" 
     android:src="@drawable/swap" /> 

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

    <ImageButton 
     android:id="@+id/ibclose" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:src="@drawable/close" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <EditText 
     android:id="@+id/etValue" 
     android:layout_width="137dp" 
     android:layout_height="wrap_content" 
     android:inputType="number" > 
    </EditText> 

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

ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter { 
private ArrayList<String> Header; 
private ArrayList<Integer> close; 
private ArrayList<String> From; 
private ArrayList<String> To; 
private ArrayList<String> Value; 
private Activity activity; 

public ListViewAdapter(Activity activity,ArrayList<String> Header, ArrayList<String> From,ArrayList<String> To,ArrayList<String> Value,ArrayList<Integer> close) { 
super(); 
     this.Header = Header; 
     this.From = From; 
     this.close = close; 
     this.activity = activity; 
     this.To = To; 
     this.Value = Value; 


} 
@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return 3; 
} 

@Override 
public Object getItem(int paramInt) { 
    // TODO Auto-generated method stub 
    return close.size(); 
} 

@Override 
public long getItemId(int paramInt) { 
    // TODO Auto-generated method stub 
    return 0; 
} 
public static class ViewHolder 
    { 
     public ImageView imgViewFlag; 
     public TextView header,from,to; 
     public EditText value; 
    } 

@Override 
public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) { 
    // TODO Auto-generated method stub 

    ViewHolder view; 
    LayoutInflater inflator = activity.getLayoutInflater(); 
    if(paramView==null) 
     { 
      view = new ViewHolder(); 
      paramView = inflator.inflate(R.layout.listview_row, null); 

      view.header = (TextView) paramView.findViewById(R.id.tvHeader); 
      view.from = (TextView) paramView.findViewById(R.id.tvfrom); 
      view.to = (TextView) paramView.findViewById(R.id.tvto); 
      view.value = (EditText) paramView.findViewById(R.id.etValue); 
      view.imgViewFlag = (ImageView) paramView.findViewById(R.id.ibclose); 
      paramView.setTag(view); 
     } 
    else 
     { 
      view = (ViewHolder) paramView.getTag(); 
     } 
    view.header.setText(Header.get(paramInt)); 
    view.from.setText(From.get(paramInt)); 
    view.to.setText(To.get(paramInt)); 
    view.value.setText(Value.get(paramInt)); 
    view.imgViewFlag.setImageResource(close.get(paramInt)); 

    return paramView; 
} 

} 
+0

我teried但沒有得到結果 – Rohit

+0

你在哪裏使用過TextWatcher? –

+0

我使用TextWatcher內 listView.setOnItemClickListener(新OnItemClickListener(){ @Override 公共 無效onItemClick(適配器視圖爲arg0,ARG1查看,INT位置, 長ARG3){ //在此間 } }); – Rohit

回答

2

您可以從這裏獲取索引或行。

@Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
       long arg3) { 

       rowIndex = position; 

     } 

,你也可以setTag每個在這裏你的EditText場

public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) { 
// TODO Auto-generated method stub 

ViewHolder view; 
LayoutInflater inflator = activity.getLayoutInflater(); 
if(paramView==null) 
    { 
     view = new ViewHolder(); 
     paramView = inflator.inflate(R.layout.listview_row, null); 

     view.header = (TextView) paramView.findViewById(R.id.tvHeader); 
     view.from = (TextView) paramView.findViewById(R.id.tvfrom); 
     view.to = (TextView) paramView.findViewById(R.id.tvto); 
     view.value = (EditText) paramView.findViewById(R.id.etValue); 

     view.value.setTag("myThisEditText"); 

     view.imgViewFlag = (ImageView) paramView.findViewById(R.id.ibclose); 
     paramView.setTag(view); 
    } 
else 
    { 
     view = (ViewHolder) paramView.getTag(); 
    } 
view.header.setText(Header.get(paramInt)); 
view.from.setText(From.get(paramInt)); 
view.to.setText(To.get(paramInt)); 
view.value.setText(Value.get(paramInt)); 
view.imgViewFlag.setImageResource(close.get(paramInt)); 

return paramView; 

}

,並可以在onCClickListener

得到這個標籤就像

arg1.getTag(); 

您可以爲每個索引進行任何自定義。 希望它能幫助你。

+0

sory兄弟,新我已經改變了我的應用程序的全部概念,如果你說我會接受你的答案,但它確實幫助我 – Rohit

0

的「AddTextChangeListener」你可以搜索列表中的類似項目,並加載搜索文本的新arraylist創建爲ListView ..和一些數組列表...

edittext.addTextChangedListener(new TextWatcher(){ 
public void afterTextChanged(Editable s) { 
    //search for the keyword and add the items to a new arraylist 
} 
+0

這不是一個答案 – Rohit