簡體   English   中英

使用自定義列表視圖android時訪問行項目

[英]Accessing row items when using a custom list view android

我正在使用“自定義”列表視圖創建一個列表視圖,該列表視圖在每一行上都包含一個開關和一個文本視圖。 每行都是從一個名為row.xml的xml文件創建的,列表視圖位於主活動xml文件中。 這兩個xml文件可以在下面看到。

HomeActivity.xml

<FrameLayout 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:background="#0099cc" tools:context=".Activities.HomeActivity">

<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="#ff69539d">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="120dp"
        android:layout_below="@+id/fullscreen_content_controls"/>

</RelativeLayout>

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Switch
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:id="@+id/switch"
        android:checked="false"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="10dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Switch Name"
        android:id="@+id/textView"
        android:layout_alignBottom="@+id/switch"
        android:layout_toRightOf="@+id/switch"
        android:layout_marginLeft="20dp"
        android:layout_marginStart="20dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textColor="#000000" />
</RelativeLayout>

我使用自定義列表適配器用文本字段的文本填充listview。 可以在下面看到。

customlistAdapter

 public class customListAdapter extends BaseAdapter{

    Context context;
    String[] switchName;
    private static LayoutInflater inflater = null;

    public customListAdapter(Context context, String[] data) {
        // TODO Auto-generated constructor stub
        this.context = context;
        this.switchName = data;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return switchName.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return switchName[position];
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi = convertView;
        if (vi == null)
            vi = inflater.inflate(R.layout.row, null);
        TextView text = (TextView) vi.findViewById(R.id.textView);
        Switch mySwitch = (Switch) vi.findViewById(R.id.switch);
        mySwitch.setTag(position);
        text.setText(switchName[position]);
        return vi;
    }
}

列表適配器是根據主要活動創建的,如下所示。

主要活動

 public class MainActivity extends Activity {

    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        lv = (ListView) findViewById(R.id.listView);
        lv.setAdapter(new customListAdapter(this, new String[]{"Switch 1", "Switch 2 ", "Switch 3", "Switch 4", "Switch 5", "Switch 6", "Switch 7"}));

    }
}

我遇到的問題是能夠從主要活動中訪問在自定義列表適配器中創建的開關。 我需要能夠維護對主活動中所有開關的引用,以便可以更改狀態。 理想情況下,我將能夠在mainActivity中具有每個開關的列表或數組。 任何幫助,將不勝感激。

在您的適配器類中,添加以下內容:

private OnCheckChangedListener mOnCheckChangedListener;
public void setOnCheckChangedListener( OnCheckChangedListener listener ){
  mOnCheckChangedListener = listener;
}

然后在適配器的getView方法中,執行以下操作:

mySwitch.setOnCheckChangedListener( mOnCheckChangedListener );

然后在活動中,那么您想要什么:

private OnCheckChangedListener mOnCheckChangedListener = new OnCheckChangedListener(){
  @Override
  onCheckedChanged(CompoundButton buttonView, boolean isChecked){
    // keep a list of what was checked
  }
}

並將其設置在適配器上:

BaseAdapter adapter = new customListAdapter(this, new String[]{"Switch 1", "Switch 2 ", "Switch 3", "Switch 4", "Switch 5", "Switch 6", "Switch 7"});
adapter.setOnCheckChangedListener( mOnCheckChangedListener );
lv.setAdapter(adapter);

暫無
暫無

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

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