Tuesday, January 28, 2014

In android listview, how can I display different types of item,


posts by cunlingwang edited 2013-01-22 17:42:58
Shaped like the phone's design interface, seekbar or button, I used the three forms, but do not know how to make the display in onCreated wrote, basically FC.



Here is rewritten BaseAdapter

 

class MyAdapter extends BaseAdapter {
Context mContext;
/ / LinearLayout linearLayout = null;
LayoutInflater inflater;
/ / TextView tex;

final int TYPE_SWITCH = 0;
final int TYPE_SEEKBAR = 1;
final int TYPE_BUTTON = 2;
final int TYPE_CHECKBOX = 3;

public MyAdapter (Context context) {

mContext = context;
inflater = LayoutInflater.from (mContext);
/ / Inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
}

@ Override
public int getCount () {
/ / TODO Auto-generated method stub
return mData.size ();}

/ / Each convert view will call this method to get the current view style
needed@ Override
public int getItemViewType (int position) {
/ / TODO Auto-generated method stub
int p = position% 4;
if (p == 0)
return TYPE_SWITCH;
else if (p == 1)
return TYPE_SEEKBAR;
else if (p == 4)
return TYPE_SEEKBAR_RGB;
else if (p == 2)
return TYPE_BUTTON;
else if (p == 3)
return TYPE_CHECKBOX;
else
return TYPE_SEEKBAR;
}

@ Override
public int getViewTypeCount () {
/ / TODO Auto-generated method stub
return 1;
}

@ Override
public Object getItem (int arg0) {
/ / TODO Auto-generated method stub
return mData.get (arg0);
}

@ Override
public long getItemId (int position) {
/ / TODO Auto-generated method stub
return position;
}

@ Override
public View getView (int position, View convertView, ViewGroup parent) {
/ / TODO Auto-generated method stub

viewHolder_switch holder_switch = null;
viewHolder_seekbar holder_seekbar = null;
viewHolder_button holder_button = null;

int type = getItemViewType (position);

if (convertView == null)
{
Log.e ("convertView =", "NULL");

switch (type)
{
case TYPE_SWITCH:


holder_switch = new viewHolder_switch ();
convertView = inflater.inflate (R.layout.listview_switch_row, parent, false);
/ /
holder_switch.img = (ImageView) convertView.findViewById (R.id.img_sw);
holder_switch.title = (TextView) convertView.findViewById (R.id.title_sw);
holder_switch.info = (TextView) convertView.findViewById (R.id.info_sw);
Log.e ("convertView =", "NULL TYPE_SWITCH");

/ / Map myItem = getItem (position);

holder_switch.img.setBackgroundResource (R.drawable.power_ico_50dp);
holder_switch.title.setText (led_opt [position]);
holder_switch.info.setText (led_optContext [position]);
holder_switch.Switch.setChecked (true);

convertView.setTag (holder_switch);
break;
case TYPE_SEEKBAR:
convertView = inflater.inflate (R.layout.listview_seekbar_row, parent, false);

holder_seekbar = new viewHolder_seekbar ();
holder_seekbar.img = (ImageView) convertView.findViewById (R.id.img_sb);
holder_seekbar.title = (TextView) convertView.findViewById (R.id.title_sb);
holder_seekbar.info = (TextView) convertView.findViewById (R.id.info_sb);
Log.e ("convertView =", "NULL TYPE_SEEKBAR");
holder_seekbar.img.setBackgroundResource (R.drawable.rgb_ico_50dp);
holder_seekbar.title.setText (led_opt [position]);
holder_seekbar.info.setText (led_optContext [position]);

convertView.setTag (holder_seekbar);
break;
case TYPE_BUTTON:

convertView = inflater.inflate (R.layout.listview_button_row, parent, false);
holder_button = new viewHolder_button ();
holder_button.img = (ImageView) convertView.findViewById (R.id.img_btn);
holder_button.title = (TextView) convertView.findViewById (R.id.title_btn);
holder_button.info = (TextView) convertView.findViewById (R.id.info_btn);
Log.e ("convertView =", "NULL TYPE_BUTTON");

holder_button.img.setBackgroundResource (R.drawable.dream_ico_50dp);
holder_button.title.setText (led_opt [position]);
holder_button.info.setText (led_optContext [position]);

convertView.setTag (holder_button);
break;
}
} Else {
/ / There convertView, Belong, do not get the layout
switch (type)
{
case TYPE_SWITCH:
holder_switch = (viewHolder_switch) convertView.getTag ();
Log.e ("!!!!!! ConvertView =", "NULL TYPE_SWITCH");
break;
case TYPE_SEEKBAR:
holder_seekbar = (viewHolder_seekbar) convertView.getTag ();
Log.e ("!!!!!! ConvertView =", "NULL TYPE_SEEKBAR");
break;
case TYPE_BUTTON:
holder_button = (viewHolder_button) convertView.getTag ();
Log.e ("!!!!!! ConvertView =", "NULL TYPE_BUTTON");
break;
}
}
return convertView;
}
}

