2016-01-29 76 views
-1

我在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监听器上执行他的任务列表视图。感谢您提前帮助。

+0

想要发送哪个字符串值? –

+0

row_userid从textView –

回答

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"); 
} 
+0

但如何设置字符串意味着它将如何从textview中获得价值 –

+0

@HoneyKamboj:请参阅我的更新。 – Rohit5k2

+0

错误@ i.putExtra(“userid”,userId); –

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"); 
+0

不工作给予例外 –

+0

什么异常? – Msp

+0

红色下划线listView.setOnItemClickListener(新AdapterView.OnItemClickListener(){ @覆盖 公共无效onItemClick(适配器视图父,观景,INT位置,长的id){ 意向书I =新意图(getActivity(),PostView.class); TextView的用户id =(TextView的)view.findViewById(R.id.row_userid); i.putExtra( “row_userid”,userId.getText()的toString()); startActivity(ⅰ); } }); –

0

只需在xml布局文件的ImageButton中添加android:focusable =“false”即可。这之后你的ListView点击听者将工作

+0

不工作没有任何反应 –

0

当您使用 userId = (TextView)view.findViewById(R.id.row_userid); 您正在使用view内李斯特,而你应该使用this.view