views:

520

answers:

4

Hi guys... I have a list Activity 4 which i have extended the BaseAdapter and the getview looks like this...

public View getView(int position, View convertView, ViewGroup parent) {
            View row=null;
            for(int i=0;i<10;)
            {   
            row = convertView;
            if(row==null)
            {
                LayoutInflater inflater = mContext.getLayoutInflater();
                row = inflater.inflate(R.layout.parsed,null);
            }
            TextView id = (TextView)row.findViewById(R.id.id);
            id.setText(idvector.elementAt(position));
            TextView photo = (TextView)row.findViewById(R.id.photo);
            photo.setText(photovector.elementAt(position));
            TextView subcategory = (TextView)row.findViewById(R.id.subcategory);
            subcategory.setText(subcategoryvector.elementAt(position));
            TextView name = (TextView)row.findViewById(R.id.name);
            name.setText(namevector.elementAt(position));
            }
            return(row);
        }

    }

now my problem is that the list is not giving all the values of the vector and nor is it scrolling.. how to make my list Scrollable???

A: 

Hi!
I'm not sure but is seems that you didn't put items to your adapter. In your code snipet you are retrieving elements from different vectors (idvector, subcategoryvector, etc.). Probably you should create a class that will stand for item type, hold there all you need - id, subcategory, photo) and add that item to your adapter.
Please, check the size of your adapter.

One more thing. It seems that "for" loop in your code is not needed.

Regards!

Ok, here is some example. RowData is a class for my items. As you can see my custom adapter extends ArrayAdapter < RowData > I can just put there new items by calling adapter.add(rowInstance).


public class CustomList extends ListActivity {
private LayoutInflater mInflater;
private Vector data;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        
    mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    data = new Vector();
    RowData rd = new RowData("item1", "description1");
    data.add(rd);
    rd = new RowData("item2", "description2");
    data.add(rd);
    rd = new RowData("item2", "description3");
    data.add(rd);

    CustomAdapter adapter = new CustomAdapter(this, R.layout.custom_row,R.id.item, data);
    setListAdapter(adapter);        
    getListView().setTextFilterEnabled(true);
}


public void onListItemClick(ListView parent, View v, int position, long id) {
    CustomAdapter adapter = (CustomAdapter) parent.getAdapter();
        RowData row = adapter.getItem(position);                
    Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(row.mItem); 
    builder.setMessage(row.mDescription + " -> " + position );
    builder.setPositiveButton("ok", null);
    builder.show();
}

/**
 * Data type used for custom adapter. Single item of the adapter.      
 */
private class RowData {
    protected String mItem;
        protected String mDescription;

        RowData(String item, String description){
        mItem = item;
        mDescription = description;             
    }

        @Override
        public String toString() {
                return mItem + " " +  mDescription;
        }
}

private class CustomAdapter extends ArrayAdapter < RowData > {

        public CustomAdapter(Context context, int resource,
                        int textViewResourceId, List objects) {
                super(context, resource, textViewResourceId, objects);

        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                ViewHolder holder = null;

                //widgets displayed by each item in your list
                TextView item = null;
                TextView description = null;

                //data from your adapter
                RowData rowData= getItem(position);


                //we want to reuse already constructed row views...
                if(null == convertView){
                        convertView = mInflater.inflate(R.layout.custom_row, null);
                        holder = new ViewHolder(convertView);
                        convertView.setTag(holder);
                }
                // 
                holder = (ViewHolder) convertView.getTag();
                item = holder.getItem();
                item.setText(rowData.mItem);

                description = holder.getDescription();          
                description.setText(rowData.mDescription);

                return convertView;
        }
}

/**
 * Wrapper for row data.
 *
 */
private class ViewHolder {      
    private View mRow;
    private TextView description = null;
    private TextView item = null;

        public ViewHolder(View row) {
        mRow = row;
        }

        public TextView getDescription() {
                if(null == description){
                        description = (TextView) mRow.findViewById(R.id.description);
                }
                return description;
        }

        public TextView getItem() {
                if(null == item){
                        item = (TextView) mRow.findViewById(R.id.item);
                }
                return item;
        }       
}
Ramps
Thanx Ramps... but i didnt perfectly get wat u said... i mean how can i hold everything in class and pass them to a baseadapter??? a simple example code will be very usefulRegards!
JaVadid
well i removed the "for" loop but no change is visible... only 4 rows are drawn on screen while the others don show up... actually there should be around 8rows... Don know wat is creating problem...
JaVadid
Ok, I'll prepare a simple code example... hold a moment :)
Ramps
A: 

Do you mean that if your ListView contents are larger than the area you are displaying it in, you want it to scroll? If so, add a ScrollView around what you want to scroll.

// inside of the parent layout(most likely a Linear Layout)
<ScrollView android:layout_height="wrap_content" 
            android:id="@+id/ScrollView01" 
            android:layout_width="wrap_content" 
            android:scrollbarAlwaysDrawHorizontalTrack="false" 
            android:scrollbars="vertical">

      <RelativeLayout android:id="@+id/RelativeLayout01" 
                      android:layout_width="wrap_content" 
                      android:layout_height="wrap_content">

             <ListView android:id="@id/android:list" 
                       android:layout_width="wrap_content" 
                       android:layout_height="380dip"
                       android:layout_marginLeft="20dip" 
                       android:layout_marginRight="20dip"
                       android:layout_marginTop="130dip" />     
      </RelativeLayout>
</ScrollView>
Eclipsed4utoo
No, you shouldn't put a ListView inside a ScrollView; the ListView already handles scrolling.. you're going to end up in a weird situation where the views get confused about who will handle the user's scroll events.
Christopher
I swear I had tried it without it and it didn't scroll, that's why I used a ScrollView. Just went back and tried it again and it did scroll on it's own. Guess I missed something the first time around.
Eclipsed4utoo
A: 

The getView method is meant to inflate, populate and return a single list row.

All you should need to do is:

public View getView(int position, View convertView, ViewGroup parent) {
    // Inflate the individual row from the 'parsed' layout
    View row = mInflater.inflate(R.layout.parsed);

    // Set the properties on the inflated row
    TextView id = (TextView) row.findViewById(R.id.id);
    id.setText(idvector.elementAt(position));
    TextView photo = (TextView) row.findViewById(R.id.photo);
    photo.setText(photovector.elementAt(position));
    TextView subcategory = (TextView) row.findViewById(R.id.subcategory);
    subcategory.setText(subcategoryvector.elementAt(position));
    TextView name = (TextView) row.findViewById(R.id.name);
    name.setText(namevector.elementAt(position));

    return row;
}

What Ramps was saying is that rather than creating your BaseAdapter and passing in four separate vectors, you could create a single Vector (or List) of Photo objects (or whatever it is you're representing), each with id, photo, subcategory and name member variables. i.e. make things a bit more object oriented! :)

As for the list not scrolling; it will only scroll if the number of items in the list exceeds the screen space available. If you only have four items being displayed in your list, then nothing will scroll.

You should also check how you're calling the BaseAdapter super method (presumably you're extending ArrayAdapter): are you passing in one of the Vectors of data, or some other data structure? If the base adapter doesn't have the correct information you want to render, it isn't going to render it! :)

Christopher
A: 

sorry guys... very very sorry... so foolish of me... i had not changed the following code... sorry guys 4 wasting a lot f ur precious time...

  public int getCount() 
{
                return idvector.size();
            }

Thanx a lot Ramps... i'm going to implement the procedure u showed...

JaVadid
Not a problem, glad I could help. Regards!
Ramps