class viewHolder_switch {

/ ** Photo ID * /
private ImageView img;

/ ** Title * /
private TextView title;

/ ** Contents * /
private TextView info;
/ ** Switch * /
private Switch Switch;
}
class viewHolder_seekbar {

/ ** Photo ID * /
private ImageView img;

/ ** Title * /
private TextView title;

/ ** Contents * /
private TextView info;

private SeekBar sb;
}

class viewHolder_button
{
private ImageView img;

/ ** Title * /
private TextView title;

/ ** Contents * /
private TextView info;

/ ** Button name * /
private String btnText;
}





 

MyAdapter listAdapter;

protected void onCreate (Bundle savedInstanceState) {
/ / SetTheme (16974123);
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

{
ListView lv = (ListView) this.findViewById (R.id.listview_home);




listAdapter = new MyAdapter (this);
lv.setAdapter (listAdapter);
}
}




The question now is how to make this list contains three layouts and displays, do not know how to write a reference to the many, many of them are consistent layout for each item, this is not the same, I do not know how to start.
Reply:
holder set the content code was put out if else, can not be executed only if there

Reply:
cited a floor whq1982 reply:
holder set the content code was put out if else, can not be executed


Thank you. Above has been changed, but how to build an ArrayList <> What type of can put three different types are put into?
Please know who gets advice.
Reply:
getViewTypeCount If 3, 3 can be placed on different item.
At the same time this getView to distinguish between these three Item, respectively, to load different xml files.
Reply:
This is an example I

http://download.csdn.net/detail/piaohong/5020778
Reply:
reference to the third floor liusha0225 reply:
getViewTypeCount If 3, you can place three different item.
At the same time this getView to distinguish between these three Item, respectively, to load different xml files.


Yes, it is like this, how is now the main problem in the Activity display.
Reply:
references, 4th Floor piaohong reply:
me this is an example

http://download.csdn.net/detail/piaohong/5020778


Thank you, I refer to.
Reply:
references, 4th Floor piaohong reply:
me this is an example

http://download.csdn.net/detail/piaohong/5020778



Your example is feasible, but I think: we are generally used to write my above this practice, do not do this right? So it seems a little backward. Ha ha.
Just seen an example with perferenceActivity, the seekbar can not drag, it can not add the big picture on this page, so there is no use.
Reply:
ArrayList

class MyObject {
int resId;
......
......
}
Reply:
reference to the 7th floor cunlingwang reply:
references, 4th Floor piaohong reply: I've got this example

http://download.csdn.net/detail/piaohong/5020778


Your example is feasible, but I think: we are generally used to write my above this practice, do not do this right? So it seems a little backward. Ha ha.
Just seen an example with perferenceActivity, the seekbar can not drag, it can not add on this page ......


I told you that the wording of that example what is the difference?
Improvements to the viewHolder_switch viewHolder_seekbar so you kind of wording for each different list item must define a viewHolder. I engage in a ViewHolder fit all the different list items

Also added a class ListItem (encapsulates a Map and mType), as is to solve the "how to build a ArrayList <> What can these three types are put into different types of"


