gpt4 book ai didi

java - 具有自定义 View 和 onClick ItemListener 的 ListView

转载 作者:行者123 更新时间:2023-11-29 06:10:53 25 4
gpt4 key购买 nike

我的 ListView 有问题...我正在尝试添加 OnClickListener,但仍然无法正常工作。我想在点击后显示另一个 Activity 。有人可以帮我吗?我知道有很多例子,但它对我的应用程序不起作用,或者我不知道如何在我的例子中使用它......

这是我的 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();
}

}

EDIT:: 我有第二个问题...我想从项目中获取 ID(项目正在从 json url 下载)这是我的列表:

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

谢谢,米甲。

最佳答案

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

关于java - 具有自定义 View 和 onClick ItemListener 的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6913458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com