user3234804
user3234804

Reputation: 23

make array in list view

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>

Upvotes: 1

Views: 126

Answers (4)

Raghunandan
Raghunandan

Reputation: 133560

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>

Upvotes: 1

Daud Arfin
Daud Arfin

Reputation: 2499

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

Upvotes: 0

Neha Agarwal
Neha Agarwal

Reputation: 622

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

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157437

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

Upvotes: 1

Related Questions