如何从ListView

时间:2017-05-13 16:07:04

标签: android listview checkbox

所以我的主要问题是获得对它们的引用...... 我使用的是自定义适配器,因为我的自定义列表项包含ImageViewTextViewCheckBoxes等内容。

通过:

MySimpleAdapter adapter = new MySimpleAdapter(this, R.layout.row, content);
listV.setAdapter(adapter);

那么有没有办法获得这个复选框的引用? 当我通过xml文件将“row”中的复选框的id设置为“checkbox”时,每个listitem /行中的每个复选框都有此ID吗?

这是怎么做到的? 因为id我认为不能引用多个视图? 或者是否自动添加了像checkbox1,checkbox2这样的后缀? 我可以这样做:

        // TEMP ARRAY FOR CHECKBOXES
        ArrayList<CheckBox> tempCBS = new ArrayList<CheckBox>();
        // NUMBER OF LISTITEMS
        // int childCount = listV.getChildCount();
        int childCount = listV.getCount();

            for (int i = 0; i > childCount; i++) {
            ViewGroup vg = (ViewGroup) listV.getItemAtPosition(i);
            // ONLY ITEMS WITH THIS TAG
            CheckBox cBOX = (CheckBox) vg.findViewWithTag("c_BOX");
            // IF NOT SUCCESSFUL
            if (cBOX.equals(null) || !(cBOX instanceof CheckBox)) {
                eLog.debLog(tag, "CHECKBOX IST NULL", context, 0);
                eLog.debLog(tag, "FINDEN DER CHECKBOXEN WAR ERFOLGLOS", context, 
    0);
                eLog.debLog(tag, cBOX.toString(), context, 0);
                return;
            }
            // IF SUCCESSFUL
            else {
                eLog.debLog(tag, "FOUND VALID CHECKBOX ... ADD", context, 0);
                tempCBS.add(cBOX);



}

    }

当我使用此...时,应包含Array的结果CheckBoxes为空:/

那怎么做呢? 怎么了? 我可以用findViewById做到这一点吗? 如何正确地做到这一点?

--------- EDIT -------------

完整来源:)

活动:

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

public boolean cbsVisible = false;
public ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lv = (ListView) this.findViewById(R.id.listV);
    // CREATE FAKE DATA
    ArrayList<String> fakeD = new ArrayList<String>();
    fakeD.add("sadfasdf");
    fakeD.add("dsgdfg");
    fakeD.add("jkl");
    fakeD.add("iul");
    fakeD.add("tzru");
    // SETZTE LISTENINHALT
    MySimpleAdapter adapter = new MySimpleAdapter(this, R.layout.row, fakeD);
    // ZEIGE LISTENINHALT
    lv.setAdapter(adapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

public void togChecks(View v) {
    // //WENN NICHT SICHTBAR
    // if(v.getVisibility() == View.GONE) {
    // v.setVisibility(View.VISIBLE);
    // }
    // //WENN SICHTBAR
    // else {
    // v.setVisibility(View.GONE);
    // }
    // BULLSHIT HAHA :D

    // CREATE/GET ARRAY OF CHECKBOXES
    // ArrayList<CheckBox> cbs = new ArrayList<CheckBox>();
    // cbs = lv.getr

    // TEMP ARRAY FOR CHECKBOXES
    ArrayList<CheckBox> tempCBS = new ArrayList<CheckBox>();
    // NUMBER OF LISTITEMS
    // int childCount = listV.getChildCount();
    int childCount = lv.getCount();

    for (int i = 0; i > childCount; i++) {
        ViewGroup vg = (ViewGroup) lv.getItemAtPosition(i);
        // ONLY ITEMS WITH THIS TAG
        CheckBox cBOX = (CheckBox) vg.findViewWithTag("c_BOX");
        // IF NOT SUCCESSFUL
        if (cBOX.equals(null) || !(cBOX instanceof CheckBox)) {
            Toast.makeText(this, "CHECKBOX IST NULL", Toast.LENGTH_SHORT);
            Toast.makeText(this, "FINDEN DER CHECKBOXEN WAR ERFOLGLOS", 
Toast.LENGTH_SHORT);
            Toast.makeText(this, cBOX.toString(), Toast.LENGTH_SHORT);
            return;
        }
        // IF SUCCESSFUL
        else {
            Toast.makeText(this, "FOUND VALID CHECKBOX ... ADD", 
Toast.LENGTH_SHORT);
            tempCBS.add(cBOX);
        }

    }

    // WENN CHECKBOXEN SICHTBAR
    if (cbsVisible) {
        for (CheckBox cb : tempCBS) {
            cb.setVisibility(View.GONE);
        }
        cbsVisible = false;
    }
    // WENN NICHT SICHTBAR
    else {
        for (CheckBox cb : tempCBS) {
            cb.setVisibility(View.VISIBLE);
        }
        cbsVisible = true;
    }
  }
  }

