[英]android: ListActivity , custom adapter
I am trying to create custom adapter for list activity to show different images for each action. 我正在尝试为列表活动创建自定义适配器,以为每个操作显示不同的图像。 I am getting following error.
我收到以下错误。
Error: The method getView(int, View, ViewGroup) of type
ClientOperations.MyCustomAdapter must override or implement a supertype method
Can someone please help me. 有人可以帮帮我吗。
public class ClientOperations extends ListActivity {
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.clientoperations, parent, false);
TextView op=(TextView)row.findViewById(R.id.operation);
op.setText(operation[position]);
ImageView icon=(ImageView)row.findViewById(R.id.icon);
if (operation[position] == "Share"){
icon.setImageResource(R.drawable.filesharing);
} else if(operation[position] == "Remove"){
icon.setImageResource(R.drawable.remove);
} else if(operation[position] == "Exit"){
icon.setImageResource(R.drawable.logout);
} else {
icon.setImageResource(R.drawable.viewshare);
}
return row;
}
}
String username = null;
String[] operation = new String[] { "Share", "Servershare", "Usershare", "Myshare",
"Remove", "Exit"};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.clientoperations);
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
username = extras.getString("username");
}
// Use your own layout and point the adapter to the UI elements which
// contains the label
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.clientoperations,
R.id.operation, operation));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
.show();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="@+id/icon"
android:layout_height="wrap_content"
android:src="@drawable/icon"
android:layout_width="22px"
android:layout_marginTop="10px"
android:layout_marginRight="7px"
android:layout_marginLeft="7px">
</ImageView>
<TextView android:text="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:typeface="sans"
android:id="@+id/operation"
android:layout_marginTop="10px">
</TextView>
</LinearLayout>
I'm usually getting this error when the Java compiler assigned to the project is <= 1.4. 当分配给项目的Java编译器<= 1.4时,我通常会收到此错误。
Try this: On the project properties window -> Java compiler -> set the compiler compliance level to at least 1.5, preferably 1.6. 尝试以下操作:在项目属性窗口-> Java编译器->设置编译器符合级别至少为1.5,最好为1.6。 Rebuild.
重建。
尝试将@Override
与public void onCreate(Bundle savedInstanceState) {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.