简体   繁体   中英

android custom listactivity arrayadapter

I'm trying to wrap my head around how to implement a custom arrayadapter to provide items for a list activity and then display that selected item in a new activity.

For example, i'm pulling a list of documents from a RESTful web service and i want to display these in a listactivity. My first call to the API will return JSON of documents with two fields: title, and id. I'd like to populate my listactivity with just the title of the documents in the UI. When i click on the item it, ideally, it will open a normal activity where it will make another API call to return the entire selected document in JSON format and display it into the new activity's UI.

After googling around, i've come up with what i think my needed steps are:

  1. Create a RecordListItem class that will only contain the title and id's of the list items.
  2. Create an arrayadapter of the RecordListItem type.
  3. Attach that arrayadapter to my listactivity.
  4. ???

I'm confused on the proper way to pass the id of the selected item to the normal activity so i can make the API call to pull that specific record. Do those steps make sense?

I'm used to web dev so this is a different way of thinking and i'm stuck. Can someone explain the correct steps or possibly point me to a tutorial that displays the selected item in a new activity?

In your ArrayAdapter constructor you pass the array with the title/id

public Docs_Array(Context context, Object[] docs) {
   super(context, R.layout.row, docs);
   this.context = context;
   this.docs = docs;
}

in the getView method you'll set each row's layout and information, the "position" argument is how you will get the document that you want. You can cast the Object[i] instance to another array if you want to have more than just one thing, like title+id.

public View getView(int position, View convertView, ViewGroup parent) {
   LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View rowView = inflater.inflate(R.layout.row, parent, false);
   TextView textView = (TextView)rowView.findViewById(R.id.row_title);
   textView.setText(((String[])docs[position])[0]); // 0 for title.
}

add an onClickListener to each row view, and ((String[])docs[position])[1] will give you the id for the document title that was clicked.

Without seeing any code, it sounds like you are on the right track. I would suggest maybe storing the id's in a HashMap using the title as the key and the id as the value . Then, when you select your item you can look up the . Then, when you select your item you can look up the id using the title and send it to your next Activity`. That is probably how I would handle it.

As far as tutorials, I would start with the Docs if you haven't already. Then if you have a specific problem I'm sure you can find similar issues people have had on SO and many more on the Google. Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM