1

i want to make a array of stringin listview android so i used this code but evry time eclipse say The constructor ArrayAdapter(listefriends.connect, int, String) is undefined in this line so how can help me :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.listview, user); 

codes java:

    public class listefriends extends Activity {
    ListView tvhttp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.friends);
        setContentView(R.layout.activity_list_item);
        tvhttp= ((ListView)findViewById(R.id.tvhttp));


        connect obj = new connect ();
        obj.execute("http://development.www.chatpassion.org/androidchat/friends.php?username=yassine11");

    }

    public class connect extends AsyncTask<String , Void   , String>
    {

        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            BufferedReader reader = null;
            String data = null;
            try {
                HttpClient client = new DefaultHttpClient();

                URI uri = new URI(params[0]);

                HttpGet get = new HttpGet(uri);

                HttpResponse response = client.execute(get);

                InputStream stream = response.getEntity().getContent();             
                reader = new BufferedReader(new InputStreamReader(stream));
                StringBuffer buffer = new StringBuffer("");
                String line ="";


                while ((line = reader.readLine()) !=null){
                    buffer.append(line);

                }
                reader.close();

                data = buffer.toString();
                return data;
            }catch (URISyntaxException e){
                e.printStackTrace();}
            catch (ClientProtocolException e){
                e.printStackTrace();}
            catch (IOException e){
                e.printStackTrace();}

            finally {
                if (reader !=null){
                    try {
                        reader.close();}
                    catch (Exception e){Log.i("error", e.toString());}

                    }
                }

            return null;
            }



    @Override
    protected  void onPostExecute(String result)
    {
        super.onPostExecute ( result);
        JSONObject json;
        //JSONObject objet2;
        try {
            json = new JSONObject(result);
            //tvhttp.setText(json.toString(2));
            JSONArray jsonArray = json.getJSONArray("FriendsList");
            String user = null ;

            for (int j = 0; j < jsonArray.length(); j++) {

                JSONObject jsonObjectData1 = jsonArray.getJSONObject(j);
                String username = jsonObjectData1.getString("username");
                String avatar = jsonObjectData1.getString("avatar");
                user = avatar.concat(username);  
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.activity_list_item,user);
                tvhttp.setAdapter(adapter);
                 //Log.i("tessssssssssst", user);
            }



        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




}
}
}

the layout listview :

<?xml version="1.0" encoding="utf-8"?>
<!--  Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:textSize="16dip"
        android:textStyle="bold" >
</TextView>

the layout of main :

<?xml version="1.0" encoding="UTF-8"?>
  <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
           android:id="@+id/scrollView1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/tvhttp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />



    </ScrollView>
0

4 Answers 4

1

Yes the ArrayAdapter's constructor takes a third parameter a Collection or an Array of Objects (String in your case). Note that in your case you are nesting a ListView inside a ScrollView and that's bad. Since the ScrollView will not let your ListView to scroll

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

4 Comments

ok i delete the scroll view but it don't work the probleme is in this line : ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.listview,user);
I know, and as I told you it is expecting an Array or an ArrayList of String not a string.
yes user is array of string but i think the problem is in first param
no user is a String not an array of string. Also to avoid the issue with the first parameter you can change this with listefriends.this. Also you must not create an Adapter at every iteration. Just do it when the for loop ends
1

You have ListView inside a ScrollView which is wrong.

http://developer.android.com/reference/android/widget/ScrollView.html

You should never use a ScrollView with a ListView, because ListView takes care of its own vertical scrolling

Secondly the param's that you pass to the constructor of ArrayAdapter is wrong.

It should match any of the below

ArrayAdapter(Context context, int resource)
ArrayAdapter(Context context, int resource, int textViewResourceId)
ArrayAdapter(Context context, int resource, T[] objects)
ArrayAdapter(Context context, int resource, int textViewResourceId, T[] objects)
ArrayAdapter(Context context, int resource, List<T> objects)
ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects)

Edit:

    ArrayList<Person> user = new ArrayList<Person>();
    //JSONObject objet2;
    try {
        json = new JSONObject(result);
        //tvhttp.setText(json.toString(2));
        JSONArray jsonArray = json.getJSONArray("FriendsList");


        for (int j = 0; j < jsonArray.length(); j++) {

            JSONObject jsonObjectData1 = jsonArray.getJSONObject(j);
            String username = jsonObjectData1.getString("username");
            String avatar = jsonObjectData1.getString("avatar");
            Person p = new Person();
            p.setUsername(username);
            p.setTitle(avatar);
            user.add(p);

        }
        CustomAdapter cus = new CustomAdapter(listfriends.this,user);
        tvhttp.setAdapter(cus);


    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

CustomAdapter

public class CustomAdapter extends ArrayAdapter<Person> {

    Context context;
    LayoutInflater mInflater;
    ArrayList<Person> user;
    public CustomAdapter(Context context, ArrayList<Person> user) {
    super(context,R.layout.row,user);
    this.context =context;
    this.user= user;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) { 
            convertView = mInflater.inflate(R.layout.row,parent, false);
            holder = new ViewHolder(); 
            holder.tv1 = (TextView) convertView.findViewById(R.id.textView1);
            holder.tv2 = (TextView) convertView.findViewById(R.id.textView2);
            convertView.setTag(holder);  
       } else { 
           holder = (ViewHolder) convertView.getTag();
       } 
          Person p = user.get(position);
          holder.tv1.setText(p.getUsername());
          holder.tv2.setText(p.getTitle());
       return convertView; 
    }
    static class ViewHolder
    {
        TextView tv1,tv2;

    }
}

Person.java

public class Person {
    String title,username;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

}

row.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="55dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="65dp"
        android:text="TextView" />

</RelativeLayout>

7 Comments

ok i delete the scroll view but it don't work the probleme is in this line : ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.listview,user);
@user3234804 blackbelt already suggested you that the 3rdd param should be a array or a arraylist of type string
@Raghunandan user is a String, since he is calling concat upon it
@blackbelt hmm. you are right. i mentioned assuming user is array. Misleading i guess.
yes user is array of string but i think the problem is in first param
|
0

I think you are creating fragment not an activity.

so use getActivity().getApplicationContext() instead of this in below line.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.listview, user);

Comments

0

Instead of this use listefriends.this. You can not have direct this here as you actually need context of the activity here.

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.