0

我正在尝试创建自己的 arrayAdapter,以便可以在列表视图中放置多个文本视图。我到处搜索,找不到办法。我是新手,不太确定如何处理它。到目前为止,我有一个在 JSON 方法中收集 3 个字符串的异步任务。这些字符串是我想要放置在 textViews 中的,但我不知道该怎么做,这是我当前的代码。

    class loadComments extends AsyncTask<JSONObject, String, JSONObject> {
             private ArrayAdapter<String> mAdapter = null;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();


            } 

            @Override
            protected void onProgressUpdate(String... values) {
                super.onProgressUpdate(values);

            } 

            protected JSONObject doInBackground(JSONObject... params) {


                JSONObject json2 = CollectComments.collectComments(usernameforcomments, offsetNumber);


                    return json2;



            }

            @Override
            protected void onPostExecute(JSONObject json2) {
                try {  
                    if (json2.getString(KEY_SUCCESS) != null) { 
                        registerErrorMsg.setText("");
                        String res2 = json2.getString(KEY_SUCCESS);
                        if(Integer.parseInt(res2) == 1){ 


                            JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
                            final String comments[] = new String[commentArray.length()];
                            for ( int i=0; i<commentArray.length(); i++ ) {
                                comments[i] = commentArray.getString(i);
                            }
                            JSONArray numberArray = json2.getJSONArray(KEY_NUMBER);
                            String numbers[] = new String[numberArray.length()];
                            for ( int i=0; i<numberArray.length(); i++ ) {
                                numbers[i] = numberArray.getString(i);
                            }
                            JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME);
                            String usernames[] = new String[usernameArray.length()];
                            for ( int i=0; i<usernameArray.length(); i++ ) {
                                usernames[i] = usernameArray.getString(i);
                            }

                            ArrayList<String> myList = new ArrayList<String>();

                            class MyClassAdapter extends ArrayAdapter<String> {

                                private Context context;

                                public MyClassAdapter(Context context, int textViewResourceId, ArrayList<String> items) {
                                    super(context, textViewResourceId, items);
                                    this.context = context;
                                }

                                public View getView(int position, View convertView) {
                                    View view = convertView;
                                    if (view == null) {
                                        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                                        view = inflater.inflate(R.layout.list_item, null);
                                    }

                                    String item = getItem(position);
                                    if (item!= null) {
                                        // My layout has only one TextView
                                        TextView commentView = (TextView) view.findViewById(R.id.listComment);
                                        TextView usernameView = (TextView) view.findViewById(R.id.listPostedBy);
                                        TextView NumberView = (TextView) view.findViewById(R.id.listNumber);

                                            // do whatever you want with your string and long
                                            commentView.setText(comments);
                                            NumberView.setText(numbers);
                                            usernameView.setText(usernames);

                                     }

                                    return view;
                                }
                            }



                            }//end if key is == 1
                        else{
                            // Error in registration
                            registerErrorMsg.setText(json2.getString(KEY_ERROR_MSG));
                        }//end else
                    }//end if
                } //end try

                catch (JSONException e) { 
                    e.printStackTrace();
                }//end catch    
            }
        }


        new loadComments().execute();

此代码不起作用,但我认为我在正确的轨道上。

4

1 回答 1

0

假设您创建了一个类来保存有关评论的信息,而不是创建三个相关的 Arrays :

class Commentary
{
    public String username;
    public String comment;
    public int commentaryIndex;
}

BaseAdapter 可以将 List 作为参数,而 ArrayAdapter 不会。

class MyRealAdapter extends BaseAdapter
{
    private List<Commentary> comments;

    public MyRealAdapter(List<Commentary> comments )
    {
        this.comments = comments;
    }

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

    @Override
    public Object getItem(int index) {
        return comments.get(index);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Commentary c = (Commentary) getItem(position);
        //c.username, c.comment, c.commentaryIndex

        // create the view and stuff
        return null;
    }
}

如您所见,您再次拥有 getView 方法,但现在您可以检索完整的对象,而不仅仅是字符串。还有更多方法可以覆盖,但正如您所见,它非常简单。

您可能需要将其他参数(如 Context 或 LayoutInflater)传递给构造函数,但这不是强制性的。

编辑 :

    JSONArray commentArray = json2.getJSONArray(KEY_COMMENT);
    JSONArray numberArray = json2.getJSONArray(KEY_NUMBER);
    JSONArray usernameArray = json2.getJSONArray(KEY_USERNAME);
    ArrayList<Commentary> comments = new ArrayList<commentary>();
    for ( int i=0; i<commentArray.length(); i++ ) {
        Commentary c = new Commentary();
        c.username = usernameArray.getString(i);
        c.comment = commentArray.getString(i);
        c.commentaryIndex = Integer.parseInt(numberArray.getString(i));
        comments.add(c);
    }

    MyRealAdapter adapter = new MyRealAdapter(comments);
于 2013-07-10T16:27:39.970 回答