views:

190

answers:

2

I have written a ListView with a text and image clubbed together in a row. I wanted to add a button on top of listview, then the entire content of listview should be seen. Button is named as "More", when someone clicks it i will be updating the listcontent. That is the main theme.

Can anyone suggest how to do it?

My java file.

package com.example.firstandroid;


import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class Screen2 extends ListActivity  {

String result="";
private static String DATA[];        
/**
 * Demonstrates how to write an efficient list adapter. The adapter used in this example binds
 * to an ImageView and to a TextView for each row in the list.
 *
 * To work efficiently the adapter implemented here uses two techniques:
 * - It reuses the convertView passed to getView() to avoid inflating View when it is not necessary
 * - It uses the ViewHolder pattern to avoid calling findViewById() when it is not necessary
 *
 * The ViewHolder pattern consists in storing a data structure in the tag of the view returned by
 * getView(). This data structures contains references to the views we want to bind data to, thus
 * avoiding calls to findViewById() every time getView() is invoked.
 */


private static class EfficientAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Bitmap mIcon1;
    private Bitmap mIcon2;

    public EfficientAdapter(Context context) {
        // Cache the LayoutInflate to avoid asking for a new one each time.
        mInflater = LayoutInflater.from(context);

        // Icons bound to the rows.
        mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.next_arrow);
        mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.next_arrow);
    }

    /**
     * The number of items in the list is determined by the number of speeches
     * in our array.
     *
     * @see android.widget.ListAdapter#getCount()
     */
    public int getCount() {
        return DATA.length+1;
    }

    /**
     * Since the data comes from an array, just returning the index is
     * sufficent to get at the data. If we were using a more complex data
     * structure, we would return whatever object represents one row in the
     * list.
     *
     * @see android.widget.ListAdapter#getItem(int)
     */
    public Object getItem(int position) {
        return position;
    }

    /**
     * Use the array index as a unique id.
     *
     * @see android.widget.ListAdapter#getItemId(int)
     */
    public long getItemId(int position) {
        return position;
    }

    /**
     * Make a view to hold each row.
     *
     * @see android.widget.ListAdapter#getView(int, android.view.View,
     *      android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) {
        // A ViewHolder keeps references to children views to avoid unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is no need
        // to reinflate it. We only inflate a new View when the convertView supplied
        // by ListView is null.
        if(position ==0)
        {
            convertView = mInflater.inflate(R.layout.main2, null);
        }
        else
        {


        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.main1, null);

            // Creates a ViewHolder and store references to the two children views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.text);
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);

            convertView.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the TextView
            // and the ImageView.
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        holder.text.setText(DATA[position]);
        holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);
        }
        return convertView;
    }

    static class ViewHolder {
        TextView text;
        ImageView icon;
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent myIntent = new Intent(this , firstandroid.class);
    //startActivityForResult(myIntent, 0);
    Bundle bun = getIntent().getExtras();

    result = bun.getString("mani");


    try
    {
    JSONObject json=new JSONObject(result);
    JSONArray ja;
    json = json.getJSONObject("responseData");
    ja = json.getJSONArray("results");

    int resultCount = ja.length();
    DATA = new String[resultCount];
    for (int i = 0; i < resultCount; i++)
      {
      JSONObject resultObject = ja.getJSONObject(i);
      DATA[i]=resultObject.get("titleNoFormatting").toString();
      DATA[i]+="\n";
      JSONArray addr;
      addr = resultObject.getJSONArray("addressLines");
      int count = addr.length();
      for(int j=0;j<count;j++)
      {
         // JSONObject resultObject1 = ja.getJSONObject(j);
          DATA[i]+=addr.getString(j);


      }
      }
    }
    catch(Exception e)
    {


    }
    setListAdapter(new EfficientAdapter(this));
}

}

My xml file. /res/layout

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

TextView android:id="@+id/text" 
android:layout_gravity="center_vertical" 
android:layout_width="0dip" 
android:layout_weight="1.0" 
android:layout_height="wrap_content" />

ImageView android:id="@+id/icon" 
android:layout_width="30dip" 
android:layout_height="30dip" />

</LinearLayout>
A: 

Two possibilities:

1) Android's ListView already has methods for what you are trying to do.

2) Use commonsguy's cwac-endless.

The second one feels more android way :)

Macarse
A: 

There are two solutions :

1) use the addHeaderView(View v) method from ListView

2) use the weight attribute in your xml layout like :

<?xml version="1.0" encoding="utf-8" ?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" 
  android:layout_width="match_parent"
  android:layout_height="match_parent">
  <Button
    android:id="@+id/moreButton"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/moreButtonLabel"/>
  <ListView 
    android:id="@+id/aList" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"/>
</LinearLayout>
Mathieu