How to add Editext and Button to Efficent Adapter which has a Icon and text

Posted by jayanthgande on Stack Overflow See other posts from Stack Overflow or by jayanthgande
Published on 2010-04-19T12:48:34Z Indexed on 2010/04/19 12:53 UTC
Read the original article Hit count: 582

Hi,

I want to create a layout in such a way that on top edittext and button should be there in one row. The search text I enter in editext and click on search button. Then I want to display a custom list view where each row contains image and text.(As per the API demos example list14 I have tried). But when I run the application, button and edittext are being added to each row (i.e., Each row contains a image, text, editext, button. Can Any one guide how to resolve this issue.

Below is my xml file:

<!--
    <FrameLayout android:layout_width="wrap_content"
    android:layout_height="0dip" android:layout_weight="1"></FrameLayout>
-->
<ImageView android:id="@+id/icon" android:layout_width="48dip"
    android:layout_height="48dip" />

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


<!--
    <EditText android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/prdsearchtb"
    android:text="@string/tb_prd_search_lbl"></EditText>
-->
<!--
    <TableLayout android:id="@+id/TableLayout01"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"> <TableRow>
-->
<Button android:id="@+id/prdsrcbutton" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="@string/btn_lbl_prd_search"
    android:layout_x="2px" android:layout_y="410px"></Button>
<!-- </TableRow>
</TableLayout>

-->

and Java File: /** * */ package org.techdata.activity;

import android.app.AlertDialog; 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.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView;

/** * @author jayanthg * */ public class ProductSearch extends ListActivity {

private static class ProductSearchAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    private Bitmap mIcon1;
    private Bitmap mIcon2;

    public ProductSearchAdapter(Context context) {

        mInflater = LayoutInflater.from(context);

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

    @Override
    public int getCount() {

        return DATA.length;
    }

    @Override
    public Object getItem(int position) {

        return position;
    }

    @Override
    public long getItemId(int position) {

        return position;
    }

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

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.productsearch, 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);
            btn=(Button)convertView.findViewById(R.id.prdsrcbutton);
            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);

        holder.icon.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i("image", " u clicked on icon Position" + position);

            }
        });
        holder.text.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i("Text", " u clicked on text Position" + position);

            }
        });

        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i("Button","U clicked on button");

            }
        });


        return convertView;
    }

    static class ViewHolder {
        TextView text;
        ImageView icon;
    }

    private static final String[] DATA = { "Abbaye de Belloc",
            "Abbaye du Mont des Cats" };

}

ListView product_search_list;
Button srch_btn;
EditText srch_text;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setListAdapter(new ProductSearchAdapter(this));
    // setContentView(R.layout.productsearch);
    // getListView().setEmptyView(findViewById(R.id.text));
    // srch_text = (EditText)findViewById(R.id.prdsearchtb);
    // srch_btn = (Button) findViewById(R.id.prdsearchtb);
    // srch_btn.setOnClickListener(new View.OnClickListener() {
    //      
    // @Override
    // public void onClick(View v) {
    // callProductSearchAdapter();
    //
    // }
    // });

}

void callProductSearchAdapter() {
    setListAdapter(new ProductSearchAdapter(this));
}

private void createDialog(String title, String text, final Intent i) {
    if (i == null) {
        AlertDialog ad = new AlertDialog.Builder(this).setIcon(
                R.drawable.alert_dialog_icon).setPositiveButton("Ok", null)
                .setTitle(title).setMessage(text).create();
        ad.show();
    }
}

}

Regards: Jayanth

© Stack Overflow or respective owner

Related posts about android

Related posts about listview