我在Fragment Home中有一个列表视图,我想当有人点击列表项时,他们将打开新活动,数据将传递给新活动并设置为文本视图。但是,当我点击列表项
时,什么也没发生这是我的完整代码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);
}
}
}
现在只需要在listview上点击监听器来执行他的任务。提前谢谢你的帮助。
答案 0 :(得分:0)
像这样发送数据
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(), PostView.class);
TextView userId = (TextView) view.findViewById(R.id.row_userid);
i.putExtra("userid", userId.getText().toString());
startActivity(i);
}
在PostView
Intent intent = getIntent();
if (intent!=null) {
String stringData= intent.getStringExtra("userid");
}
答案 1 :(得分:0)
用它来发送数据,
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent i = new Intent(getActivity(), PostView.class);
TextView userId = (TextView) view.findViewById(R.id.row_userid);
intent.putExtra("row_userid", userId.getText().toString());
startActivity(i);
}
并像这样检索它,
String row_userid = getIntent().getStringExtra("row_userid");
答案 2 :(得分:0)
只需添加android:focusable =&#34; false&#34;在xml布局文件中的ImageButton中。之后,ListView单击列表器将起作用
答案 3 :(得分:0)
使用时
userId = (TextView)view.findViewById(R.id.row_userid);
您正在使用view
内部列表,而应使用this.view
。