我想以这样一种方式创建一个布局,即顶部的edittext和按钮应该在一行中。我在editext中输入的搜索文本并单击搜索按钮。然后我想显示一个自定义列表视图,其中每一行都包含图像和文本。(根据我尝试过的 API 演示示例 list14)。但是当我运行应用程序时,按钮和edittext被添加到每一行(即每一行包含一个图像、文本、editext、按钮。任何人都可以指导如何解决这个问题。
下面是我的xml文件:
<!--
<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>
-->
和 Java 文件:/** * */ package org.techdata.activity;
导入 android.app.AlertDialog;导入android.app.ListActivity;导入android.content.Context;导入android.content.Intent;导入android.graphics.Bitmap;导入android.graphics.BitmapFactory;导入android.os.Bundle;导入android.util.Log;导入 android.view.LayoutInflater;导入android.view.View;导入android.view.ViewGroup;导入 android.widget.BaseAdapter;导入android.widget.Button;导入 android.widget.EditText;导入android.widget.ImageView;导入 android.widget.ListView;导入 android.widget.TextView;
/** * @author jayanthg * */ 公共类 ProductSearch 扩展 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();
}
}
}
问候:贾扬斯