Reputation: 231
So, I'm still a bit new to Android and tried to find some help online but I can't seem to find a clear solution. I'm creating an app that consists of only a ListView. I created an xml file that consists of 2 textviews (it's basically the same as the built-in android.R.layout.simple_list_item_2 layout but I'm guessing I may need to change this). It looks like this:
TextView1
TextView2
Nothing fancy. The problem I'm coming across is working with arrays. For my app, TextView1 will simply be a set String. Textview2, though, will technically not just be a single TextView. I'd like to take an array, create the necessary number of TextViews, and place them in each row of the ListView under TextView1.
For example, if I have these 3 arrays:
String[] array1 = {TextView1A, TextView1B, TextView1C, TextView1D};
String[] array2 = {TextView2A, TextView2B};
String[] array3 = {TextView3A, TextView3B, TextView3C};
I'd like the output to look something like this:
TextView1
TextView1A
TextView1B
TextView1C
TextView1D
TextView2
TextView2A
TextView2B
TextView3
TextView3A
TextView3B
TextView3C
Like I said, I'm still a bit new to teaching myself android. I've done simple ListViews through ArrayAdapters but I'm not sure if that would work here. I'd like to create this with more arrays that can each have a different number of elements so I really don't think simply creating a ton of custom ListView rows would be an option. I really have no idea how to even begin implementing this so any push in the right direction would be appreciated. Any other suggestions on how to do this are also welcome. Thank you.
Upvotes: 0
Views: 143
Reputation: 1000
I think you will have a better implementation if you think of each row in that ListView
as an owner of its data. For example, create a class MyObject that has all the attributes you need a row to have. In my custom adapter below MyObject can have four different String
, but it could be anything.
And then you implement a custom adapter that you attach to your ListView
:
public class MyCustomAdapter extends ArrayAdapter<MyObject> {
Context context;
public MyCustomAdapter(Context context, int resourceId, List<MyObject> items) {
super(context, resourceId, items);
this.context = context;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
MyObject myObj = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.my_listview_item, null);
holder = new ViewHolder();
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
String one = myObj.getString1();
String two = myObj.getString2();
String three = myObj.getString3();
String four = myObj.getString4();
// Depending if any of those strings are empty/null set textView1 and textView2 appropriatelly
return convertView;
}
/* private view holder class */
private class ViewHolder {
TextView textView1;
TextView textView2;
}
}
Upvotes: 0
Reputation: 1081
So you just want to combine all the arrays into a single list?
It looks to me like you could still accomplish this with a single ArrayAdapter. You'd need an extra array as the ArrayAdapter's data source that contains all of your arrays concatenated together, and you'd just manually keep it in sync.
If that setup's not going to work for you, you could also take a look at MergeAdapter That wraps an arbitrary number of adapters of arbitrary types and combines them all into a single list.
Upvotes: 0