2016-03-11 59 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对象要求的值。

相关问题