2

私はこの問題で多くの問題を抱えています。私は以下を含むリストビューを持っています:

ImageView / contactName / TextView / CheckBox

リストビューのcontactNameは、 SimpleCursorAdapterから電話の連絡先を読み取ることで入力されます。すべての要素はアプリの実行時に表示されますが、私が抱えている問題は、チェックボックスをリスト内の対応する項目に接続することです。

調査の結果、チェックボックスをリスト内の項目にリンクするにはgetView()を使用する必要があることがわかりましたが、実際には、正しく機能するようには見えません。さらに、私が試した例のどれも、getView()を適用する方法を実際に説明していませんでした。私が取り組んできた最も完全な例はここからです:

http://androidcocktail.blogspot.com/2012/04/adding-checkboxes-to-custom-listview-in.html

ひねりは、これが私のリストビューに私の連絡先を読み取って入力することです。

private void populateContactList() {
    // Build adapter with contact entries
    Cursor cursor = getContacts();
    String[] fields = new String[] {
            ContactsContract.Data.DISPLAY_NAME       
    };

    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
            fields, new int[] {R.id.contactEntryText});    
    lv.setAdapter(adapter);        
} // END POPULATECONTACTLIST


private Cursor getContacts()
{ 
    // Run query
    Uri uri = ContactsContract.Contacts.CONTENT_URI;
    String[] projection = new String[] {
            ContactsContract.Contacts._ID,
            ContactsContract.Contacts.DISPLAY_NAME
    };
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
            (chkboxAllVisible ? "0" : "1") + "'";
    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

    return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
} // END GETCONTACTS 

各チェックボックスをリストビューの対応する連絡先アイテムにリンクするにはどうすればよいですか?

4

3 に答える 3

2

さて、私はあなたがコードを理解しようとするためのテストプロジェクトを作成しました。あなたが抱えている問題があれば、私はあなたを助けようとします...

これが私の活動の具体的な機能です。

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<String> elements = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
    elements.add("elements " + i);
}

CheckBox master_cb = new CheckBox(getApplicationContext());
master_cb.setText("Check All");
//HERE IS THE LIST VIEW WHICH I HAVE CREATED IN MY XML FILE.
ListView lv = (ListView) findViewById(R.id.listView1);
//HERE I AM CREATING CUSTOM ADAPTER OBJECT.
my_custom_adapter adapter = new my_custom_adapter(this, android.R.layout.simple_list_item_1, elements);
lv.addHeaderView(master_cb);
lv.setAdapter(adapter);
master_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Intent my_intent = new Intent("master_check_change");
        my_intent.putExtra("check_value", isChecked);
        sendBroadcast(my_intent);
    }
});

}

これが私のカスタムアダプタです。

public class my_custom_adapter extends ArrayAdapter<String> {
    private Context context                     = null;
    ArrayList<String>  elements                 = null;
    private ArrayList<Boolean> itemChecked      = null;

    public my_custom_adapter(Context context, int type, ArrayList<String>  elements)
    {
        super(context, type, elements);
        this.elements =  elements;
        this.context = context;
        itemChecked = new ArrayList<Boolean>();
        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (intent.getAction().equals("master_check_change")) {
                    boolean check_value = intent.getBooleanExtra("check_value", false);
                    set_checked(check_value);
                    notifyDataSetChanged();
                }
            }
        };
        context.registerReceiver(receiver, new IntentFilter("master_check_change"));
        set_checked(false);
    }

    // AS EVERY TIME LISTVIEW INFLATE YOUR VIEWS WHEN YOU MOVE THEM SO YOU NEED TO SAVE ALL OF YOUR CHECKBOX STATES IN SOME ARRAYLIST OTHERWISE IT WILL SET ANY DEFAULT VALUE.
    private void set_checked(boolean is_checked)
    {
        for (int i=0; i < elements.size(); i++) {
            itemChecked.add(i, is_checked);
        }
    }

    //THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
    static class ViewHolder
    {
        public TextView tv;
        public CheckBox cb;
        public ImageView iv;
    }

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

        if (rowView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(
                                               Context.LAYOUT_INFLATER_SERVICE);
            // HERE I AM INFLATING LISTVIEW LAYOUT.
            rowView = inflater.inflate(R.layout.inflated_layout, null, false);
            holder = new ViewHolder();
            holder.cb = (CheckBox) rowView.findViewById(R.id.checkBox1);
            holder.tv = (TextView) rowView.findViewById(R.id.textView1);
            holder.iv = (ImageView) rowView.findViewById(R.id.imageView1);
            rowView.setTag(holder);

        } else {
            holder = (ViewHolder) rowView.getTag();
        }

        if (holder != null) {
            holder.tv.setText(elements.get(position));

            holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                                             boolean isChecked) {
                    itemChecked.set(position, isChecked);
                }
            });

            if(position < itemChecked.size()) {
                holder.cb.setChecked(itemChecked.get(position));
            }
        }
        return rowView;
    }
}

main.xmlファイルは次のとおりです。

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


    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

    </ListView>

</RelativeLayout>

inflated_layoutコードは次のとおりです。

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

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="17dp" />




    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/checkBox1"
        android:layout_toRightOf="@+id/imageView1"
        android:singleLine="true"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

arraylistの代わりに文字列配列を使用したい場合は、

    String[] elements = new String[10];
    for (int i = 0; i < 10; i++) {
        elements[i] = "elements " + i;
    }

//カスタムアダプタCUNSTRUCTORで

public my_custom_adapter(Context context, int type, String[]  elements)

それに応じてさらにいくつかの変更

于 2012-05-11T05:05:33.790 に答える
0
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
        fields, new int[] {R.id.contactEntryText});

See in this particular code you are only mapping text source (in field ) with actual textView (R.id.contactEntryText)... So similarly you need to add... another field and corresponding view to map for Checkbox.

or better make a CustomAdapter, you can find tutorials on that and override getView method,you get maximum flexibility.You can do whatever you want to do.

This might help: http://windrealm.org/tutorials/android/listview-with-checkboxes-without-listactivity.php

于 2012-05-11T04:14:09.640 に答える
-1

カスタムリストビューを使用しないでください。チェックボックスの機能を備えたデフォルトのリストビューを使用できますが、リストビュープロパティについては、Android開発者サイトのリストビューを読み取る各リストアイテムが1つだけです。チェックボックスのあるリストビューには、複数選択リストビューを設定する必要があります

編集1:

リンクをたどる:ここをクリック

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      setListAdapter(new ArrayAdapter<String>(this,
              android.R.layout.simple_list_item_multiple_choice, GENRES));

      final ListView listView = getListView();

      listView.setItemsCanFocus(false);
      listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
  }
于 2012-05-11T03:03:56.427 に答える