I want my ListView
to hold View
as each item, thus I wrote my own ArrayAdapter
. That's to say, my ListView
is a list of View
now rather than TextView
.
But now, how can I manage the items in the ListView
? For example, if I want to change the background color of the view, it doesn't work if I put setBackgroundColor(Color.BLACK)
inside the constructor of the View
.
This is my custom ArrayAdapter code:
public class DataViewAdapter extends ArrayAdapter<DataView>{
Context context;
int layoutResourceId;
ArrayList<DataView> views;
public DataViewAdapter(Context context, int layoutResourceId,
ArrayList<DataView> views) {
super(context, layoutResourceId, views);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.views = views;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewHolder holder = null;
if(row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.view = (View)row.findViewById(R.id.dataView);
row.setTag(holder);
} else {
holder = (ViewHolder)row.getTag();
}
DataView dataView = views.get(position);
holder.view = dataView;
return row;
}
static class ViewHolder{
public View view;
}
}
Also, what if I need to pass different data values into each View
? On accepting the new data values, the onDraw(Canvas canvas)
method inside my DataView
needs to be triggered.
EDIT: codes for DataView
public class DataView extends View{
private LinkedList<Integer> data;
public DataView(Context context){
super(context);
data = new LinkedList<Integer>();
}
public void onDraw(Canvas canvas){
// I need to plot according to data
}
public void pushData(int value){
data.add(value);
this.invalidate();
}
}