2016-02-12 57 views
0

所以我目前在我的应用程序中有一个返回的JSON对象的列表,作为ListView,我试图让它可以点击对象然后编辑细节,但是我在一个小的损失的,为什么我的setOnItemClickListener不能正常工作,下面的代码找不到符号方法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); 



    } 


} 

}

+0

它应该是getAllDevicesListView(变量不以大写字母开头!)。是的,你必须在ListView上设置itemClickListener,而不是GetAllDevicesTask的类名。 –

回答

0

你不采取列表视图对象。

更改为此。

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(); 
    } 
} 
}); 
+0

这很完美!谢谢!我正在关注一个在线指南,所以我几乎复制了代码,但我一定错过了一些东西! – Tfish

+0

@TomFisher欢迎您的朋友! –

0

你是不是在ListView但在一个类名设置setOnItemClickListener

变化

GetAllDevicesTask.setOnItemClickListener (new AdapterView.OnItemClickListener() { 

GetAllDevicesListView.setOnItemClickListener (new AdapterView.OnItemClickListener() {