Пояснение
Все в Android могут знать, как использовать Adapter
.Мы можем создать отдельный класс для них.это сделает наше кодирование более простым в обращении и понимании.Но когда мы создаем эти классы отдельно.Им нужен контекст (Calling on behalf
).Таким образом, в этом случае мы передаем контекст действия в их конструктор.Таким образом, Android знает, что мы вызываем это от имени какого действия.
Мы не можем вызвать
getSystemService(Context...)//blah bhal
в отдельном классе адаптера, но мы можем передать контекст в конструкторе Adapterи может называть это следующим образом ..
context.getSystemService(Context....)//
Назовите свой адаптер следующим образом:
ArticleAdapter adapter = new ArticleAdapter(context, list);
list_of_article.setAdapter(adapter);
и получите контекст, подобный этому.класс
public class ArticleAdapter extends BaseAdapter
{
Context context;
ArrayList<HashMap<String, String>> list;
LayoutInflater inflater;
public ArticleAdapter(Context context,
ArrayList<HashMap<String, String>> list)
{
this.context = context;
this.list = list;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
return list.size();
}
@Override
public Object getItem(int position)
{
return list.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if (view == null)
{
view = inflater.inflate(R.layout.item_article, parent, false);
}
HashMap<String, String> map = list.get(position);
TextView Title = (TextView) view.findViewById(R.id.item_title);
TextView ByWhom = (TextView) view.findViewById(R.id.item_bywhom);
ImageView Img = (ImageView) view.findViewById(R.id.item_img);
ProgressBar bar = (ProgressBar) view.findViewById(R.id.progressBar1);
TextView TextUnderImg = (TextView) view
.findViewById(R.id.item_text_under_imag);
TextView Comments = (TextView) view.findViewById(R.id.item_comment);
TextView TableView = (TextView) view.findViewById(R.id.item_tableview);
TextView ReadMore = (TextView) view.findViewById(R.id.item_readmore);
context.getSystemService(Context.CONNECTIVITY_SERVICE);// if you want these service you must have to call it using context.
Title.setText(map.get("title"));
ByWhom.setText(map.get("publishdate"));
return view;
}
}