Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:29:11+00:00 2026-06-14T18:29:11+00:00

I’m trying to implement a simple android REST Client and i having some problems

  • 0

I’m trying to implement a simple android REST Client and i having some problems understanding how to pass data between my activities.

I have this ListActivity (I’m using the Spring REST Template) :

    public class MainActivity extends ListActivity
{
    protected static final String TAG = MainActivity.class.getSimpleName();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(this, "You have selected" + position + id ,
                Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {

        super.onStart();
        new DownloadClientesTask().execute();
    }

    private void refreshClientes(List<Cliente> clientes) {
        if (clientes == null) {
            return;
        }

        ClientesListAdapter adapter = new ClientesListAdapter(this, clientes);
        setListAdapter(adapter);
    }

    private class DownloadClientesTask extends AsyncTask<Void, Void, List<Cliente>> {

        @Override
        protected List<Cliente> doInBackground(Void... params) {
            final String url = "http://192.168.1.119/~henry/api_slim/index.php/customers";

            try {
                // Set the Accept header for "application/json"
                HttpHeaders requestHeaders = new HttpHeaders();
                List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
                acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
                requestHeaders.setAccept(acceptableMediaTypes);

                // Populate the headers in an HttpEntity object to use for the request
                HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

                // Create a new RestTemplate instance
                RestTemplate restTemplate = new RestTemplate();
                restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter());

                // Perform the HTTP GET request
                ResponseEntity<Cliente[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity,
                        Cliente[].class);

                // convert the array to a list and return it
                return Arrays.asList(responseEntity.getBody());
            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.e(TAG, e.getMessage(), e);
            }

            return null;

        }

        @Override
        protected void onPostExecute(List<Cliente> result) {

            refreshClientes(result);
        }

    } 

}

And this is My listAdapter :

public class ClientesListAdapter extends BaseAdapter{

    private List<Cliente> clientes;
    private final LayoutInflater layoutInflater;

    public ClientesListAdapter(Context context, List<Cliente> clientes) {
        this.clientes = clientes;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return this.clientes != null ? clientes.size() : 0;
    }

    @Override
    public Cliente getItem(int position) {
        return this.clientes.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = this.layoutInflater.inflate(R.layout.cliente_list_item, parent, false);
        }

        Cliente cliente = getItem(position);
        if (cliente != null) {
            TextView t = (TextView) convertView.findViewById(R.id.name);
            t.setText(cliente.getFirstname());
        }

        return convertView;
    }

}

This the POJO class of the data iḿ getting :

public class Cliente {
    private Integer id_customer; 
        private String firstname;
        public Integer getId_customer() {
        return id_customer;
    }
    public void setId_customer(Integer id_customer) {
        this.id_customer = id_customer;
    }
        public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
}

When i select an element from the listView i would like show details specific about this element on another activity or fragment, but i don’t know how to obtain the customer_id of this element from the list, do i have to save it when i procesing the response? do I need to use content provider or database provide this behavior? i’m really confused, thanks in advance for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:29:12+00:00Added an answer on June 14, 2026 at 6:29 pm

    There are good examples on how to pass data from one activity to another here, pass objects between activities. You may want to take a look first to the solutions on those links.

    Please see below an example that can put you on the right track.

    List adapter class:

    public class ClientesListAdapter extends BaseAdapter{
    
        //private members
        private List<Cliente> clientes;
    
        //adapter position - not used for this example
        public int adapterPosition;
    
        //context of app
        private Context mContext;
    
        //default constructor
        public ClientesListAdapter(Context context, List<Cliente> clientes) {
    
            //context pointer
            this.mContext = context;
    
            //alloc
            this.clientes = new ArrayList<Cliente>(clientes.size());
            this.clientes.addAll(clients);
        }
    
        //Holder for events and dates (memory management)
        public static class ViewHolder{
            TextView myTextView;//this is actually findViewById(R.id.name) @see getView() method
        }
    
        //generated method
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return this.clientes != null ? clientes.size() : 0;
        }
    
        //generated method
        @Override
        public Cliente getItem(int position) {
            return this.clientes.get(position);
        }
    
        //generated method
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }
    
        //get client's id
        public int getClienteId(int position){
            return this.clientes.get(position).getClienteId();
        }
    
        //get client's id without passing the position
        public int getClienteId(){
            return this.clientes.get(adapterPosition).getClienteId();
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            //row is actually convertView (the current view)
            View row = convertView;
            //holds our view elements
            ViewHolder holder;
    
            //if row is null 
            if(row == null){
                //inflate layout to get our view elements
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = inflater.inflate(com.yourapp.R.layout.my_layout, parent, false);//your layout here, modify code
                //set up the holder
                holder = new ViewHolder();
                holder.myTextView = (TextView) row.findViewById(com.yourapp.R.id.name);
    
                //give the row a tag (holder)
                row.setTag(holder);
    
            }else{
                //row is not null we can see it (no need to allocate memory)
                holder = (ViewHolder) row.getTag();
            }
    
            //get your cliente object
            Cliente cliente = this.clientes.get(position);
            if (cliente != null) {
                holder.myTextView.setText(cliente.getFirstname());
            }
    
            //copy position
            adapterPostion = position;
    
            return convertView;
        }
    
    }
    

    You see that we used a ViewHolder class for memory management. This is a good practice for holding view elements inside your list adapter. You can find more info about list views, explained by Romain Guy – The World of ListViews.

    From your MainActivity allocate the adapter and get your item on click:

    //---- code --- //
    ListView myListView = (ListView)findViewById(R.id.mylistview);//or you may use ListActivity
    ClientesListAdapter adapter = new ClientesListAdapter(this, clientes);//"this" or "getApplicationContext()"
    myListView.setAdapter(adapter);
    adapter.notifyDataSetChanged();//notify
    // ---- code --- //
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(this, "You have selected" + position + id ,
                Toast.LENGTH_SHORT).show();
    
        Intent intent = new Intent(MyActivity.this, ActivityB.class);
        intent.putInt("cliente_id",adapter.getClienteId());
        startActivity(intent);
    }
    

    Another example is with implementing an interface in the adapter like this:

    //--code//
    //Interface method
    private OnSaveEditsListener saveEditsListener = null;
    public void setOnSaveEditsListener(OnSaveEditsListener l) {
        saveEditsListener = l;
    }
    //--code//
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            //--code--//
    
            //get clicked position of calendar (get clicked day)
            convertView.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    v.requestFocusFromTouch();
                    currentAgendaPosition = position;
                    try{
                        saveEditsListener.onSaveEdits();
                    }catch(Exception ex){
                        ex.printStackTrace();
                    }
                }
            });
    
            //returns current row
            return row;
        }
        //--code--//
    

    And from your MainActivity start the second activity like this:

    adapter.setOnSaveEditsListener(new OnSaveEditsListener() {
    
        @Override
        public void onSaveEdits() {
            //Start activity from here
            //--code--//
            startActivity(intent);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.