0

リストビュー内に複数のテキストビューを配置できるように、独自の arrayAdapter を作成しようとしています。どこでも検索しましたが、それを行う方法が見つかりません。私はこれが初めてで、それを処理する方法がよくわかりません。これまでのところ、JSON メソッドで 3 つの文字列を収集する asynctask があります。これらの文字列は、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

3 つの関連する Array を作成する代わりに、コメントに関する情報を保持するクラスを作成するとします。

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 メソッドがありますが、今度は String だけでなく完全なオブジェクトを取得できます。オーバーライドする方法は他にもいくつかありますが、ご覧のとおり、非常に単純です。

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 に答える