一千萬個為什麽

搜索

如何將Editext和Button添加到具有圖標和文本的Efficent Adapter

我想以這樣的方式創建一個布局,即頂部的edittext和按鈕應該在一行中。  我在editext中輸入的搜索文本,然後單擊搜索按鈕。然後我想顯示一個自定義列表視圖,其中每行包含圖像和文本。(根據我嘗試過的API演示示例list14)。但是當我運行應用程序時,按鈕和edittext被添加到每一行(即,每行包含圖像,文本,editext,按鈕。可以任何人指導如何解決此問題。

下面是我的xml文件:

<!--
    
-->





<!--
    
-->
<!--
    <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>
<!-- 

-->

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

最佳答案

你正在做的是修改它用於單個行的布局,這就是按鈕出現在每一行的原因。您需要做的是使用按鈕和textview為主布局創建XML文件,然後在其中嵌套ListView。例如,創建一個這樣的main.xml文件:



    

        

        

    

    

列表視圖的ID必須與上面完全相同( @android:id/list )。如果您不使用它,您的自定義“高效適配器”將無法找到它。完成後,您只需在super.onCreate(...)行之後立即將 setContentView(R.layout.main); 添加到onCreate方法。

轉載註明原文: 如何將Editext和Button添加到具有圖標和文本的Efficent Adapter