2016-03-11 61 views
0

我在列表視圖中有兩個編輯文本和文本視圖。文本視圖值是從Sq lite通過適配器設置的,我在列表視圖外有一個按鈕。我的問題是當我點擊按鈕時,每個編輯文本中的值將被存儲到一個數組中,之後我可以再次將它存儲到SQLite中。但是在運行代碼時我會得到空值。我嘗試了很多其他方法但失敗的方法。請幫助獲得答案。如何從按鈕點擊列表視圖中獲取編輯文本值

這裏是我的代碼

protected void InsertDb() { 
     // TODO Auto-generated method stub 

    View v; 
      HashMap<String, String> itemdata = new HashMap<String, String>(); 

      for (int i = 0; i < list.getChildCount(); i++) { 
      v = list.getAdapter().getView(i, null, null); 
      cases = (EditText) v.findViewById(R.id.cases); 
      pcs = (EditText)v.findViewById(R.id.pcs); 
      itemdata.put("cases", cases.getText().toString()); 
      itemdata.put("pieces", pcs.getText().toString()); 
       } 
      Log.v("working correctly", itemdata.toString()); 
} 

this is how my activity looks like 提前謝謝..

+0

這不是你如何從適配器讀取視圖。 'getView'就是你調用它的方式,只返回空'EditText'。你能詳細說明你想達到什麼嗎? – hoomi

+0

@ hoomi我只想從編輯文本中獲取值。這個編輯文本駐留在列表視圖中。由於列表視圖的大小很大,每次運行應用程序時都不需要在所有編輯文本中輸入值。有些可能保持爲空。我想要通過迭代列表視圖來輸入編輯文本中輸入的值。 – Bivin

回答

1

活動:

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 
private ListView mList; 
private Button mButton; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    mList = (ListView) findViewById(R.id.list); 
    mButton = (Button) findViewById(R.id.click_btn); 
    MyAdapter adapter = new MyAdapter(this); 
    mList.setAdapter(adapter); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
    mButton.setOnClickListener(this); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

@Override 
public void onClick(View v) { 
    if(mList != null){ 
     for(int i = 0; i< mList.getChildCount();i++){ 
      View vie = mList.getChildAt(i); 
      EditText ed1= (EditText) vie.findViewById(R.id.edone); 
      EditText ed2 = (EditText) vie.findViewById(R.id.edtwo); 
      Log.d("value",ed1.getText().toString()); 
      Log.d("value",ed2.getText().toString()); 
     } 
    } 
} 


class MyAdapter extends BaseAdapter{ 
    private LayoutInflater inflater; 
    public MyAdapter(Context context){ 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     return 5; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View row = convertView; 
     if(row == null){ 
      row = inflater.inflate(R.layout.item_list,parent,false); 
     } 
     return row; 
    } 


    /* class MyViewHolder{ 
     public MyViewHolder(View v){ 

     } 
    }*/ 

} 

}

activity_主:

<?xml version="1.0" encoding="utf-8"?> 
 
<android.support.design.widget.CoordinatorLayout 
 
    xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
 
    android:layout_height="match_parent" android:fitsSystemWindows="true" 
 
    tools:context=".MainActivity"> 
 

 
    <android.support.design.widget.AppBarLayout android:layout_height="wrap_content" 
 
     android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> 
 

 
     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" 
 
      android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" 
 
      android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> 
 

 
    </android.support.design.widget.AppBarLayout> 
 

 
    <include layout="@layout/content_main" /> 
 

 

 
    <android.support.design.widget.FloatingActionButton android:id="@+id/fab" 
 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
 
     android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" 
 
     android:src="@android:drawable/ic_dialog_email" /> 
 

 
</android.support.design.widget.CoordinatorLayout>

content_main.xml:

<?xml version="1.0" encoding="utf-8"?> 
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 
    xmlns:tools="http://schemas.android.com/tools" 
 
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" 
 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
 
    android:paddingRight="@dimen/activity_horizontal_margin" 
 
    android:paddingTop="@dimen/activity_vertical_margin" 
 
    android:paddingBottom="@dimen/activity_vertical_margin" 
 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
 
    tools:showIn="@layout/activity_main" tools:context=".MainActivity"> 
 

 
    <ListView 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:id="@+id/list"> 
 

 
    </ListView> 
 

 
    <Button 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:text="Click Me" 
 
     android:id="@+id/click_btn" 
 
     android:background="@color/colorPrimary" 
 
     android:textColor="#ffffff" 
 
     android:layout_below="@+id/list" 
 
     /> 
 

 
</RelativeLayout>

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

 

 
    <EditText 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:id="@+id/edone" 
 
     /> 
 

 

 

 

 
    <EditText 
 
     android:paddingTop="10dp" 
 
     android:layout_width="match_parent" 
 
     android:layout_height="wrap_content" 
 
     android:id="@+id/edtwo" 
 
     android:layout_below="@+id/edone" 
 
     /> 
 

 

 

 
</RelativeLayout>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
 
    package="com.example.naveenbm.edittexttest" > 
 

 
    <application 
 
     android:allowBackup="true" 
 
     android:icon="@mipmap/ic_launcher" 
 
     android:label="@string/app_name" 
 
     android:supportsRtl="true" 
 
     android:theme="@style/AppTheme" > 
 
     <activity 
 
      android:name=".MainActivity" 
 
      android:label="@string/app_name" 
 
      android:windowSoftInputMode="adjustPan" 
 
      android:theme="@style/AppTheme.NoActionBar" > 
 
      <intent-filter> 
 
       <action android:name="android.intent.action.MAIN" /> 
 

 
       <category android:name="android.intent.category.LAUNCHER" /> 
 
      </intent-filter> 
 
     </activity> 
 
    </application> 
 

 
</manifest>

+0

@ Naveen Shriyan當我嘗試這個我得到以下堆棧trace.still我無法得到值「03-11 12:23:31.419:V/working correctly(2917):{pieces =,cases =}」 – Bivin

+0

ru sure那些件和案件有價值? –

+0

是的,我通過編輯文本輸入值,當我點擊一個按鈕時,我想從編輯文本中取值並插入到sq lite中。但我無法從中獲得價值。請您檢查我的最新問題嗎?我在那裏提供我的活動圖像。 – Bivin

0

嘗試更換此線

v = list.getAdapter().getView(i, null, null); 

隨着

v = list.getChildAt(i); 
+0

它沒有檢索到地圖的值。當我試圖在日誌中打印時,我得到一個空哈希映射。 – Bivin

1

的問題是,得到的EditText基準距離ListView困難。我建議採取以下方法。

  • 有型2類變量String Array
  • String[] csArray = new String[];
  • String[] pcsArray = new String[];

現在在你的列表的適配器的getView方法,添加一個TextWatcher並填充csArraypcsArray每當用戶更改值。 例如

@Override 

public View getView(int position, View convertView, ViewGroup parent) { csEditText.addTextChangedListener(new TextWatcher() {

@Override 
    public void afterTextChanged(Editable s) { 
     csArray[position] = s.toString(); 
    } 
});` 

//重複相同的pcsEditText

現在,在按下按鈕的方法,簡單地讓你從csArraypcsArray對象要求的值。

相關問題