How to populate one ListView from multiple sources?
I've a ListView. Each ListItem has three TextViews in it.
<LinearLayout android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/linearLayout2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="center">
<ImageView android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/imageView1"
android:src=开发者_开发问答"@drawable/icon" android:padding="5dip">
</ImageView>
<TextView android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/textView1"
android:padding="5dip"
android:text="First">
</TextView>
<LinearLayout android:id="@+id/LinearLayout01"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_weight="1">
<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Text View" android:padding="5dip">
</TextView>
<TextView android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third Text View"
android:padding="5dip">
</TextView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Now, I had three ArrayList objects. Suppose those three have been loaded like. I had a parser for this already which makes three array lists from a single array list!
["Main One","Main Two","Main Three","Main Four"]
["Top One","Top Two","Top Three","Top Four"]
["Bottom One","Bottom Two","Bottom Three","Bottom Four"]
I want to load these ArrayLists into the List view. I had made a mockup of the expected output.
Is there any solution for this? I've tried to set ListAdapter on the same ListView one after another, but doesn't seems work at all. Thanks.
Try to add Values to class and access it.Here is some snippets
ContractTestActivity.java :
public class ContractTestActivity extends Activity {
private List<Contract> contracts = new ArrayList<Contract>();
public final String TAG = "ContractTest";
//public Contract newContract = new Contract();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list;
list = (ListView)findViewById(R.id.mylist);
ArrayAdapter<Contract> adapter = new ContractAdapter(this, android.R.layout.simple_list_item_1, myContracts());
list.setAdapter(adapter);
}
private List<Contract> myContracts(){
List<Contract> list = new ArrayList<Contract>();
list.add(new Contract("Friend1"));
return list;
}
}
Contract.java
public class Contract {
private String name;
public Contract(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
ContractAdapter.java
public class ContractAdapter extends ArrayAdapter<Contract> {
private List<Contract> contracts;
public ContractAdapter(Context context, int view, List<Contract> passedContracts) {
super(context, view, passedContracts);
contracts = passedContracts;
}
@Override
public int getCount() {
return contracts.size();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View currentView = convertView;
LayoutInflater currentViewInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
currentView = currentViewInflater.inflate(R.layout.deal_list, null);
Contract currentContract = contracts.get(position);
TextView text = (TextView) currentView.findViewById(R.id.text1);
text.setText(currentContract.getName());
return currentView;
}
}
For Inflated Layout deal_list.xml :
Create layout with Text View with Id as text1.
You should create a custom adapter by extending the baseadapter.
And I would create a data structure class
public class Data {
String LeftMainString;
String RightTopString;
String RightBottomString;
}
Then I would create a arraylist of the data object like
ArrayList<Data> datas= new ArrayList<Data>();
And pass the datas to the custom adapter to create the list. There are many tutorials out there for custom adapter.
So the combining of the 3 ArrayLists goes wrong if I understand you correctly?
Maybe this can help you
https://github.com/commonsguy/cwac-merge
And the Hello ListView tutorial on the Android Developers site http://developer.android.com/resources/tutorials/views/hello-listview.html
You can achieve this behaviour using BaseAdapter.
You only need to handle getItem(), getCount() etc. methods by yourself to properly determining the right source (list or whatever) when fetching your item for the given position (from the correct resource).
On the other hand, you can create one meta-class having the needed data from the both data sources and passing the instances of that class to the adapter.
精彩评论