[android]找不到符号方法 setOnItemClickListener

标签: Android
发布时间: 2016/9/26 4:05:04
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

所以我现在有的列表返回 JSON 对象作为 ListView 上我 application ,我试图让它以便我可以单击该对象,然后编辑的详细信息,不过我有点不知为什么我 setOnItemClickListener 不工作,下面的代码

package mmu.tom.linkedviewproject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

private ListView GetAllDevicesListView;
private JSONArray jsonArray;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.GetAllDevicesListView = (ListView) this.findViewById(R.id.GetAllDevicesListView);

    new GetAllDevicesTask().execute(new ApiConnector());

    GetAllDevicesTask.setOnItemClickListener (new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){

        try

        {
            //GetDevice that was clicked
            JSONObject deviceClicked = jsonArray.getJSONObject(position);


            //Send DeviceId
            Intent showDetails = new Intent(getApplicationContext(), DeviceDetailsActivity.class);
            showDetails.putExtra("deviceID", deviceClicked.getString("deviceID"));
        }

        catch(
        JSONException e
        )

        {
            e.printStackTrace();
        }
    }
    });

}

public void setListAdapter(JSONArray jsonArray){

    this.jsonArray = jsonArray;
    this.GetAllDevicesListView.setAdapter((new GetAllDeviceListViewAdapter(jsonArray,this)));


}

private class GetAllDevicesTask extends AsyncTask<ApiConnector,Long,JSONArray>
{
    @Override
    protected JSONArray doInBackground(ApiConnector... params) {

        // it is executed on Background thread

         return params[0].GetAllDevicesState();
    }

    @Override
    protected void onPostExecute(JSONArray jsonArray) {

     setListAdapter(jsonArray);



    }


}

}

解决方法 1:

你不带列表 view 对象。

对此更改。

GetAllDevicesListView.setOnItemClickListener (new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){

    try

    {
        //GetDevice that was clicked
        JSONObject deviceClicked = jsonArray.getJSONObject(position);


        //Send DeviceId
        Intent showDetails = new Intent(getApplicationContext(), DeviceDetailsActivity.class);
        showDetails.putExtra("deviceID", deviceClicked.getString("deviceID"));
    }

    catch(
    JSONException e
    )

    {
        e.printStackTrace();
    }
}
});
赞助商