Reply:
reference to the second floor cunlingwang reply:
cited a floor whq1982 reply: holder set the content of the code was put out if else, can not be executed only if there

Thank you. Above has been changed, but how to build an ArrayList <> What type of can put three different types are put into?
Please know who gets advice.

With the parent class \ subclass be all right
Reply:
reference to the 9th floor piaohong reply:
reference to the 7th floor cunlingwang reply: Quote, 4th Floor piaohong reply: I've got this example

http://download.csdn.net/detail/piaohong/5020778


Your example is feasible, but I think: we are generally used to write my above this practice, do not do this right? So it seems a little backward. Ha ha.
Just seen an example with perferenceActivity ......


First of gratitude, your example, as you said, there can be adapted to a variety of types. Very good, in fact, the essence is the same. Reference to your example, the problem is now solved.

Reply:
Here is modified according to your views. No major changes.
Interface implemented as follows: Three types



 

public class MainActivity extends Activity {

private final int TYPE_SWITCH = 0;
private final int TYPE_SEEKBAR = 1;
private final int TYPE_BUTTON = 2;


private static final String [] led_opt = {
"Jiangsu", "Guangzhou", "Xinjiang" and "Taiwan"
};
private static final String [] led_optContext = {
"East China", "located in the southeast coast," "a distant place", "across the sea"
};

List > mData = new ArrayList > ();
MyAdapter listAdapter;


@ Override
protected void onCreate (Bundle savedInstanceState) {
/ / SetTheme (16974123);
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);

{
ListView lv = (ListView) this.findViewById (R.id.listview_home);

Map map;
/ *
* Power Switch
* /
map = new HashMap ();
map.put ("img", R.drawable.power_ico_50dp);
map.put ("title", led_opt [0]);
map.put ("info", led_optContext [0]);
mData.add (map);

map = new HashMap ();
map.put ("img", R.drawable.white_ico_50dp);
map.put ("title", led_opt [1]);
map.put ("info", led_optContext [1]);
mData.add (map);

map = new HashMap ();
map.put ("img", R.drawable.rgb_ico_50dp);
map.put ("title", led_opt [2]);
map.put ("info", led_optContext [2]);
mData.add (map);

map = new HashMap ();
map.put ("img", R.drawable.dream_ico_50dp);
map.put ("title", led_opt [3]);
map.put ("info", led_optContext [3]);
mData.add (map);

listAdapter = new MyAdapter (this);
lv.setAdapter (listAdapter);
}
}

........................
........................


 
............
............

