显示listview项目的全部内容

时间:2012-09-03 09:32:33

标签: android listview listviewitem

我希望在whole content of the listview item上显示listview。我怎么能做到这一点?

  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);

main.xml中

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

检查图像:

Image of the listview

谢谢。

编辑:

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

我尝试使用此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;  
}   

}

Listitem.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 个答案:

答案 0 :(得分:1)

看到这个问题

android listview item height

ListView中的

wrap_content将不会执行任何操作,因为此处重要的高度是项目中的高度。

答案 1 :(得分:0)

android:layout_height="wrap_content":)

答案 2 :(得分:0)

请将其更改为此并检查您是否还有任何问题......

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

答案 3 :(得分:0)

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