在列表视图中创建数组

时间:2014-02-03 09:40:56

标签: android arrays string listview

我想创建一个stringin listview android的数组,所以我使用了这段代码但是ery时间eclipse说构造函数ArrayAdapter(listefriends.connect,int,String)在这行中是未定义的,所以如何帮助我:

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

代码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();
        }




}
}
}

布局列表视图:

<?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>

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>

4 个答案:

答案 0 :(得分:1)

ListView内有ScrollView这是错误的。

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

你永远不应该使用带有ListView的ScrollView,因为ListView负责自己的垂直滚动

其次,传递给ArrayAdapter的构造函数的参数是错误的。

它应匹配以下任何一个

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)

编辑:

    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>

答案 1 :(得分:1)

是的,ArrayAdapter的构造函数接受第三个参数Collection或Object of Array(在您的情况下为String)。请注意,在您的情况下,您正在ScrollView中嵌套ListView,这很糟糕。由于ScrollView不会让ListView滚动

答案 2 :(得分:0)

我认为你创建的片段不是活动。

因此请在下面一行使用getActivity().getApplicationContext()代替this

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

答案 3 :(得分:0)

而不是this使用listefriends.this。你不能在这里直接this,因为你实际上需要这里的活动背景。

相关问题