2

我的列表视图中有一个编辑文本和一个保存按钮。当用户单击保存按钮时,我必须清除文本。我试过像

txtDescription.setText("");

但没有奏效。任何人都知道为什么它不起作用?适配器类已附加

请帮助我,在此先感谢!

private class ListAdapters extends ArrayAdapter<ApplicationBean> {
    private ArrayList<ApplicationBean> items;
    private int position;

    public ListAdapters(Context context, int textViewResourceId,
            ArrayList<ApplicationBean> mTitleList) {
        super(context, textViewResourceId, mTitleList);
        this.items = mTitleList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        this.position = position;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.applicationlistitem, null);
        }

        final ApplicationBean o = (ApplicationBean) items.get(position);

        if (o != null) {

            txtDescription = (EditText) v
                    .findViewById(R.id.description_text);



            submitButton = (Button) v.findViewById(R.id.submit_btn);
            submitButton.setTag(position);
            submitButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {

                        PostRequest p = new PostRequest(Integer.parseInt(v
                                .getTag().toString()));
                        p.execute();
                }

            });
        }
        return v;
    }

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

    @Override
    public int getViewTypeCount() {
        // TODO Auto-generated method stub
        return items.size();
    }

}



private class PostRequest extends AsyncTask<Void, Void, String> {
    ProgressDialog dlgprogress;
    int position;

    public PostRequest(int selectedIndex) {
        position = selectedIndex;
    }

    @Override
    protected void onPostExecute(String result) {
        dlgprogress.dismiss();
        final Dialog dlg = new AlertDialog.Builder(mContext)
                .setTitle("Message")
                .setMessage(result)
                .setPositiveButton("Ok",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                dlgprogress.dismiss();
                                dlgprogress.cancel();
                                txtDescription.setText("");
                            }
                        }).create();
        dlg.show();
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        dlgprogress = ProgressDialog.show(mContext, "", "Please wait");
        // dlgprogress.show(mContext, "", "Please wait");
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(Void... arg0) {
        //do something.............
        //..........................
        dlgprogress.dismiss();
        return rsponse;
    }

}

应用程序列表项.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:background="#FFFFFF">



    <LinearLayout
        android:id="@+id/lineatlayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="DESCRIPTION : "
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000" />

        <EditText
            android:id="@+id/description_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:clickable="true"
            android:ems="10"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:inputType="textMultiLine" 
            android:textColor="#000000">
        </EditText>
    </LinearLayout>

    <Button
        android:id="@+id/submit_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SUBMIT" />

   </LinearLayout>
4

3 回答 3

2

维护列表视图的自定义适配器类

 public View getView(final int position, View convertView, ViewGroup parent)
    {
        View row=null;
        Context context = getApplicationContext();
        LayoutInflater inflater=cntx.getLayoutInflater();
        row=inflater.inflate(R.layout.search_list_item, null);

        EditText txtDescription=    (EditText)row.findViewById(R.id.txtDescription);
        Button Savebtn  =   (Button)row.findViewById(R.id.SaveBtn);

        Savebtn.setOnClickListener(new OnClickListener(){

            public void onClick(View v) 
            {
                txtDescription.setText("");
                // And your additional coding
            }
        });
   }

如果您不熟悉列表视图的自定义适配器,请在此处查看

于 2012-10-31T05:32:23.320 回答
1

您还可以将任何设置object为 TAG 到您的按钮视图。

所以在你的getView()方法中这样做,我想它会起作用。

             //EDIT
         txtDescription.setTag(position); // set position to edittext

         submitButton = (Button) v.findViewById(R.id.submit_btn);
         submitButton.setTag(txtDescription); // set the current edittext object 

         submitButton.setOnClickListener(new OnClickListener() {        
                @Override
                public void onClick(View v) {
                       EditText tv = (EditText)v.getTag(); // get edittext object
                       tv.setText("");
                             //Edit
                       PostRequest p = new PostRequest(Integer.parseInt(tv
                                .getTag().toString())); // get position from edittext
                    }
                });

编辑发送完整视图

我没有对此进行测试,但我想如果它不起作用,那么它也会在您发送editext时将复选框发送给构造函数。

class PostTask extends AsyncTask<Void,Void,Void>
{
    CheckBox cb;
    EditText et;
    int pos;
    PostTask(int pos,View view)
    {
        cb = view.findViewById(R.id.cbox1);
        et = view.findViewById(R.id.et1);
        pos = Integer.parseInt(et.getTag().toString());
    }
}

//现在在getView中改变

submitButton.setOnClickListener(new OnClickListener() {     
            @Override
            public void onClick(View v) {
                   PostRequest p = new PostRequest((View)v.getParent()); // get position from edittext
            }
 });
于 2012-10-31T05:48:52.203 回答
0

您在每个列表项上都有 editText 和提交按钮?

好吧,listView 具有转换视图的系统...它不会像数组大小那样创建视图,但只会创建足够的内容以在屏幕上显示用户,然后它会在您滚动时开始转换它们但是您想要的...所以如果你清除一个editText,你可以看到它充满了其他的值......所以我建议你有另一个数组来提供你的listView,就像你保存你的editText值并在提交按钮上单击你删除它们并在适配器上您应该使用此数组设置 editText 的文本,因此如果该值已提交,它将为空。

这是您的问题的解决方案:

尝试使用与您的列表相同大小的数组:

ArrayList<String> texts = new ArrayList<String>();
for(int i = 0; i < items.size(); i++)
texts.add(“”);

所以你所有的editTexts一开始都是空的......然后你在你的自定义适配器的getView方法上设置它们:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    this.position = position;
    if (v == null) {
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = inflater.inflate(R.layout.applicationlistitem, null);
    }

    final ApplicationBean o = (ApplicationBean) items.get(position);

    if (o != null) {

        txtDescription = (EditText) v
                .findViewById(R.id.description_text);

    // you should set your every views’ editText here
    txtDescription.setText(texts.get(position));

    // here you save texts also they changes
    txtDescription.addTextChangedListener({
         @Override
         public void afterTextChanged(Editable s) {
            texts.remove(position);
            texts.add(position, s.toString());
         }

         @Override
         public void beforeTextChanged(CharSequence s, int start, int count,
            int after) { }

         @Override
         public void onTextChanged(CharSequence s, int start, int before,
            int count) { }
     });

        submitButton = (Button) v.findViewById(R.id.submit_btn);
        submitButton.setTag(position);
        submitButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
        texts.remove(position);
        texts.add(position, “”);

                    PostRequest p = new PostRequest(Integer.parseInt(v
                            .getTag().toString()));
                    p.execute();
            }

        });
    }
    return v;
}
于 2012-10-31T05:57:06.790 回答