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

  • Home
  • SEARCH
  • 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 6665403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:43:11+00:00 2026-05-26T02:43:11+00:00

I am fairly new to Android development and I am trying to build a

  • 0

I am fairly new to Android development and I am trying to build a ListView which get data from web service using gson. I have a model class, a list class, an adapter class and the activity class.

The list works fine and it got the data, and now I want to integrate the OnItemClickListener to it and pass the data to the 2nd activity. And I’d like to get the item id (DistrictId) and pass it to the next Activity(listView) instead of the row id. It would be great if someone could show me the light… as the documentation is not as clear to understand and because I am new.

Below is my code.

The model class

package com.sample.myapp;

public class DistrictModel {
private String id;
private String districtName;

public String getDistrictId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getDistrictName(){
    return districtName;
}
public void setDistrictEN(String districtName){
    this.districtName = districtName;
}
}

The List class

public class DistrictList {
private List<DistrictModel> districts;

public List<DistrictModel> getDistricts(){
    return districts;
}
public void setDistrictList(List<DistrictModel> districts){
    this.districts = districts;
}
} 

The Adapter class

public class DistrictAdapter extends ArrayAdapter<DistrictModel>{
int resource;
String response;
Context context;
private LayoutInflater dInflater;

public DistrictAdapter(Context context, int resource, List<DistrictModel> objects) {
        super(context, resource, objects);
        this.resource = resource;
        dInflater = LayoutInflater.from(context);
}

static class ViewHolder {
    TextView title;
}
public View getView(int position, View convertView, ViewGroup parent)
{
    ViewHolder holder;
    //Get the current location object
    DistrictModel lm = (DistrictModel) getItem(position);

    //Inflate the view
    if(convertView==null)
    {
        convertView = dInflater.inflate(R.layout.item_district, null);
        holder = new ViewHolder();
        holder.title = (TextView) convertView
                .findViewById(R.id.district_name);

        convertView.setTag(holder);
    }
    else
    {
        holder = (ViewHolder) convertView.getTag();
    }

    holder.title.setText(lm.getDistrictName());

    return convertView;
}
}

The activity class

public class DistrictListActivity extends Activity{
LocationManager lm;

ArrayList<DistrictModel> districtArray = null;
DistrictAdapter districtAdapter;
DistrictList list;

ListView lv;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.districtlist_layout);

    lv = (ListView) findViewById(R.id.list_district);

    districtArray = new ArrayList<DistrictModel>();
    districtAdapter = new DistrictAdapter(DistrictListActivity.this, R.layout.item_district, districtArray);

    lv.setTextFilterEnabled(true);
            lv.setAdapter(districtAdapter);

            try {
                new DistrictSync().execute("http://aws.something.com/service");
            } catch(Exception e) {}

    lv.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View convertView, int position, long id) {
                AlertDialog.Builder adb=new AlertDialog.Builder(DistrictListActivity.this);
                adb.setTitle("LVSelectedItemExample");
                adb.setMessage("Selected Item is = "+(lv.getItemIdAtPosition(position)));
                adb.setPositiveButton("Ok", null);
                adb.show();

                }
        }); **//i'd like to get the DistrictId from the json data.**
}



private class DistrictSync extends AsyncTask<String, Integer, DistrictList> {

    protected DistrictList doInBackground(String... urls) {
        DistrictList list = null;
        int count = urls.length;

        for (int i = 0; i < count; i++) {
            try {           
                // ntar diganti service
                RestClient client = new RestClient(urls[i]);

                try {
                    client.Execute(RequestMethod.GET);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String json = client.getResponse();

                list = new Gson().fromJson(json, DistrictList.class);

                //
            } catch(Exception e) {}
        }
        return list;
    }

    protected void onProgressUpdate(Integer... progress) {

    }

    protected void onPostExecute(DistrictList dislist) {

        for(DistrictModel lm : dislist.getDistricts())
        {
            districtArray.add(lm);
        }
        districtAdapter.notifyDataSetChanged();
    }

}
}

For testing purpose, now I click the row it will show me the row id, so I know the onclick listener works, but I just want it to grab me the DistrictId so I can use it to pass to the next activity.

Thank you so much.

  • 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-05-26T02:43:11+00:00Added an answer on May 26, 2026 at 2:43 am

    (out of my head) Try this:

    ((DistrictModel)lv.getAdapter().getItem(position)).getDistrictId();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im fairly new to android development, but i'm having some trouble trying to make
I'm fairly new to the Android platform and was wondering if I could get
Im fairly new to android dev and I want to build an app that
I'm fairly new to eclipse and android development, and I'm having what must be
Another question about my implementation. I am fairly new to java/android coming from a
How can I implement the fairly new Voice Actions from Google, in Android? I'm
How can I implement the fairly new Voice Actions from Google, in Android? I'm
I'm fairly new to Android, but have done lots of Java/JSP development and HTML
So I'm fairly new to Android Development and Programming in general... I have a
I am fairly new to Android development, and I would like to make 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.