Listview只返回可见的控件ID?和null为不可见

时间:2013-11-15 11:46:34

标签: android listview android-listview

我正在尝试获取所有复选框的ID,但它返回我只有可见复选框的ID。我必须在滚动后选择其他ID

  for(int i=0;i<workData.size();i++)
        {
            System.out.println("Total Size"+lstUsers.getCount());
            CheckBox chk=(CheckBox)lstUsers.findViewById(533+i);
            System.out.println("Chck "+chk);
             // It is returing id for visible and null for others
        } 

Adapater代码 -

package Adapters;




import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.mymapapp.JobDetails;
import com.mymapapp.R;

import Properties.WorkProperties;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class HistoryAdapter extends BaseAdapter {


    private List<WorkProperties> userData;
    private Activity activity;
    private Context context;
     ArrayList<WorkProperties> arraylist;
    //Layouts

    RelativeLayout outerLayout;
    RelativeLayout.LayoutParams params;
    TextView wrkUserName;
    TextView wrkDiscription;
    Button btnShowOnMap;
    ListView lstView;
    private int height;
    private int width;
    private Context pref;
    private CheckBox chekBox;

    public HistoryAdapter(List<WorkProperties> contacts,Activity activity,Context context)
    {
        userData=contacts;
        arraylist = new ArrayList<WorkProperties>();
        arraylist.addAll(userData);
        this.activity=activity;
        this.context=context;

    }



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

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

    @Override
    public long getItemId(int position) {
        return position;
    }

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



        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.topMargin=2;
        params.leftMargin=10;
        wrkUserName=new TextView(context);
        wrkUserName.setId(5001+position);
        wrkUserName.setTextSize(22);
        wrkUserName.setTypeface(null, Typeface.BOLD);
        wrkUserName.setTextColor(Color.BLACK);
        wrkUserName.setHorizontallyScrolling(true);
        wrkUserName.setLayoutParams(params);
        wrkUserName.setText(userData.get(position).getWrkUsrName());



        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        params.addRule(RelativeLayout.CENTER_VERTICAL);
        chekBox=new CheckBox(context);
        chekBox.setId(533+ position);
        chekBox.setLayoutParams(params);

        chekBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(activity.getApplicationContext(),"Check Bu Click",Toast.LENGTH_SHORT).show();

            }
        });





        params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.FILL_PARENT);
        params.addRule(RelativeLayout.BELOW,wrkUserName.getId());
        params.topMargin=2;
        params.bottomMargin=5;
        params.leftMargin=10;
        wrkDiscription=new TextView(context);
        wrkDiscription.setId(6001+position);
        wrkDiscription.setText(userData.get(position).wrkDiscription);
        wrkDiscription.setTextSize(19);
        wrkDiscription.setTextColor(Color.BLACK);
        wrkDiscription.setLayoutParams(params);
        wrkDiscription.setMaxLines(2);
















        outerLayout=new RelativeLayout(activity.getApplicationContext());
        outerLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT));
        outerLayout.setPadding(0, 10, 0, 0);
        outerLayout.setBackgroundColor(Color.WHITE);


        outerLayout.addView(wrkUserName);
        outerLayout.addView(wrkDiscription);
        outerLayout.addView(chekBox);


        outerLayout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            }
        });



        return outerLayout;

    }









}

1 个答案:

答案 0 :(得分:0)

ListView重用视图,因此不存在不可见的视图。在这种情况下,您应该在数据中设置用作模型的标记。在适配器中试试这个:

    chekBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Toast.makeText(activity.getApplicationContext(), "Check Bu Click", Toast.LENGTH_SHORT).show();
            userData.get(position).setChecked(isChecked);
        }
    });

您应该在userData对象中定义方法setChecked(boolean bool)。我强烈建议使用.xml来创建视图。