【问题标题】:ListView with customView and onClickItemListenerListView 与自定义 View 和 onClick ItemListener
【发布时间】:2011-08-02 14:11:30
【问题描述】:

我的列表视图有问题...我正在尝试添加 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。

我还有 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();

谢谢,米哈尔。

【问题讨论】:

    标签: java android listview android-custom-view


    【解决方案1】:

    您可以在您的适配器实例上定义一个 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);
                    }
                });
    

    【讨论】:

      【解决方案2】:
          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{} 块之后。

      【讨论】:

      • arf,你在线上打败了我……可惜我的一个同事正在这个时候来闲聊……;)
      • 我喜欢我们都称它为 NextActivity.class。 :D
      猜你喜欢
      • 1970-01-01
      • 2019-06-14
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多