how to get the content of listview with a button outside the list

老子叫甜甜 提交于 2019-12-11 09:59:54

问题


Whatever I searched so far is about getting the list view data by placing the button in each row, But what I want to achieve is to pick the listview content by placing the button outside the list.

I have created an editable listview where user will enter the value and this value will be multiplied by one of the columns already present in the list and the result will be set in another textview.

Now on clicking the button (which is given below the list)I want to perform the following two things.

  1. I want to get only those rows where user has entered the values in the textboxes. and

  2. the values of the editTexts (name and address) which are given above the listview. and saves them to sqlite.

I don't know how to do this, any help would be greatly appreciated. Sorry if I'm not clear. Below is the code of my listview adapter

@Override
public View getView( final int position, View convertView, ViewGroup parent)   {        
  final   ViewHolder holder;
  if (convertView == null) {
        convertView = inflater.inflate(R.layout.productslistviewadapter, parent, false);

        holder = new ViewHolder();
        holder.tvdrCode = (TextView) convertView.findViewById(R.id.tvname);
        holder.tvDrName = (TextView) convertView.findViewById(R.id.tvprodpack);
        holder.tvterrcode= (TextView) convertView.findViewById(R.id.textView3);
        holder.caption = (EditText)convertView.findViewById(R.id.editText1);
        holder.tvValue = (TextView) convertView.findViewById(R.id.value);
        holder.tvValue.setVisibility(View.GONE);
        convertView.setTag(holder);

    } 
 else {
        holder = (ViewHolder) convertView.getTag();
    }

    Products p = prodList.get(position);
    holder.tvdrCode.setText(p.getDocCode());
    holder.tvDrName.setText(p.getDocName());
    holder.tvterrcode.setText(p.getAdr());

    //for editText
    holder.caption.setTag(position);
    holder.caption.setText(p.getCaption());
    int tag_position=(Integer) holder.caption.getTag();
    holder.caption.setId(tag_position); 

    holder.caption.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
           if (!hasFocus) {
               /*
             * When focus is lost save the entered value for
             * later use
             */
               int position2; 
               position2 = holder.caption.getId();
               position2 = holder.tvValue.getId();
               final EditText Caption = (EditText) holder.caption;
               final TextView TvValue = (TextView) holder.tvValue;

               if(Caption.getText().toString().length()>0)
                 {
                   prodList.get(position2).setCaption(Caption.getText().toString());

                   String prodpack = prodList.get(position).getDocName().toString();
                   String prodname = prodList.get(position).getDocCode().toString();
                   String quantity = prodList.get(position2).getCaption()

                   int  value = Integer.parseInt(prodpack) * Integer.parseInt(quantity);   
                   holder.tvValue.setText(Integer.toString(value)); 
                   holder.tvValue.setVisibility(View.VISIBLE);

               }  
               else{
                    Log.e("enter some value", "yes");
               }

             }
        }
    });     

回答1:


Try the below code snippet on the button click

for (int i = 0; i < listView.getAdapter().getCount(); i++) {
            Products pold = (Products) listView.getAdapter().getItem(i);
            Products pnew = prodlist.get(i);         
            if(pold.getCaption() !=null)
            {
             if(pold.getCaption() != pnew.getCaption().toString())
             {
              //Call sqlite save here
              Log.d("yes", "yes");
             }
             else {
              Log.d("no", "no");
             }   
            }

Hope this helps




回答2:


For listview ...as it has edit text..set on text change listener on edit text...and keep a hasmap to store the pos of these records...along with value entered , so as and when user starts typing...put check if editext is empty or unchanged ...then don't add it to list.

As when user press save ...get name and address value...also u get values from hashmap. I hope clear enough to explain you.



来源:https://stackoverflow.com/questions/31414398/how-to-get-the-content-of-listview-with-a-button-outside-the-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!