android:ListActivity,自定义适配器

时间:2011-10-21 17:52:28

标签: android adapter listactivity android-arrayadapter

我正在尝试为列表活动创建自定义适配器,以便为每个操作显示不同的图像。我收到了以下错误。

Error: The method getView(int, View, ViewGroup) of type 
ClientOperations.MyCustomAdapter must override or implement a supertype method

有人可以帮帮我吗。

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>

2 个答案:

答案 0 :(得分:0)

当分配给项目的Java编译器为&lt; = 1.4。

时,我通常会收到此错误

试试这个:在项目属性窗口 - &gt; Java编译器 - &gt;将编译器合规性级别设置为至少1.5,最好是1.6。重建。

答案 1 :(得分:0)

尝试将@Overridepublic void onCreate(Bundle savedInstanceState) {

一起使用