1

这里我要显示whole content of the listview itemlistview. 我怎样才能做到这一点?

  ArrayAdapter< String> myadapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_single_choice,answers);
     listview.setAdapter(myadapter);
     listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

主.xml

<ListView
    android:id="@+id/questions_list"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:cacheColorHint="@android:color/transparent" >
</ListView>  

检查图像:

列表视图的图像

谢谢你。

编辑 :

带有复选框/单选按钮的自定义列表视图。我得到了这个,但我需要一个单一的选择。

我尝试使用它lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE);,但它不适用于我。

main.java

  private ImageAdapter  adapter; 

private static String month[] = {"January","February","March","April","May",  
    "June","July","August","September",  
    "October","November","December"};  


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ListView lstvw = (ListView) findViewById(R.id.listView);

     adapter = new ImageAdapter(this, month);  
     lstvw.setAdapter(adapter);  
      lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

     lstvw.setOnItemClickListener(this);
}

我不知道如何将复选框的代码添加到适配器类中。请检查适配器类,如下所示。

ImageAdapter.class

public class ImageAdapter extends BaseAdapter{

public String title[];  
public String description[];  
public Activity context;  
public LayoutInflater inflater;

public ImageAdapter(Activity context,String[] title) {  
    super();  

    this.context = context;  
    this.title = title;  

    this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
}  

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

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

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

public static class ViewHolder  
{  

    TextView txtViewTitle;  
    RadioButton radiobtn;

}  

@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
    // TODO Auto-generated method stub  

    ViewHolder holder;  
    if(convertView==null)  
    {  
        holder = new ViewHolder();  
        convertView = inflater.inflate(R.layout.listitem, null);  

        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.lstvw_textView);  

         holder.radiobtn = (RadioButton) convertView.findViewById(R.id.itemradio);

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

    holder.txtViewTitle.setText(title[position]);  

    /* holder.radiobtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            RadioButton rb = (RadioButton) v;
            if (rb.isChecked() == true) 
            {
                // Here you will get list of checked item.

            }

            else 
           {
            // Here you will get list of unchecked item.    
            }

        }

    }); */

    return convertView;  
}   

}

列表项.xml

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

<TextView
    android:id="@+id/lstvw_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/itemCheckBox"
    android:padding="8dp"
    android:text="hello world" />



<CheckBox
    android:id="@+id/itemCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:gravity="right" />

4

4 回答 4

1

看到这个问题

android listview项目高度

ListView 中的 wrap_content 不会做任何事情,因为这里重要的高度是项目的高度。

于 2012-09-03T09:39:36.357 回答
0

android:layout_height="wrap_content":)

于 2012-09-03T09:33:43.843 回答
0

请把它改成这个并检查你是否还有任何问题......

<ListView
    android:id="@+id/questions_list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:cacheColorHint="@android:color/transparent" >
</ListView>
于 2012-09-03T09:54:01.040 回答
0

您必须为列表视图创建自定义列表项布局。您不能使用该默认列表项android.R.layout.simple_list_item_single_choice

于 2012-09-03T09:54:25.077 回答