Reputation: 15535
Please help me friends. What is wrong in this program. I cant use LayoutInflater here. how To solve this problem.
public class MyListAdapter extends ArrayAdapter<MyData> implements
OnClickListener {
private ArrayList<MyData> items;
Context context;
public MyListAdapter(Context context, int textViewResourceId,
ArrayList<MyData> items) {
super(context, textViewResourceId, items);
this.items = items;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
MyData myData = items.get(position);
if (myData != null) {
TextView textViewTwo = (TextView) v
.findViewById(R.id.text_view_two);
if (textViewTwo != null) {
textViewTwo.setText(myData.getText());
// put the id to identify the item clicked
textViewTwo.setTag(myData.getId());
textViewTwo.setOnClickListener(this);
}
}
return v;
}
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("Sample", "Clicked on tag: " + v.getTag());
Toast.makeText(context, "", Toast.LENGTH_SHORT).show();
}
}
Error is : The method getSystemService(String) is undefined for the type MyListAdapter.
Thanks in advance.
Upvotes: 0
Views: 137
Reputation: 412
Use Activity's context to get the system services
This is the right way to call the inflator.
LayoutInflater vi = (LayoutInflater) context. getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Upvotes: 1
Reputation: 20155
Try context.getStystemService()
like this
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Upvotes: 1
Reputation: 132992
use
LayoutInflater vi = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
instead of
LayoutInflater vi = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
because for getSystemService
is method of Context class instead of ArrayAdapter
. u will need to use context instance to access getSystemService
method
Upvotes: 2