簡體   English   中英

從listView類的適配器獲取arrayList值

[英]Get arrayList value from adapter in listView class

如果單擊復選框,我已經在arraylist中存儲了一些值,但是我很難從listView類中獲取它。 我怎么才能得到它。 另外,如果我將clickListener保留在包含復選框和textView的視圖中,則復選框的單擊不起作用,但是如果我在textview中單擊,則它起作用。 如何解決。 下面是我的代碼。 提前致謝

singlerow_compare.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:id="@+id/singleRow" android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewCompare" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageViewCompare"/>

</RelativeLayout>

listview xml:

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listViewCompare" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Compare"
    android:id="@+id/compare"
    android:layout_alignParentBottom="true"/>

ListView.java

listView = (HorizontalListView) findViewById(R.id.listViewCompare);
compareBtn = (Button) findViewById(R.id.compare);
listView.setAdapter(new CustomAdapter(this, nameList, imageList)); 
compareBtn.setOnClickListener(new View.OnClickListener()
    {   @Override
            public void onClick(View view) {
            //I need to have addCheckBoxValue arraylist from adapter here
            }
    }

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
ArrayList result;
Context context;
Drawable [] imageId;
protected ArrayList<String> addCheckBoxValue;

private static LayoutInflater inflater=null;
public CustomAdapter(CompareListView mainActivity, ArrayList nameList, Drawable[] imageList) {
    result=nameList;
    context=mainActivity;
    imageId=imageList;
    inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public class Holder
{
    TextView tv;
    ImageView img;
    CheckBox checkBox;
}

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    addCheckBoxValue = new ArrayList();
    final Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.singlerow_compare, null);
    holder.tv=(TextView) rowView.findViewById(R.id.textViewCompare);
    holder.checkBox = (CheckBox) rowView.findViewById(R.id.imageViewCompare);
    holder.tv.setText(result.get(i).toString());
    holder.checkBox.setButtonDrawable(imageId[i]);
    final String selectedCbValue = holder.tv.getText().toString();
    holder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean checked = ((CheckBox) view).isChecked();
            if(checked){
                if (!addCheckBoxValue.contains(selectedCbValue))
                    addCheckBoxValue.add(selectedCbValue);
            }else{
                if (addCheckBoxValue.contains(selectedCbValue))
                    addCheckBoxValue.remove(selectedCbValue);
            }
        }
    });
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
     //if i click on textView, it works but when i click on checkbox here, it doesnt work why?? So i have clicklistener in checkbox abov
        }
    });
    return rowView;
}

在您的CustomAdapter類中創建CustomAdapter方法:

public  ArrayList<String> getCheckBoxValue(){
    return addCheckBoxValue;
}

並在您的ListActivity中。 像這樣更改您的活動代碼:

private CustomAdapter listAdapter; // declare this before on create

現在,您要在創建適配器的位置編寫以下代碼:

listAdapter = new CustomAdapter(this, nameList, imageList)
// set Adapter like this:
listView.setAdapter(listAdapter);

您的點擊代碼按鈕應如下所示:

compareBtn.setOnClickListener(new View.OnClickListener()
    {   @Override
            public void onClick(View view) {
            //I need to have addCheckBoxValue arraylist from adapter here
             listAdapter.getCheckBoxValue(); // do whatever you want to do here

            }
    }

快樂編碼!

我建議您為復選框創建布爾數組並像這樣維護它,這對我有用

public class CustomAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private final Context context;
    private List<ModelPooja> listData;

    public CustomAdapter(Context mainActivity, List<ModelPooja> listData) {
        context = mainActivity;
        this.listData = listData;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

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

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.list_item_poojaselection, null);
            holder.tv = (TextView) convertView.findViewById(R.id.list_item_poojaname);
            holder.checks = (CheckBox) convertView.findViewById(R.id.list_item_poojacheck);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.checks.setOnCheckedChangeListener(null);
        holder.checks.setFocusable(false);

        if (listData.get(position).isselected) {
            holder.checks.setChecked(true);
        } else {
            holder.checks.setChecked(false);
        }

        holder.checks.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton cb, boolean b) {

                if (checkMaxLimit()) {

                    if (listData.get(position).isselected && b) {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;

                    } else {
                        holder.checks.setChecked(false);
                        listData.get(position).isselected = false;
                        Toast.makeText(context, "Max limit reached", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (b) {
                        listData.get(position).isselected = true;
                    } else {
                        listData.get(position).isselected = false;
                    }
                }
            }
        });

        holder.tv.setText(listData.get(position).getPOOJA_LISTING_NAME());
        return convertView;
    }

    public boolean checkMaxLimit() {
        int countermax = 0;
        for(ModelPooja item : listData){
            if(item.isselected){
                countermax++;
            }
        }
        return countermax >= 5;
    }

    public class ViewHolder {
        TextView tv;
        public CheckBox checks;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM