Sam
Sam

Reputation: 4284

Custom list view is not diplaying properly

I have created an app which uses a custom listview Fragment. But the custom listview is not displaying properly.

below attached is my screen shot.

enter image description here

ANd here is my Xml file of individual row item

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

<TextView
    android:id="@+id/Jobtitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="7dp"
    android:paddingRight="7dp"
    android:paddingTop="2dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/green"
    android:textStyle="bold" >
</TextView>

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/Jobtitle"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp" 
    android:paddingBottom="5dp">

    <TextView
        android:id="@+id/publishdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PubDate:-"
        android:textColor="@color/textgrey"
        />

    <TextView
        android:id="@+id/lefttime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/publishdate"
        android:layout_alignBottom="@+id/publishdate"
        android:layout_alignParentRight="true"
        android:text="Time Left:-"
        android:textColor="@color/textgrey"
         />
  </RelativeLayout>

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/relativeLayout1"
    android:background="@drawable/dividr" >
 </LinearLayout>

and here is the list fragment layout file

<?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="wrap_content"
android:orientation="vertical" >

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

<TextView
    android:id="@android:id/empty"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="No text" >

</TextView>

And here is my adapter

 package my.newapp.freelanceeye;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class joblisted extends ListFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapt my=new adapt(getActivity(), R.layout.listrow,m);
        setListAdapter(my);
    }

    Context ctx;
    public class adapt extends ArrayAdapter<String>{
       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
           LayoutInflater inflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View row=inflater.inflate(R.layout.listrow,parent,false);
           return row;
            //return super.getView(position, convertView, parent);

        }

        public adapt(Context context, int textViewResourceId, String[] objects) {
            super(context, textViewResourceId, objects);
            ctx=context;


        }


    }
    String[] m={"as","asa","sd","Ad","sad","asda"};

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View v=inflater.inflate(R.layout.listfragmnetlayout,container,false);
            return v;

            }


    }

Upvotes: 0

Views: 771

Answers (4)

Saugat man ligal
Saugat man ligal

Reputation: 21

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

Change layout_height to fill_parent

Upvotes: 0

ishu
ishu

Reputation: 1332

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Large Text"
            android:textColor="#339966"
            android:textAppearance="?android:attr/textAppearanceLarge" 
            android:textStyle="bold" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                  android:text="PubDate:-"
                android:textColor="#606060"
                 />

            <TextView
                android:id="@+id/text3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Time Left:-"
                android:textColor="#606060"
                android:paddingLeft="180dp"
               />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

try this....

Upvotes: 0

waqaslam
waqaslam

Reputation: 68167

You should set your fragment's layout something like this:

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

    <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</<LinearLayout>

I think the problem is with height of your Views, thats why try it with match_parent.

Upvotes: 1

Emil Adz
Emil Adz

Reputation: 41099

Your are building a custom array adapter but your are not setting the parameters in your row fields.

Take a look at this adapter implementation, there are comments explaining every step:

private class CustomArrayAdapter extends ArrayAdapter<RowData>
{   
private ArrayList<RowData> list;

//this custom adapter receives an ArrayList of RowData objects.
//RowData is my class that represents the data for a single row and could be anything.
public CustomArrayAdapter(Context context, int textViewResourceId, ArrayList<RowData> rowDataList) 
{
    //populate the local list with data.
    super(context, textViewResourceId, rowDataList);
    this.list = new ArrayList<RowData>();
    this.list.addAll(rowDataList);
}

public View getView(final int position, View convertView, ViewGroup parent)
{
    //creating the ViewHolder we defined earlier.
    ViewHolder holder = new ViewHolder();) 

    //creating LayoutInflator for inflating the row layout.
    LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    //inflating the row layout we defined earlier.
    convertView = inflator.inflate(R.layout.row_item_layout, null);

    //setting the views into the ViewHolder.
    holder.title = (TextView) convertView.findViewById(R.id.tvItemTitle);
    holder.changeRowStatus = (ImageView) convertView.findViewById(R.id.iStatus);
    holder.changeRowStatus.setTag(position);

    //define an onClickListener for the ImageView.
    holder.changeRowStatus.setOnClickListener(new OnClickListener() 
    {           
        @Override
        public void onClick(View v) 
        {
            Toast.makeText(activity, "Image from row " + position + " was pressed", Toast.LENGTH_LONG).show();
        }
    });
    holder.checked = (CheckBox) convertView.findViewById(R.id.cbCheckListItem);
    holder.checked.setTag(position);

    //define an onClickListener for the CheckBox.
    holder.checked.setOnClickListener(new OnClickListener() 
    {       
        @Override
        public void onClick(View v)
        {
            //assign check-box state to the corresponding object in list.    
            CheckBox checkbox = (CheckBox) v;
            rowDataList.get(position).setChecked(checkbox.isChecked());
            Toast.makeText(activity, "CheckBox from row " + position + " was checked", Toast.LENGTH_LONG).show();    
        }
    });

    //setting data into the the ViewHolder.
    holder.title.setText(RowData.getName());
    holder.checked.setChecked(RowData.isChecked());

    //return the row view.
    return convertView;
}
}

Upvotes: 0

Related Questions