2017-05-13 37 views
0

所以我的主要問題是讓他們參考...使用自定義適配器 I'm,因爲我有這樣的東西ImageViewTextView自定義列表項,CheckBoxes如何從所有行所有CheckBox在ListView

通過設置:

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

那麼,有沒有辦法讓這個複選框參考? 當我通過xml文件將「行」中的複選框的標識設置爲「複選框」時,每個listitem/row中的每個複選框都有此標識嗎?

這是如何完成的? 因爲我認爲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); 



} 

    } 

當我用這個......應該包含CheckBoxes結果Array爲空:/

那麼如何做到這一點嗎? 有什麼不對? 我可以用findViewById來做到這一點嗎? 如何以正確的方式做到這一點?

--------- -------------編輯

全部的源代碼:)

活動:

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; 
    } 
    } 
    } 

適配器:

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; 
} 

} 

LAYOUT:

<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應該切換複選框,但什麼也沒有發生...... 什麼錯了?

+0

複選框的CBox =(複選框)vg.findViewById(R.id.the_ID_Asigned_In_Xml); –

+0

那麼每個複選框都有相同的ID嗎? – user6366761

+0

是的,但每個CheckBox是一個不同的對象實例 –

回答

0

內,您的適配器類,創建一個新的布爾成員變量,並切換它的方法:

@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); 
    } 
} 

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

然後,使用新的變量切換知名度然後,在你的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