Я работаю над списком, и у меня есть адаптер, в котором я хочу показать два вида. Для этого я использую listview
с пользовательским базовым адаптером. Мой код работает нормально, но я не знаю, какое значение мне нужно вставить в getViewType()
.Как вставить значение в getViewType в android?
код:
private Context mContext;
private List<NewsSnippet> mItems;
private String TAG = CNewsAdpter.class.getSimpleName();
public CNewsAdpter(Context m_Context, List<NewsSnippet> items) {
this.mContext = m_Context;
mItems = items;
}
@Override
public int getItemViewType(int position) {
return 2;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {// get total arraylist size
return mItems.size();
}
@Override
public Object getItem(int position) {// get item position in array list
return mItems.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressWarnings("deprecation")
@SuppressLint({"SetTextI18n", "InflateParams"})
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
int type = getItemViewType(position);
if (v == null) {
// Inflate the layout according to the view type
LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (type == 0) {
// Inflate the layout with image
v = inflater.inflate(R.layout.news_item_for_layout, parent, false);
} else {
v = inflater.inflate(R.layout.vcom_layout, parent, false);
}
}
final NewsSnippet m = mItems.get(position);
ImageView m_Icon = (ImageView) v.findViewById(R.id.imgNewsIcon);
TextView m_Titletext = (TextView) v.findViewById(R.id.txtNewsTitle);
TextView m_Description = (TextView) v.findViewById(R.id.description);
Button m_ActionButton = (Button) v.findViewById(R.id.actionBtn);
if (type == 0) {
RatingBar m_RatingBar = (RatingBar) v.findViewById(R.id.pop_ratingbar);
String s = String.valueOf((float) m.s_szrating);
if (!s.equalsIgnoreCase("")) {
m_RatingBar.setRating((float) m.s_szrating);
}
}
m_Titletext.setText(m.s_sztitle);
m_Description.setText(m.s_szdescription);
m_ActionButton.setText(m.s_szcta);
m_ActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse(m.s_szlandingUrl));
v.getContext().startActivity(browserIntent);
}
});
try {
String url = m.s_szicon;
if (url != null && !url.equals(m_Icon.getTag())) {
m_Icon.setTag(url);
new CDownloadImageTask(m_Icon)
.execute(m.s_szicon);
}
} catch (Exception e) {
e.printStackTrace();
}
return v;
}
В каком положении вы хотите изменить макет вернуть эту позицию из getItemViewType, когда речь идет о .. вы можете проверить его, если условие –