如何从适配器获取数据并在listview中显示

时间:2014-11-05 06:33:53

标签: android listview adapter

通过从服务器获取显示自定义列表视图的数据。列表项有一个按钮和一个textview。在这里,我正在尝试显示另一个列表视图和选定的列表视图数据。当用户点击列表项中的按钮时,完成选择。我已经完成从第一个Listview获取选定数据并将其存储在arraylist中。之后如何使用这个arraylist(它出现在适配器类中)并在第一个listview下面显示一个listview。如何继续。这是我的firstlistview适配器类

 package com.spatel.slantright.adapter;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;
import com.spatel.slantright.R;
import com.spatel.slantright.model.ManagerModel;
import com.spatel.slantright.model.UserDetials;

public class ManagersAdapter extends BaseAdapter {

    Context context;
    private List<UserDetials> rowItem = null;
    private List<UserDetials> notFollowing = null;
    LayoutInflater mInflater, notFollowinflater;
    boolean state[] = { true, true, true, true };





    public ManagersAdapter(Context context, List<UserDetials> alFollowings) {
        this.context = context;
        this.rowItem = alFollowings;

        this.mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    }

    /* private view holder class */
    private class ViewHolder {

        ImageView ivBlack;
        TextView tvUsername;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final UserDetials row_pos;
        row_pos = rowItem.get(position);
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.row_managers, null);
            holder = new ViewHolder();

            holder.ivBlack = (ImageView) convertView.findViewById(R.id.ivBlack);
            holder.tvUsername = (TextView) convertView
                    .findViewById(R.id.tvUsername);
            convertView.setTag(holder);
        }

        else {
            holder = (ViewHolder) convertView.getTag();
        }

        // setting the image resource and title


        //holder.ivBlack.setImageResource(rowItem.get(position).getIcon());
        holder.tvUsername.setText(rowItem.get(position).getUserName());

        holder.ivBlack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (state[position]) {
                    row_pos.setIcon(R.drawable.black_circle);
                    ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                            "UserDetails");
                    query.whereEqualTo("userId", ParseUser.getCurrentUser()
                            .getObjectId().toString());
                    query.findInBackground(new FindCallback<ParseObject>() {

                        @Override
                        public void done(List<ParseObject> object,
                                ParseException e) {

                            ParseObject objects = object.get(0);
                            if (e == null) {

                                try {

                                    JSONArray jArray = objects
                                            .getJSONArray("followings");
                                    JSONArray notfollowingJArray = new JSONArray();
                                    final JSONArray followingJArray = new JSONArray();

                                    if (jArray != null && jArray.length() > 0) {
                                        // alFollowings = new
                                        // ArrayList<UserDetials>();
                                        for (int i = 0; i < jArray.length(); i++) {
                                            // Excluding the item at position
                                            System.out
                                                    .println("POSITION ::::::::::::::::"
                                                            + position);
                                            if (i != position) {

                                                followingJArray.put(jArray.get(i));

                                            }else {
                                                System.out
                                                        .println("RRRRRRRRRRRRRRRRRRRRROOOOOOOOOOOOOOOOOOOOWWWWWWWWWWWWWWW" + rowItem.get(i).getUserName().toString());
                                                followingJArray.put(jArray.get(i));
                                                ArrayList<String> listdata = new ArrayList<String>();  
                                                if (jArray != null) { 
                                                       for (int j=0;j<jArray.length();j++){ 
                                                        listdata.add(jArray.get(j).toString());
                                                       } 
                                                    } 

                                            }
                                            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                                                    "UserDetails");
                                            query.whereEqualTo("userId", ParseUser.getCurrentUser()
                                                    .getObjectId().toString());
                                            query.findInBackground(new FindCallback<ParseObject>() {
                                                @Override
                                                public void done(List<ParseObject> object,
                                                        ParseException e) {

                                                    ParseObject objects = object.get(0);

                                                    objects.put(
                                                            "followings",
                                                            followingJArray);
                                                    objects
                                                            .saveInBackground();


                                                }
                                            });
                                        }

                                    }
                                } catch (JSONException e1) {
                                    e1.printStackTrace();
                                }

                            }

                        }
                    });

                    state[position] = false;
                    rowItem.remove(position);
                    notifyDataSetChanged();
                } else if (!state[position]) {
                    row_pos.setIcon(R.drawable.black_circle);
                    state[position] = true;
                    notifyDataSetChanged();
                }
            }
        });

        return convertView;
    }

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

    @Override
    public Object getItem(int position) {
        return rowItem.get(position);
    }

    @Override
    public long getItemId(int position) {
        return rowItem.indexOf(getItem(position));
    }
}

0 个答案:

没有答案