从listview中的Textviews获取数据

时间:2017-04-20 08:22:33

标签: java android listview view

我试图在用户修改后从列表视图中获取Textviews中的数据。 那么用户点击" descrec" Textview将更改为"已完成"

listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        TextView text2 = (TextView) view.findViewById(R.id.descrec);
        if(text2.getText().toString().equals("Selected"))
        {
            text2.setText(Outstanding.outDesc[(int)id]);
        }
        else
        {
            text2.setText("Selected");
        }
        Log.i("you selected row number",String.valueOf(id));
    }
});

一旦用户完成选择行并点击&#34;提交&#34;按钮我应该打印所有行&#34;选择&#34; &#34; descrec&#34;中的字符串Textview但它什么都没打印。

public void submit (View view)
{
    for (int i = 0; i < listView.getCount(); i++) 
    {
        v = listView.getAdapter().getView(i, null, null);
        TV = (TextView) v.findViewById(R.id.descrec);
        if (TV.getText().toString().equals("Selected"))
        {
            //just to make sure descrec text has beeen changed somewhere
            Log.i("descrec", (String) TV.getText());
        }
        //to check the Textview anyway
        Log.i("descrec value", (String) TV.getText());
    }
}

打印整行的结果&#39; &#34; descrec&#34;在用户将其更改为&#34;选择&#34;。

之前,给出Textview的原始值

以下是选择行并点击提交按钮后的结果。

enter image description here

这是完整的代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_outstanding);
        new MyAsync().execute();
    }




    class MyAsync extends AsyncTask<Void, Void, Void> {
        protected void onPreExecute() {
            Log.i("preexcute","first");
            listView = (ListView)findViewById(R.id.listviewout);
            Contactadapter2 = new contactadapter2(Outstanding.this , R.layout.rowrecord_layout);
            listView.setAdapter(Contactadapter2);




            BackgroundWorker Backgroundworker = new BackgroundWorker(Outstanding.this);
            Backgroundworker.execute(type,Homescreen.userID);
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            return null;
        }

        @Override
        protected void onPostExecute(final Void unused) {
            Log.i("postexcute","second");

            for (int i = 0; i < outrecieptID.length; i++) {
                Log.i("test", valueOf(i) + "out of" + valueOf(outrecieptID.length));

                Contacts2 contacts2 = new Contacts2(Outstanding.outrecieptID[i], Outstanding.outAmount[i], Outstanding.outDesc[i]);
                Contactadapter2.add(contacts2);
            }

            for(int i=0 ; i < listView.getCount() ; i++) {
                v[i] = listView.getAdapter().getView(i, null, null);
            }

            listView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                    TextView text2 = (TextView) view.findViewById(R.id.descrec);
                    TV = (TextView) v[(int)id].findViewById(R.id.descrec);
                    if(text2.getText().toString().equals("Selected"))
                    {
                        text2.setText(Outstanding.outDesc[(int)id]);
                    }else
                    {
                        text2.setText("Selected");
                        TV.setText("Selected");
                    }
                    Log.i("you selected row number",String.valueOf(id));
                }
            });


        }

    }




    public void submit (View view)
    {

        for (int i = 0; i < listView.getCount(); i++) {
           // v = listView.getAdapter().getView(i, null, null);
            TV = (TextView) v[i].findViewById(R.id.descrec);
            if (TV.getText().toString().equals("Selected"))
            {
            //just to make sure descrec text has beeen changed somewhere
                Log.i("descrec", (String) TV.getText());
            }
            //to see the Textview anyway
            Log.i("descrec value", (String) TV.getText());

        }

    }

以下是contactadapter代码:

public class contactadapter2 extends ArrayAdapter {
    List list= new ArrayList();
    public contactadapter2(Context context, int resource) {
        super(context, resource);
    }

    public void add(Contacts2 object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row;
        row=convertView;
        ContentHolder contentHolder;
        if(row==null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.rowrecord_layout,parent,false);
            contentHolder = new ContentHolder();
            contentHolder.recieptidout=(TextView) row.findViewById(R.id.recieptidrec);
            contentHolder.amountout= (TextView) row.findViewById(R.id.amountrec);
            contentHolder.descout= (TextView) row.findViewById(R.id.descrec);




            row.setTag(contentHolder);
        }
        else
        {
            contentHolder = (ContentHolder)row.getTag();
        }

        Contacts2  contacts2 = (Contacts2) this.getItem(position);
        contentHolder.recieptidout.setText(contacts2.getrecieptidrec());
        contentHolder.amountout.setText(contacts2.getamountrec());
        contentHolder.descout.setText(contacts2.getdescrec());




        return row;
    }

    static class ContentHolder
    {


        TextView recieptidout,amountout,descout;

    }

1 个答案:

答案 0 :(得分:0)

您应该使用“已选择”文本更新模型。否则它不会工作。因为当你调用v = listView.getAdapter().getView(i, null, null)它会绑定你的arraylist中的数据。因此,它将更改您在itemclick侦听器中设置的文本。

尝试改变这样的事情,

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView text2 = (TextView) view.findViewById(R.id.descrec);
                if(text2.getText().toString().equals("Selected"))
                {
                    text2.setText(Outstanding.outDesc[(int)id]);
                }
                else
                {
                    text2.setText("Selected");
                }
                Contacts2 contacts2 = parent.getAdapter().getItem(position);
                contacts2.setdescrec(text2.getText().toString());
                Log.i("you selected row number",String.valueOf(id));
            }
        });