Android: ListActivity, пользовательский адаптер - PullRequest
0 голосов
/ 21 октября 2011

Я пытаюсь создать собственный адаптер для работы со списком, чтобы показать разные изображения для каждого действия. Я получаю следующую ошибку.

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 голосов
/ 21 ноября 2011

Попробуйте использовать @Override с public void onCreate(Bundle savedInstanceState) {

0 голосов
/ 21 октября 2011

Обычно я получаю эту ошибку, когда назначенный для проекта компилятор Java <= 1.4. </p>

Попробуйте это: В окне свойств проекта -> Компилятор Java -> установите уровень соответствия компилятора на atминимум 1,5, предпочтительно 1,6.Перестроить.

...