2011-08-02 27 views
1

我有问题与listview ...我试图添加OnClickListener但仍然无法正常工作。我想在点击后显示另一个活动。有人能帮助我吗?我知道,有很多例子,但它是我的申请不工作或我不知道如何在我的例子中使用它...ListView与customView和onClickItemListener

这是我LocationAdapter类:

public class LocationAdapter extends ArrayAdapter<LocationModel> { 

int resource; 
String response; 
Context context; 
private LayoutInflater mInflater; 

public LocationAdapter(Context context, int resource, List<LocationModel> objects) { 
    super(context, resource, objects); 
    this.resource = resource; 
    mInflater = LayoutInflater.from(context); 
} 

static class ViewHolder { 
    TextView titleGameName; 
    TextView distanceGame; 
     } 


public View getView(int position, View convertView, ViewGroup parent) 
{ 
    ViewHolder holder; 
    //Get the current location object 
    LocationModel lm = (LocationModel) getItem(position); 

    //Inflate the view 
    if(convertView==null) 
    { 
     convertView = mInflater.inflate(R.layout.item, null); 
     holder = new ViewHolder(); 
     holder.titleGameName = (TextView) convertView 
       .findViewById(R.id.it_location_title); 
     holder.distanceGame = (TextView) convertView 
       .findViewById(R.id.it_location_distance); 

     convertView.setTag(holder); 


    } else { 

     holder = (ViewHolder) convertView.getTag(); 

    } 

    holder.titleGameName.setText(lm.getGameName()); 
    holder.distanceGame.setText(lm.getGameDistance()+" km"); 

    return convertView; 
} 

}

这是我mainListView类:

public class SelectGameActivity extends Activity { 
LocationManager lm; 
GeoPoint userLocation; 

ArrayList<LocationModel> locationArray = null; 
LocationAdapter locationAdapter; 
LocationList list; 

ListView lv; 
TextView loadingText; 
TextView sprawdz; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.selectgame); 

    lv = (ListView) findViewById(R.id.list_nearme); 

    locationArray = new ArrayList<LocationModel>(); 
    locationAdapter = new LocationAdapter(SelectGameActivity.this, R.layout.item, locationArray); 


    lv.setTextFilterEnabled(true); 
    lv.setAdapter(locationAdapter); 
    lv.setItemsCanFocus(true); 





    String serverName = getResources().getString(R.string.serverAdress); 
    ApplicationController AC = (ApplicationController)getApplicationContext(); 
    String idPlayer = AC.getIdPlayer(); 
    int latitude = AC.getCurrentPositionLat(); 
    int longitude = AC.getCurrentPositionLon(); 
    int maxDistance = 99999999; 


    try { 
     new LocationSync().execute("myserverName"); 
    } catch(Exception e) {} 







} 


//this is connection with json 
private class LocationSync extends AsyncTask<String, Integer, LocationList> { 

    protected LocationList doInBackground(String... urls) { 
     LocationList list = null; 
     int count = urls.length; 

     for (int i = 0; i < count; i++) { 
      try {   
       // ntar diganti service 
       RestClient client = new RestClient(urls[i]); 

       try { 
        client.Execute(RequestMethod.GET); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

       String json = client.getResponse(); 

       list = new Gson().fromJson(json, LocationList.class); 

       // 
      } catch(Exception e) {} 
     } 
     return list; 
    } 

    protected void onProgressUpdate(Integer... progress) { 

    } 

    protected void onPostExecute(LocationList loclist) { 

     for(LocationModel lm : loclist.getLocations()) 
     { 
      locationArray.add(lm); 
     } 
     locationAdapter.notifyDataSetChanged(); 
    } 

} 

编辑::我都第二个问题......我想从项目获取ID(项目下载从JSON网址荷兰国际集团)这是我的名单:

enter image description here

我想例如:ID:159的第一个项目,并将其发送给nextActivity。

我也controllerClass.java在那里我设置和获取selectedIdGame:

public String getIdGameSelected() { 

    return idGame; 
} 

public void setIdGameSelected(String idGame) { 

    this.idGame = idGame; 
} 

是不是好主意?感谢帮助。

好的,完成了。我用:

public void onItemClick(AdapterView<?> a, View 
           v, int position, long id) { 


      String idGame = (String) ((TextView) v.findViewById(R.id.idGameSelected)).getText(); 

谢谢,米迦勒。

回答

2

你可以上(即在mainListView.java,只是lv.setAdapter后价格)的适配器实例定义onItemClick:

lv.setOnItemClickListener(new OnItemClickListener() { 
       public void onItemClick(AdapterView<?> a, View 
             v, int position, long id) { 
        Intent i = new Intent(v.getContext(), NextActivity.class); 
        startActivity(i); 
       } 
      }); 
+0

非常感谢!它运作良好! – Michal

1
lv.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 
      Intent i = new Intent(view.getContext(), NextActivity.class); 
      startActivity(i); 

     } 
    }); 

我不知道为什么这不起作用,把它放在try {} catch {}块之后。

+1

ARF,你上线打我......对矿井太糟糕了,一个同事来在这个瞬间聊天...;) – Renaud

+0

我喜欢,我们都称之为NextActivity.class。 :D – Rob