0

I have a ListViewwhich has a custom cell in it with 2 buttons, a label and an editText. I've got it so that when I click the button for the custom cell it does a log saying at what position the button has been pressed. However I can't work out how I make it so when I press the button, it changes the text in the textbox for that cell. I can't work out how to reference it at a position.

My XML is:

<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" >

     <ListView
         android:id="@+id/lvItems"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:drawSelectorOnTop="false" >

    </ListView>

</RelativeLayout>

My Code:

    class CustomAdapter extends BaseAdapter
{

@Override
public int getCount() {

    return mDescription.size();
}

@Override
public Object getItem(int arg0) {

    return null;
}

@Override
public long getItemId(int arg0) {

    return 0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {

    LayoutInflater inf=getLayoutInflater();
    View v=inf.inflate(R.layout.noncriticalasset, arg2,false);
    TextView tv=(TextView)v.findViewById(R.id.txtOption);

    final EditText et =(EditText)v.findViewById(R.id.tbAnswer);

    Button btPlus=(Button)v.findViewById(R.id.btnPlus);
    Button btMinus=(Button)v.findViewById(R.id.btnMinus);

    btMinus.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {

               int position = listView
                        .getPositionForView((View) v.getParent());
                Log.v("Position id", "" + position);

                et.setText("Test Text");

                notifyDataSetChanged();

            } catch (Exception e) {

                Log.e("error", "error" + e.getMessage());

            }


        }
    });



    tv.setText(mDescription.get(arg0).toString()); 



    return v;
}

I tried referencing the textbox with just an et.setText() but it didn't work. Is there a way to say atPosisiton.et.setText etc?

Thanks a lot.

4

4 に答える 4

1

First of all I would insist you to use recycling pattern of ListView, which will work faster as it reduces the creation of the ListView row item views everytime when ListView scroll and re-uses the already created/instantiated view. And for maintaining the value of EditText/CheckBox you have to use setTag() for maintaining its position when ListView scrolls. I had written a blog post for the same here.

于 2012-10-26T09:08:30.980 に答える
0

実際に取得した位置は正しいですが、notifyDataSetChanged() が呼び出されると、ListView は getView() メソッドを使用してすべての項目ビューを新しいビューに置き換えます。そのため、クリックした位置をどこかに保持し、getView() で EditText を設定する必要があります。

于 2012-10-27T15:02:38.517 に答える
0

こんにちは get ビューと ViewHolder ではなく Adapter で Button を宣言します。以下のように

ボタン btPlus、btMinus; ViewHolder ホルダー;

class ViewHolder {
  int fieldPossition;
 }

@Override public View getView(int position, View v, ViewGroup arg2) {

 holder = new ViewHolder();
LayoutInflater inf=getLayoutInflater();
  v=inf.inflate(R.layout.noncriticalasset, arg2,false);
    TextView tv=(TextView)v.findViewById(R.id.txtOption);

    EditText et =(EditText)v.findViewById(R.id.tbAnswer);

    btPlus=(Button)v.findViewById(R.id.btnPlus);
    btMinus=(Button)v.findViewById(R.id.btnMinus);


      fieldPossition = position;

    btMinus.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {

            ViewHolder deleteHolder = (ViewHolder) v.getTag();
                    //   
        int pos = deleteHolder.fieldpossition;
                Log.v("Position id", "" + position);

                et.setText("Test Text");

                notifyDataSetChanged();

            } catch (Exception e) {

                Log.e("error", "error" + e.getMessage());

            }


        }
    });



    tv.setText(mDescription.get(position).toString()); 

   btMinus.setTag(holder);


    return v;
}

それでおしまい 。

于 2012-10-26T11:01:39.243 に答える
-1

編集 テスト済み
OKLazyViewHolder.class

 public class LazyViewHolder {
        private EditText et;
        private TextView text;
        private Button btnSub;
        public LazyViewHolder() {
            // TODO Auto-generated constructor stub
        }

        LazyViewHolder(TextView textView,EditText ed,Button btn) {
            super();
            this.setEt(ed);
            this.setText(textView);
            this.setBtnSub(btn);
        }

        public EditText getEt() {
            return et;
        }

        public void setEt(EditText et) {
            this.et = et;
        }

        public TextView getText() {
            return text;
        }

        public void setText(TextView text) {
            this.text = text;
        }

        public Button getBtnSub() {
            return btnSub;
        }

        public void setBtnSub(Button btnSub) {
            this.btnSub = btnSub;
        }




    }

カスタムアダプタのgetView()

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        final TextView textView;
        final EditText et;
        final Button sub;

        if (v == null) {
            v = inflater.inflate(R.layout.row, null);
            textView = (TextView) v.findViewById(R.id.tvLabel);
            textView.setText("Hello"+position);
            et = (EditText) v.findViewById(R.id.etWhatToFill);
            sub = (Button) v.findViewById(R.id.btnSubmit);
            v.setTag(new LazyViewHolder(textView, et, sub));
            sub.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                textView.setText(et.getText().toString());  
                }
            });
        } else {
            LazyViewHolder viewHolder = (LazyViewHolder) v.getTag();
            sub = viewHolder.getBtnSub();
            et=viewHolder.getEt();
            textView = viewHolder.getText();
        }
        return v;
    }

ListViewとCustomListViewの詳細な説明は こちらをご覧ください

于 2012-10-26T09:17:41.050 に答える