ADAPTER:

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class MySimpleAdapter extends ArrayAdapter {

ArrayList<String> titles;
Context context;

public MySimpleAdapter(Context context, int resource, ArrayList<String> fakeD) {
    // KONSTRUKTORAUFRUF DATEN MITGEBEN
    super(context, resource, fakeD);
    this.context = context;
    this.titles = fakeD;
    // TODO Auto-generated constructor stub
}

// MUSS ÜBERSCHRIEBEN WERDEN UM JEDES EINZELNE VIEW VOM LISTENELEMENT BEIM
// ERSTELLEN DER LISTE ANZUPASSEN
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    // return super.getView(position, convertView, parent);
    // SUPER METHODE ÜBERSCHREIBEN
    // UM INHALT ANZUPASSEN
    LayoutInflater inflater = (LayoutInflater) 
context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View list_item = inflater.inflate(R.layout.row, parent, false);
    // ANPASSEN
    TextView tempTXT = (TextView) list_item.findViewById(R.id.textView1);
    tempTXT.setText("" + titles.get(position));
    // MACHE CHECKBOXEN UNSICHTBAR
    // UM ALLE DANN WIEDER SICHTBAR ZU MACHEN ÜBER MAINACTIVITY
    // TODO AUF DIESES ELEMENT ÜBER MAINACTIVITY ZUGREIFEN
    CheckBox cb = (CheckBox) list_item.findViewById(R.id.checkBox1);
    cb.setVisibility(View.GONE);
    // ANGEPASSTEN INHALT WIEDERGEBEN
    return list_item;
}

}

布局:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.listviewcustomadapter.MainActivity" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#344"
    android:gravity="center"
    android:text="Custom ListView Sample"
    android:textSize="40sp" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:onClick="togChecks"
    android:text="toggle checkboxes" />

<ListView
    android:id="@+id/listV"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/button1"
    android:layout_below="@id/textView1" >
</ListView>

</RelativeLayout>

所以单击按钮1应该切换复选框但没有任何反应...... 怎么了?

1 个答案:

答案 0 :(得分:0)

在适配器类中,创建一个新的布尔成员变量和一个切换它的方法:

ArrayList<String> titles;
Context context;
Boolean showCheckBoxes; // Add this to top with your other member variables

public void setCheckBoxVisibility(Boolean visible){
    showCheckBoxes = visible;
    notifyDataSetChanged(); // Refresh the adapter
}

然后在适配器的getView内,使用新变量切换可见性:

@Override
public View getView(int position, View convertView, ViewGroup parent){
    CheckBox checkBox = (CheckBox) list_item.findViewById(R.id.YourCheckBoxId);
    if(showCheckBoxes){
        checkBox.setVisibility(View.VISIBLE);
    }else{
        checkBox.setVisibility(View.GONE);
    }
}

然后,在Activity内,使您的适配器成为成员变量:

public boolean cbsVisible = false;
public ListView lv;
public MySimpleAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState){

    // Make sure to use the member variable in your onCreate
    adapter = new MySimpleAdapter(this, R.layout.row, fakeD);
    lv.setAdapter(adapter);
}

然后,在setCheckBoxVisibility

中拨打togChecks
cbsVisible = !cbsVisible; // Toggle value
adapter.setCheckBoxVisibility(cbsVisible); // Set visibility