1

我有列表视图的问题...我正在尝试添加 OnClickListener 但仍然无法正常工作。我想在点击后显示另一个活动。有人可以帮助我吗?我知道有很多示例,但它不适用于我的应用程序,或者我不知道如何在我的示例中使用它...

这是我的 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 url 下载)这是我的列表:

在此处输入图像描述

例如,我想获取:第一项的 ID:159 并将其发送到 nextActivity。

我还有我正在设置和获取 selectedIdGame 的 controllerClass.java:

    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();

谢谢,迈克尔。

4

2 回答 2

2

您可以在您的适配器实例上定义一个 onItemClick(即在 mainListView.java 中,就在 lv.setAdapter 之后):

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);
                }
            });
于 2011-08-02T14:21:03.040 回答
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{} 块之后。

于 2011-08-02T14:16:39.250 回答