1

リスト項目をチェックできるように作成しListViewたいのですが、自分で表示用のレイアウトを作成していて、チェック可能にListViewする方法がわかりません。それで、誰かがいくつかのアドバイスを与えることができますか?以下のコードは、私の質問をより明確に説明します。リストアイテムをチェック可能にしたい場合は、以下のコードを示します。

setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,presidents));

しかし、私のコードがこのようなものである場合はどうですか?その中に他のレイアウトがあります:

ArrayAdapter<String> list = new ArrayAdapter<String>(NotificationView.this,R.layout.friendlist_item, R.id.friend_name,people);
lv.setAdapter(list); 

ありがとう..

これがfriendlist_item.xmlです:

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

<TextView android:id="@+id/friend_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="20dip"
    android:textStyle="bold"/>  

</LinearLayout>  

ここでview.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#ffffff">

<ListView android:id="@android:id/list" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_weight="1">
</ListView>

<FrameLayout android:id="@+id/FrameLayout01" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content">


  <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="fill_parent"
      android:background="#ffffff"
      android:orientation="horizontal" >


      <Button
          android:id="@+id/acceptbtn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_horizontal"
          android:layout_weight="0.46"
          android:text="Accept" />


      <Button
          android:id="@+id/closebtn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="0.49"
          android:text="Close" />

  </LinearLayout>

 </FrameLayout>

 </LinearLayout>
4

2 に答える 2

1

ListViewAdapterクラスを使用しての各行をレイアウトできます。

まず、各行(list_adapter.xml)に拡張されるレイアウトを作成します。

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

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:text="CheckBox" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="15dp"
        android:layout_marginTop="5dp"
        android:gravity="right|center_horizontal"
        android:text="ghg"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:ignore="HardcodedText" />

</LinearLayout>

android:focusableの属性に注意してCheckboxください。

次に、拡張するクラスBaseAdapter(ListAdapter.java)を作成します。

    public class ListAdapter extends BaseAdapter {

    private LayoutInflater myInflater;

    public ListAdapter(Context context) {
        super();
        myInflater = LayoutInflater.from(context);

    }

    static class ViewHolder {
        TextView tvName;
    }

    @Override
    public int getCount() {

        return 0;
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

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

        if (convertView == null) {
            convertView = myInflater.inflate(R.layout.list_adapter, parent,
                    false);
            holder = new ViewHolder();
            holder.tvName = (TextView) convertView.findViewById(R.id.tvName);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvName.setTag(list.get(position).getId());
        holder.tvName.setText(list.get(position).getName());
        // Log.d("Ganjoor", "Adapter: " + list.get(position).getName());

        if (position % 2 == 0) {
            convertView.setBackgroundResource(R.drawable.grad_blue);
        } else {
            convertView.setBackgroundResource(R.drawable.row_style);
        }

        return convertView;
    }

}

アクティビティで、の新しいインスタンスを作成し、それを:ListAdapterに割り当てます。ListView

    public class MainActivity extends Activity {

    ListView listView;
    private ListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        adapter = new ListAdapter(this);

        listView = (ListView) findViewById(R.id.listView1);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

            }
        });
    }
}
于 2012-11-20T05:03:51.530 に答える
0

現在、各リストアイテムに表示するウィジェットfriendlist_item.xmlしかありません。TextView

チェックボックスが必要な場合は、CheckBoxウィジェットを置き換える(または追加する)必要があります。それらの例の一部であり、コードにすばやく適応させるには、次のようにします。

<!-- language: lang-java  -->
<CheckBox android:id="@+id/checkbox_meat"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/meat"
    android:onClick="onCheckboxClicked"/>

<CheckBox android:id="@+id/friend_name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dip"
    android:textSize="20dip"
    android:textStyle="bold"/>  
于 2012-11-20T05:02:06.647 に答える