我可以帮您翻译成中文:我有一个问题,我想在我的ListView上方放置一个EditText,并在该ListView中执行搜索。这个能做到吗?
以下是Activity代码:
以下是Activity代码:
public static class BirreChiareListView extends Main implements AdapterView.OnItemClickListener {
ListView list;
String [] birrechiare;
int [] images={R.drawable.ichnusa, R.drawable.ichnusa_speciale, R.drawable.ichnusa_cruda};
private MyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(birrechiare_listview);
Resources res=getResources();
birrechiare=res.getStringArray(R.array.birre_chiare);
list = (ListView)findViewById(R.id.listViewBirreChiare);
MyAdapter adapter = new MyAdapter(this,birrechiare, images);
list.setAdapter(adapter);
list.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> adapter, View view, int position, long l) {
String item = adapter.getItemAtPosition(position).toString();
Toast.makeText(getApplicationContext(), "Hai selezionato: " + item, Toast.LENGTH_SHORT).show();
if (position == 0){
Intent gotoclassica = new Intent(BirreChiareListView.this,Ichnusa_Classica.class);
startActivity(gotoclassica);
}
if (position == 1){
Intent gotospeciale = new Intent(BirreChiareListView.this,Ichnusa_Speciale.class);
startActivity(gotospeciale);
}
if (position == 2){
Intent gotocruda = new Intent(BirreChiareListView.this,Ichnusa_Cruda.class);
startActivity(gotocruda);
}
}
}
static class MyAdapter extends ArrayAdapter<String> {
Context context;
int[] images;
String[] titleArray;
MyAdapter(Context c, String[] titles, int imgs []){
super (c, single_row, R.id.Titletxt, titles);
this.context = c;
this.images=imgs;
this.titleArray=titles;
}
@Override
public View getView (int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(single_row, parent,false);
ImageView myimage = (ImageView) row.findViewById(R.id.imageView);
TextView mytitle = (TextView) row.findViewById(R.id.Titletxt);
myimage.setImageResource(images[position]);
mytitle.setText(titleArray[position]);
return row;
}
}
好的,这是ListView的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background_listview">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Birre Chiare"
android:singleLine="false"
android:textColor="#FFFFFF"
android:gravity="center"/>
<EditText
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<ListView
android:id="@+id/listViewBirreChiare"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
最终,这是single_row.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="@drawable/ichnusa"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/Titletxt"
android:layout_alignBottom="@+id/imageView"
android:layout_toRightOf="@+id/imageView"
android:layout_marginBottom="35dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>