如何在按钮单击时从列表视图中编辑文本值

时间:2016-03-11 06:36:58

标签: android listview android-edittext

我在列表视图中有两个编辑文本和文本视图。文本视图值是从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 提前谢谢..

3 个答案:

答案 0 :(得分:1)

活动:

body {
margin: 0px;
}

header {
color: white;
text-align: center;
width: 100%;
background-color: #2995f3;
}

.center {
 width: calc(100% - 400px);
 background: green;
 margin-left: 200px;
 float: left;
 }
 .left {
 float: left;
 background: red;
 position: absolute;
 width: 200px;
 }
.left2 {
float: left;
position: absolute;
width: 200px;
overflow: hidden; // to hide the overflow of long text
}
p{background-color: red; white-space: nowrap;}
.left2:hover{width: calc(100% - 200px);}
.right {
float: left;
background: red;
width: 200px;
}

}

<强> activity_main:

&#13;
&#13;
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){

        }
    }*/

}
&#13;
&#13;
&#13;

<强> content_main.xml:

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

<强> item_list.xml:

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

<强>的AndroidManifest.xml

&#13;
&#13;
<?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>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

问题在于EditText难以获得对ListView的引用。我建议采用以下方法。

  • 有2个类String Array
  • 的类变量
  • String[] csArray = new String[];
  • String[] pcsArray = new String[];

现在,在您的列表的适配器getView方法上,添加TextWatcher并在用户更改值时填充csArraypcsArray。 E.g。

@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对象中获取所需的值。

答案 2 :(得分:0)

尝试替换此行

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

。通过

v = list.getChildAt(i);