Reputation: 21
package com.in2em.hotel;
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
@SuppressLint("ParserError")
Activity to display two arraylist in listview
public class OrderActivity extends ListActivity {
ArrayList<String> data,rate;
OrderAdapter orderAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
Bundle extras = getIntent().getExtras();
data = extras.getStringArrayList("list");
rate = extras.getStringArrayList("rate");
for(String str : data) {
Log.d("data", str);
}
for(String str1 : rate) {
Log.d("data", str1);
}
data and rate are two arraylist
String[] captionArray = (String[]) data.toArray(
new String[data.size()]);
String[] rateArray = (String[]) rate.toArray(
new String[rate.size()]);
R.layout.order_list,R.id.itemname, captionArray));
How to set the two array list in listview ? I can set only one arraylist ?
orderAdapter = new OrderAdapter(
OrderActivity.this, R.layout.order_list,
captionArray);
setting the list adapter
setListAdapter(orderAdapter);
return ;
}
Custum listAdapter
private class OrderAdapter extends BaseAdapter {
String[] items;
public OrderAdapter(Context context, int textViewResourceId,
String[] items) {
this.items = items;
}
Returning the views
public View getView(final int POSITION, View convertView,
ViewGroup parent) {
TextView tvNumber,tvName,tvRate;
EditText etQty,etRemarks;
View view = convertView;
Inflate
if (view == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.order_list, null);
}
tvName = (TextView) findViewById(R.id.itemname);
tvRate = (TextView) findViewById(R.id.itemrate);
tvName.setText(data.get(POSITION));
tvRate.setText(rate.get(POSITION));
return view;
}
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
}
Upvotes: 0
Views: 3103
Reputation: 2513
List<String> firstList = new ArrayList<String>();
List<String> secondList = new ArrayList<String>();
List<String> thirdList;
firstList.add("...");
secondList.add("...");
thirdList = new ArrayList<String>(firstList);
thirdList.addAll(secondList);
Apply thirdList
to your Adapter.
Upvotes: 0
Reputation: 86958
Change your constructor to accept two ArrayLists and store them within the Adapter:
private class OrderAdapter extends BaseAdapter {
ArrayList<String> data;
ArrayList<String> rate;
public OrderAdapter(Context context, ArrayList<String> data, ArrayList<String> rate) {
this.data = data;
this.rate = rate;
}
// Use the same getView(), for now
public int getCount() {
return data.size(); // rate must be the same size as data!
}
public String getItem(int position) {
return data.get(position);
}
And use it like this:
orderAdapter = new OrderAdapter(this, data, rate);
Also you should watch this Android lecture on adapters and ListViews to speed up getView()
.
Upvotes: 1