Display whole content of listview item

筅森魡賤 提交于 2021-01-28 14:10:16

问题


Here I want to display whole content of the listview item on the listview. How can I achive this?

  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>  

Check the image:

Image of the listview

Thank you.

EDIT :

custom listview with checkbox / RadioButton. I got this but I need the single selection for that.

I try using this lstvw.setChoiceMode(ListView.CHOICE_MODE_SINGLE); but it not works for me.

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

I have no idea for the how to add code for the checkbox into the adapter class.please check the adapter class as below.

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" />


回答1:


See this question

android listview item height

wrap_content in the ListView won't do nothing, as the height that matters here is the one from the item.




回答2:


Please change it to this and check if you still have any issue...

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



回答3:


You have to create the custom list item layout for the listview. You cannot do with that default List Item android.R.layout.simple_list_item_single_choice




回答4:


android:layout_height="wrap_content" :)



来源:https://stackoverflow.com/questions/12245258/display-whole-content-of-listview-item

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!