0

I need to display image and text in my listview when i click a button search but it giving me an error in my setListAdapter(new MobileArrayAdapter(this, PNames,PImages));.. I have two classes. When I display onformLoad it working but, now I need to display it using a buttton, here my two classess :

    B_KZN.setOnClickListener(new View .OnClickListener() {

        @Override
            public void onClick(View Kv) {
    // TODO Auto-generated method stub

   JSONObject json = JSONfunctions.getJSONfromURL("http://10.0.2.2/php/p.php");

   try{

            JSONArray  earthquakes = json.getJSONArray("SKZN");

           for(int i=0;i<earthquakes.length();i++){                     

            JSONObject e = earthquakes.getJSONObject(i);    
            String BookName = e.getString("P_City");
        PNames.add(BookName);
    String BookImg = e.getString("Pname");
        PImages.add(BookImg);
    }       
        }catch(JSONException e){
         Log.e("log_tag", "Error parsing data "+e.toString());
    }                   
        setListAdapter(new MobileArrayAdapter(this, PNames,PImages));

And my second Class, :

public class MobileArrayAdapter extends ArrayAdapter<String> {
    private final Context context;
    private final String[] myBookNamelist = null;
    private ArrayList<String> MyP = new ArrayList<String>();
    private ArrayList<String> myPurl = new ArrayList<String>();

    public MobileArrayAdapter(Context context,ArrayList<String> Bname,ArrayList<String> BUrl) {
        super(context, R.layout.list_mobile, Bname);
        this.context = context;
        this.MyP = Bname;
        this.myPurl = BUrl;
    }

here my error : The constructor MobileArrayAdapter(new View.OnClickListener(){}, ArrayList<String>, ArrayList<String>) is undefined...it showing me red line.

0

3 Answers 3

3

Your declaring this as your Context. But you are in an Annoymous class of type View.OnClickListener

try this instead:

 setListAdapter(new MobileArrayAdapter(YourActivityName.this, PNames,PImages));
Sign up to request clarification or add additional context in comments.

Comments

3

The first parameter of the constructor is of type Context, however when passing this to the constructor you are inside a subclass of View.OnClickListener. As you are in an inner-class of your (most likely) activity, you just need to indicate that you want to pass the parent activity instance.

Where you're creating the adapter, use MyActivity.this:

setListAdapter(new MobileArrayAdapter(MyActivity.this, PNames,PImages));

4 Comments

way to take my answer and re-word it :p
@Blundell I beg your pardon? I wrote this at the same time as you wrote yours, independently. Unfortunately, SO doesn't show "seconds ago", only minutes. You'd probably see that our two answers differ by about 5 seconds. I don't like when others copy my answers and I don't do it myself.
It does show edit's too your answer, and I watched your original answer then your change. Don't worry yourself
@Blundell: yes, I changed it. I often answer with the main point and then update with more information. Is that a crime? :) But I'll give you a +1.
0

Use this code in your activity where you set adapter to your ListView, it will work

setListAdapter(new MobileArrayAdapter(yourActivityname.this, PNames,PImages));

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.