我在Fragment Home中有一个listview,当某人点击列表项目时,他们会打开新的活动,数据将传递给新的活动并设置为文本视图。但是,当我在列表项ListView.setOnItemClickListener在点击列表项目时不起作用
单击什么也没有发生,这里是我的onItemClickListner
public class Home extends Fragment {
ListView listView;
PostAdapter adapter;
TextView userId;
ArrayList<Post> postArrayList;
//private SwipeRefreshLayout swipeRefreshLayout;
DBHandler handler;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_post, container, false);
listView = (ListView) view.findViewById(R.id.listview);
ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null && networkInfo.isConnected()){
new DataFetcherTask().execute();
}else{
Toast.makeText(getActivity(), "Internet Error", Toast.LENGTH_LONG).show();
}
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
userId = (TextView)view.findViewById(R.id.row_userid);
Intent i = new Intent(getActivity(), PostView.class);
i.putExtra("userid", userId.getText().toString());
startActivity(i);
Toast.makeText(getActivity(),"hello",Toast.LENGTH_LONG).show();
}
});
return view;
}
private class DataFetcherTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... args) {
String serverData = null;// String object to store fetched data from server
// Http Request Code start
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://xyz.lt/json.php");
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
serverData = EntityUtils.toString(httpEntity);
Log.d("response", serverData);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Http Request Code end
// Json Parsing Code Start
try {
postArrayList = new ArrayList<Post>();
JSONObject jsonObject = new JSONObject(serverData);
JSONArray jsonArray = jsonObject.getJSONArray("post");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObjectPOST = jsonArray.getJSONObject(i);
String post_id = jsonObjectPOST.getString("post_id");
String user_id = jsonObjectPOST.getString("user_id");
String user_name = jsonObjectPOST.getString("user_name");
String post_title = jsonObjectPOST.getString("post_title");
String likes = jsonObjectPOST.getString("likes");
String post_cat = jsonObjectPOST.getString("post_cat");
String post_time = jsonObjectPOST.getString("post_time");
Post post = new Post();
post.setPost_id(post_id);
post.setUser_id(user_id);
post.setUser_name(user_name);
post.setPost_title(post_title);
post.setLikes(likes);
post.setPost_cat(post_cat);
post.setPost_time(post_time);
handler.addPost(post);// Inserting into DB
}
} catch (JSONException e) {
e.printStackTrace();
}
//Json Parsing code end
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
ArrayList<Post> postList = handler.getAllPost();
adapter = new PostAdapter(getActivity(), postList);
listView.setAdapter(adapter);
}
}
@Override
public void onResume() {
super.onResume();
new DataFetcherTask().execute();
ArrayList<Post> postList = handler.getAllPost();
adapter = new PostAdapter(getActivity(), postList);
listView.setAdapter(adapter);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new DBHandler(getActivity());
new DataFetcherTask().execute();
}
}
我用图片+的TextView +图像按钮做了自定义列表项的完整代码。我只需要post_id,然后所有的数据将使用该文本从sqlite中获取。 Textview在PostAdapter中初始化,其中数据连接到列表视图。
点击后,该意向将打开
public class PostView extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_view);
TextView textView = (TextView) findViewById(R.id.textView3);
Intent intent = getIntent();
if (intent!=null) {
String stringData= intent.getStringExtra("userid");
textView.setText(stringData);
}
}
}
现在只需要点击steup监听器上执行他的任务列表视图。感谢您提前帮助。
想要发送哪个字符串值? –
row_userid从textView –