我正在玩我在网上找到的一段代码,我正在尝试将一个点击监听器添加到列表视图中并获取该项目的位置。但是由于某种原因,这个位置总是空的。这是我的代码:
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.v("position", position); //POSITION IS ALWAYS EMPTY
Intent i = new Intent(Example.this, ShowReleases.class);
i.putExtra("id", position);
startActivityForResult(i, ACTIVITY_CREATE);
}
});
我还添加了完整代码以防万一:
package com.example;
import java.util.ArrayList;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class Example extends Activity {
private static final int ACTIVITY_CREATE = 0;
public Bitmap placeholder;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<Tweet> tweets = getTweets("android", 1);
ListView listView = (ListView) findViewById(R.id.ListViewId);
listView.setAdapter(new TweetItemAdapter(this, R.layout.listitem, tweets));
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.v("position", position); //POSITION IS ALWAYS EMPTY
Intent i = new Intent(Example.this, ShowReleases.class);
i.putExtra("id", position);
startActivityForResult(i, ACTIVITY_CREATE);
}
});
}
public ArrayList<Tweet> getTweets(String searchTerm, int page) {
String searchUrl = "http://search.twitter.com/search.json?q=@" + searchTerm;
Log.v("searchUrl", searchUrl);
ArrayList<Tweet> tweets = new ArrayList<Tweet>();
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(searchUrl);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = null;
try{
responseBody = client.execute(get, responseHandler);
}catch(Exception ex) {
ex.printStackTrace();
}
JSONObject jsonObject = null;
JSONParser parser=new JSONParser();
try {
Object obj = parser.parse(responseBody);
jsonObject=(JSONObject)obj;
}catch(Exception ex){
Log.v("TEST","Exception1: " + ex.getMessage());
}
JSONArray arr = null;
try {
Object j = jsonObject.get("results");
arr = (JSONArray)j;
}catch(Exception ex){
Toast.makeText(this, "JSON Error", Toast.LENGTH_LONG);
Log.v("TEST","Exception2: " + ex.getMessage());
}
for(Object t : arr) {
Tweet tweet = new Tweet(
((JSONObject)t).get("from_user").toString(),
((JSONObject)t).get("text").toString(),
((JSONObject)t).get("profile_image_url").toString(),
((JSONObject)t).get("id").toString()
);
tweets.add(tweet);
}
return tweets;
}
/** Classes **/
public class Tweet {
public String username;
public String message;
public String image_url;
public String post_id;
public Boolean usernameSet = false;
public Boolean messageSet = false;
public Boolean imageSet = false;
public Bitmap avatar;
public Tweet(String username, String message, String url, String post_id) {
this.username = username;
this.message = message;
this.image_url = url;
this.post_id = post_id;
}
}
}
提前谢谢
罗伯特
答案 0 :(得分:0)
首先检查列表大小,我在我的应用程序中使用了相同的东西,它的工作正常。 在第一次行动中我像这样通过了
in.putExtra(id, position);
在第二个活动中我写这样来获取数据
Bundle extras = getIntent().getExtras();
arg=extras.getInt(id);
System.out.println("Position:"+arg);
答案 1 :(得分:0)
你可以看到构造函数AdapterView(Context context),所以使用get application context。 或者访问此链接
http://developer.android.com/reference/android/widget/AdapterView.html
希望这会对你有所帮助
答案 2 :(得分:0)
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Log.v("position", String.valueOf(position));
Intent i = new Intent(Example.this, ShowReleases.class);
i.putExtra("id", String.valueOf(position));
startActivityForResult(i, ACTIVITY_CREATE);
}
});