Search code examples
androidjsonlistviewandroid-arrayadapter

Get Title and Description when Title item is clicked on Listview android


I use JSON to get data items Title and Description from a web server. I know how to parse it, use a custom Adapter and populate a listview with the Title only.. I want it to open a new activity and display the Title and Description of the clicked Item. Do I have to create a new custom adapter to populate this listview? What should I pass from this side of the activity and how do I receive it on the other side? Thanks in advance.

 displayTitleAdapter = new DisplayTitleAdapter(this, R.layout.rowlayout);
    listView = (ListView)findViewById(R.id.listviewfinal);
    listView.setAdapter(displayTitleAdapter);
    listView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });  

This is my Adapter class' getView method

public View getView(int position,  View convertView,  ViewGroup parent) {
    View row;
    row = convertView;
    ReqHolder reqHolder;
    if(row == null){
        LayoutInflater layoutInflate = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflate.inflate(R.layout.rowlayout, parent, false);
        reqHolder = new ReqHolder();

        reqHolder.Title = (TextView)row.findViewById(R.id.title_title);
        reqHolder.Requirement = (TextView)row.findViewById(R.id.req_req);

        row.setTag(reqHolder);
    }
    else{
        reqHolder = (ReqHolder)row.getTag();
    }
    //Requirements is the model class with getter and setter methods for Fields Title and Requirement.
    Requirements requirements = (Requirements) this.getItem(position);
    reqHolder.Title.setText(requirements.getTitle());
    reqHolder.Requirement.setText(requirements.getRequirement());
    return row;
}

static class ReqHolder{
    TextView Title;
     TextView Requirement;
}

}


Solution

  • that's works good :

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Object o = listView.getItemAtPosition(position);
                    UserConnected str=(UserConnected) o;//As you are using Default String Adapter
                    Toast.makeText(getBaseContext(),str.getPrenom(), Toast.LENGTH_SHORT).show();
                }
            });