android - 如何使列表项可检查?

标签 android listview

我想创建一个 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>

最佳答案

您可以使用 Adapter 类对 ListView 的每一行进行布局:

首先,创建一个将膨胀到每一行的布局 (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>

注意 Checkboxandroid:focusable 属性。

然后,创建一个扩展 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;
    }

}

在您的 Activity 中,创建一个新的 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) {

            }
        });
    }
}

关于android - 如何使列表项可检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13466686/

相关文章:

java - 如何在android中创建动态水平表格行?

android - ViewPager FragmentPagerAdapter 只更新最后一个 fragment onNavigationItemSelected

android - 如何知道 fragment 何时在 viewpager 中实际可见

java - 以编程方式 android 中 LinearLayout 中微调器和 TextView 的布局对齐

C# WPF listview不更新

java - 使用 JSON 文件填充 ListView

wpf - 在 ListView 中选择mvvm ComboBox

java - 将 sqlite 数据从 ListView 传递到其他 Activity

Android缓存图像的最佳方式

java - 我无法将 Edittext Multiline 插入服务器