OnItemClickListener无法解析为一个类型(在Fragment内)

时间:2014-11-17 14:47:07

标签: android android-fragments android-listview

这似乎是一个常见的问题,通常通过添加正确的导入来解决。但就我而言,它似乎并没有解决任何问题。我特别是在Android中编程相当新,所以请忍受我的愚蠢错误(如果适用)。

我有以下导入和代码,但Eclipse一直说AdapterView.onItemClickListener无法解析为类型。任何帮助将不胜感激。

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public static class MainApplicationFragment extends Fragment {
    private ArrayAdapter<String> listAdapter;
    public MainApplicationFragment() {

    }

    private void PopulateWorkorderList() throws IOException {

        StorageAccess access = new StorageAccess();
        listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.workorder_list_row);

        for (Workorder WO : access.GetAllWorkorders()) {
            listAdapter.add(WO.getWorkorderNumber().toString());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        try {
            PopulateWorkorderList();
            ListView mainListView = (ListView) getView().findViewById( R.id.workorderList );
            mainListView.setAdapter(listAdapter);   
            mainListView.setOnItemClickListener(new AdapterView.onItemClickListener() {
                   public void onItemClick(AdapterView<> parent, View view, int position, long id) {
                          Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
                   }});
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }           
    }   
}

1 个答案:

答案 0 :(得分:8)

(new AdapterView.onItemClickListener() 

应该是

(new AdapterView.OnItemClickListener() 

有大写O