Explanation
Everyone on Android can know how to use the Adapter . We can create a separate class for them. this will simplify the processing and understanding of our coding. But when we create these classes separately. They need Context ( Calling on behalf ). Therefore, in this case, we pass the activity context in our constructor. Thus, Android knows that we invoke it on behalf of what activity.
We cannot name
getSystemService(Context...)
in a separate adapter class, but we can pass the context in the Adapter constructor and call it as follows.
context.getSystemService(Context....)
Call your adapter as follows
ArticleAdapter adapter = new ArticleAdapter(context, list); list_of_article.setAdapter(adapter);
and get a context like this.
ArticleAdapter.class
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);
source share