0

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.

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

Try context.getStystemService() like this

 LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.