我是android的新手。目前我试图通过使用适配器来显示一个项目列表的Android应用程序,如下所示:
public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
private final List<Model> list;
private final Activity context;
protected Filter filter;
public InteractiveArrayAdapter(Activity context, List<Model> list) {
super(context, R.layout.mylist, list);
this.context = context;
this.list = list;
}
class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
protected ImageView icon_verify, icon_auth;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.mylist, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
Typeface face=Typeface.createFromAsset(getAssets(), "fonts/verdana.ttf");
viewHolder.text.setTypeface(face);
viewHolder.icon_verify = (ImageView) view.findViewById(R.id.icon_verify);
if(list_m.get(position).isVerified() == true)
viewHolder.icon_verify.setImageResource(R.drawable.verify_icon2);
else
viewHolder.icon_verify.setImageResource(R.drawable.verify_icon);
viewHolder.icon_auth = (ImageView) view.findViewById(R.id.icon_auth);
if(list_m.get(position).isAuthorised() == true)
viewHolder.icon_auth.setImageResource(R.drawable.auth_icon2);
else
viewHolder.icon_auth.setImageResource(R.drawable.auth_icon);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox.getTag();
element.setSelected(buttonView.isChecked());
System.out.println("checked change");
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
}
mylist.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="wrap_content" >
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/label"
android:layout_marginLeft="70px"
android:layout_marginTop="10px"
android:textSize="22px" >
</TextView>
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="0sp"
android:focusable="false"
android:layout_marginLeft="10dp"
android:button="@layout/checkbox" />
<ImageView
android:id="@+id/icon_verify"
android:layout_width="30px"
android:layout_height="30px"
android:layout_marginLeft="600px"
android:layout_marginRight="10px"
android:layout_marginTop="30px" >
</ImageView>
<ImageView
android:id="@+id/icon_auth"
android:layout_width="30px"
android:layout_height="30px"
android:layout_marginLeft="650px"
android:layout_marginRight="5px"
android:layout_marginTop="30px" >
</ImageView>
</RelativeLayout>
在标题处有一个edittext字段。
final View header = getLayoutInflater().inflate(R.layout.header, null);
final EditText search=(EditText)header.findViewById(R.id.txtSearch);
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
adapter1.getFilter().filter(cs.toString());
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
header.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="208dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTradeTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/trade_crtl"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/text_color_grey" />
<EditText
android:id="@+id/txtSearch"
android:layout_width="400px"
android:layout_height="70px"
android:layout_marginTop="15px"
android:imeOptions="actionSearch"
android:inputType="text" >
</EditText>
</LinearLayout>
<ImageButton
android:id="@+id/btn_search"
android:layout_width="70px"
android:layout_height="70px"
android:layout_marginLeft="10px"
android:layout_marginRight="5px"
android:layout_marginTop="70px"
android:src="@drawable/search_icon" />
</LinearLayout>
但是,每次应用搜索时,列表中的图像都会发生变化。任何人都可以在这里建议我做错了什么。任何帮助将非常感激。感谢。
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.cslucas"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_logo"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.android.cslucas.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.android.cslucas.Trade"
android:windowSoftInputMode="stateVisible|adjustPan"></activity>
</application>
</manifest>