0

1.This ListView Adapter is integrate on fragment

public class ListViewAdapter extends BaseAdapter {

  // Declare Variables
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();

public ListViewAdapter(NewsFragment newsFragment,
        ArrayList<HashMap<String, String>> arraylist) {
    this.context = context;
    data = arraylist;
    imageLoader = new ImageLoader(context);
}



@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public View getView(final int position, View convertView, ViewGroup parent) {
//protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Declare Variables
    TextView  productname;
    TextView productid;
    TextView producttype;
    ImageView image;

here is the problems inflater = (LayoutInflater)convertView.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 enter code here

    View itemView = inflater.inflate(R.layout.listview_item, parent, false);
    // Get the position
    resultp = data.get(position);

    // Locate the TextViews in listview_item.xml
    productname = (TextView) itemView.findViewById(R.id.product);
    productid = (TextView) itemView.findViewById(R.id.dateOfInput);
    producttype = (TextView) itemView.findViewById(R.id.productprice);

    // Locate the ImageView in listview_item.xml
    image = (ImageView) itemView.findViewById(R.id.flag);

    // Capture position and set results to the TextViews
    productname.setText(resultp.get(NewsFragment.PRODUCTNAME));
    productid.setText(resultp.get(NewsFragment.PRODUCTPRICE));
    producttype.setText(resultp.get(NewsFragment.DATEOFINPUT));
    // Capture position and set results to the ImageView
    // Passes flag images URL into ImageLoader.class
    imageLoader.DisplayImage(resultp.get(NewsFragment.IMAGE), image);
    // Capture ListView item click
    itemView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // Get the position
            resultp = data.get(position);
            Intent intent = new Intent(context, SingleItemView.class);
            // Pass all data rank
            intent.putExtra("productname",resultp.get(NewsFragment.PRODUCTNAME));
            // Pass all data country
    intent.putExtra("productid", resultp.get(NewsFragment.PRODUCTPRICE));
            // Pass all data population
              intent.putExtra("producttype", resultp.get(NewsFragment.DATEOFINPUT));                
            // Pass all data flag
            intent.putExtra("image", resultp.get(NewsFragment.IMAGE));
            // Start SingleItemView Class
            context.startActivity(intent);

        }
    });
    return itemView;
}

}

2
  • where you are passing Context in ListViewAdapter class constructor ? Commented Apr 25, 2014 at 2:55
  • imageLoader = new ImageLoader(context); Commented Apr 25, 2014 at 3:03

1 Answer 1

1

You have made mistake in initializing the LayoutInflatrot

you have done this

inflater = (LayoutInflater)convertView.getContext() .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

which is wrong, because convertView will be null at the beginning.

instead you need to do something like

inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

and If you want to use context you need to initializing it correctly in your Adapter's constructor, it is currently pointing to itself.

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

5 Comments

inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); This code is my original code, When I put it into the fragment , the logcat will be indicate this coder is error
And because the ListViewAdapter is on fragment , only one page Android Activity, this code is work!!
if you can have avtivity object in adapter you can have activity.getLayoutInflator method will be there for you
and in your constructor you are not initializing the context correctly, it is pointing to it self only
instead of having NewsFragment newsFragment in constructor put Context context over their, and where ever you are creating ListAdapter's object pass the Application context or reference of your activity, in your case you have listadapter's object in fragment so use getActivity() over there

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.