/ **
* Customize a Adapter (implements ListAdapter Interface)
*
* @ Author Administrator
*
* /
class MyAdapter extends BaseAdapter {

LayoutInflater inflater = null;

public MyAdapter (Context context) {

this.inflater = LayoutInflater.from (context);
/ / Inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
}

@ Override
public int getCount () {
/ / TODO Auto-generated method stub
return mData.size ();}

/ / Each convert view will call this method to get the current view style
needed@ Override
public int getItemViewType (int position) {
/ / TODO Auto-generated method stub
int p = position% 4;
if (p == 0)
return TYPE_SWITCH;
else if (p == 1)
return TYPE_SEEKBAR;
else if (p == 2)
return TYPE_SEEKBAR;
else if (p == 3)
return TYPE_BUTTON;
else
return 0;
}

@ Override
public int getViewTypeCount () {
/ / TODO Auto-generated method stub
return 3;
}

@ Override
public Object getItem (int position) {
/ / TODO Auto-generated method stub
/ / Return mData.get (arg0);
return position;
}

@ Override
public long getItemId (int position) {
/ / TODO Auto-generated method stub
return position;
}

@ Override
public View getView (int position, View convertView, ViewGroup parent) {
/ / TODO Auto-generated method stub

viewHolder_switch holder_switch = null;
viewHolder_seekbar holder_seekbar = null;
viewHolder_button holder_button = null;

int type = getItemViewType (position);

if (convertView == null)
{
Log.e ("convertView =", "NULL");

switch (type)
{
case TYPE_SWITCH:


holder_switch = new viewHolder_switch ();
convertView = inflater.inflate (R.layout.listview_switch_row, parent, false);
/ / ConvertView = inflater.inflate (R.layout.listview_switch_row, null);
/ /
holder_switch.img = (ImageView) convertView.findViewById (R.id.img_sw);
holder_switch.title = (TextView) convertView.findViewById (R.id.title_sw);
holder_switch.info = (TextView) convertView.findViewById (R.id.info_sw);
Log.e ("convertView =", "NULL TYPE_SWITCH");

convertView.setTag (holder_switch);


break;
case TYPE_SEEKBAR:


convertView = inflater.inflate (R.layout.listview_seekbar_row, parent, false);

holder_seekbar = new viewHolder_seekbar ();
holder_seekbar.img = (ImageView) convertView.findViewById (R.id.img_sb);
holder_seekbar.title = (TextView) convertView.findViewById (R.id.title_sb);
holder_seekbar.info = (TextView) convertView.findViewById (R.id.info_sb);
Log.e ("convertView =", "NULL TYPE_SEEKBAR");


convertView.setTag (holder_seekbar);
break;
case TYPE_BUTTON:

convertView = inflater.inflate (R.layout.listview_button_row, parent, false);
holder_button = new viewHolder_button ();
holder_button.img = (ImageView) convertView.findViewById (R.id.img_btn);
holder_button.title = (TextView) convertView.findViewById (R.id.title_btn);
holder_button.info = (TextView) convertView.findViewById (R.id.info_btn);
Log.e ("convertView =", "NULL TYPE_BUTTON");



convertView.setTag (holder_button);
break;
}
} Else {
/ / There convertView, Belong, do not get the layout
switch (type)
{
case TYPE_SWITCH:
holder_switch = (viewHolder_switch) convertView.getTag ();
Log.e ("!!!!!! ConvertView =", "NULL TYPE_SWITCH");
break;
case TYPE_SEEKBAR:
holder_seekbar = (viewHolder_seekbar) convertView.getTag ();
Log.e ("!!!!!! ConvertView =", "NULL TYPE_SEEKBAR");
break;
case TYPE_BUTTON:
holder_button = (viewHolder_button) convertView.getTag ();
Log.e ("!!!!!! ConvertView =", "NULL TYPE_BUTTON");
break;
}
}

/ / Set the resource
switch (type)
{
case TYPE_SWITCH:
holder_switch.img.setBackgroundResource ((Integer) mData.get (position) get ("img").);
holder_switch.title.setText ((String) mData.get (position) get ("title").);
holder_switch.info.setText ((String) mData.get (position) get ("info").);
/ / Holder_switch.Switch.setChecked (true);
break;
case TYPE_SEEKBAR:

holder_seekbar.img.setBackgroundResource ((Integer) mData.get (position) get ("img").);
holder_seekbar.title.setText ((String) mData.get (position) get ("title").);
holder_seekbar.info.setText ((String) mData.get (position) get ("info").);

break;
case TYPE_BUTTON:
holder_button.img.setBackgroundResource ((Integer) mData.get (position) get ("img").);
holder_button.title.setText ((String) mData.get (position) get ("title").);
holder_button.info.setText ((String) mData.get (position) get ("info").);
break;
}

return convertView;
}
}

/ / Controls the layout of each resource
class viewHolder_switch {
/ ** Layout ID * /
public int layoutID;

/ ** Photo ID * /
public ImageView img;

/ ** Title * /
public TextView title;

/ ** Contents * /
public TextView info;
/ * Switch * /
public Switch Switch;



}

class viewHolder_seekbar {
/ ** Layout ID * /
public int layoutID;

/ ** Photo ID * /
public ImageView img;

/ ** Title * /
public TextView title;

/ ** Contents * /
public TextView info;

public SeekBar sb;

}

class viewHolder_button
{

private int layoutID;

private ImageView img;

/ ** Title * /
private TextView title;

/ ** Contents * /
private TextView info;

/ ** Button name * /
private Button button;


}
}


Reply:
listen seekBar button controls how these did not
Reply:
I want to know the above demo use?

No comments:

Post a Comment