我的列表视图没有显示任何我使用数组适配器和logcat没有错误

时间:2016-07-16 12:36:52

标签: android listview android-arrayadapter custom-adapter

当我运行此代码时,我在listview中没有得到任何内容。

我试图在文本视图中从服务器获取值,我从有效负载对象中的服务器获取值。但是我的列表视图仍然没有显示任何内容,并且logcat中没有错误。

            import android.content.Context;
            import android.content.SharedPreferences;
            import android.os.Bundle;
            import android.preference.PreferenceManager;
            import android.support.annotation.Nullable;
            import android.support.v7.app.AppCompatActivity;
            import android.util.Log;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import android.widget.AdapterView;
            import android.widget.ArrayAdapter;
            import android.widget.Button;
            import android.widget.CheckBox;
            import android.widget.ListView;
            import android.widget.TextView;
            import android.widget.Toast;

            import com.android.volley.AuthFailureError;
            import com.android.volley.Request;
            import com.android.volley.RequestQueue;
            import com.android.volley.Response;
            import com.android.volley.VolleyError;
            import com.android.volley.toolbox.StringRequest;
            import com.android.volley.toolbox.Volley;
            import com.zalonstyles.app.zalon.Model.Services;
            import com.zalonstyles.app.zalon.Model.ViewHolder;

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

            import java.util.ArrayList;
            import java.util.HashMap;
            import java.util.List;
            import java.util.Map;

这是我从服务器

获取数据的类
public  class popup_massage extends AppCompatActivity {
                public static final String URL =  "http://52.41.72.46:8080/service/get_category";
                public final String serviceid = "6";
                private ListView mainListView;
                private Button check;
                private List<Services> massagelist = new ArrayList<>();
                private Services massageservice[];
                private Context context;
               public ArrayAdapter<Services> listAdapter;


                @Override
                protected void onCreate(@Nullable Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.popup_massage);
                    SharedPreferences mSharedPreference= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
                    String value=(mSharedPreference.getString("AppConstant.AUTH_TOKEN", "DEFAULT"));
                    Log.e("accesslog",value);
                    mainListView = (ListView) findViewById(R.id.listView4);
                    check = (Button) findViewById(R.id.button4);

                    final JSONObject params = new JSONObject();
                    try {
                        params.put("service_id",serviceid);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    try {
                        params.put("access_token", value);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
                            new Response.Listener<String>(){

                                @Override
                                public void onResponse(String response) {

                                    Log.v("updateUPVolleyRes1",response);

                                    try {
                                        JSONObject jobject = new JSONObject(response);
                                        JSONArray payload = jobject.getJSONArray("payload");
                                        Log.e("payloaddata", String.valueOf(payload));
                                        for (int i = 0; i < payload.length(); i++) {
                                            try{
                                                JSONObject obj = payload.getJSONObject(i);
                                                Services service = new Services();
                                                service.setName(obj.getString("name"));
                                                service.setcategotry_id(obj.getString("id"));
                                                massagelist.add(service);







                                            }finally {

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

                                }

                            }
                            , new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.v("updateUPVolleyErr", error.toString());

                        }
                    }){
                        @Override
                        protected Map<String, String> getParams() throws AuthFailureError {
                            Map<String, String> params1 = new HashMap<String, String>();

                            params1.put("payload", params.toString());

                            Log.v("updateUPVolleyParams", params1.toString());

                            return params1;

                        }
                    };

                    RequestQueue requestQueue = Volley.newRequestQueue(this);
                    requestQueue.add(stringRequest);


                    mainListView
                            .setOnItemClickListener(new AdapterView.OnItemClickListener()
                            {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View item,
                                                        int position, long id)
                                {
                                    Services massageservice = listAdapter.getItem(position);
                                    Log.e("CHECKADAPTOR", String.valueOf(massageservice));
                                    massageservice.toggleChecked();
                                    ViewHolder viewHolder =(ViewHolder) item
                                            .getTag();
                                    viewHolder.getCheckBox().setChecked(massageservice.isChecked());
                                }
                            });
                    //Services[] massageService = (Services[]) getLastNonConfigurationInstance();ArrayList<Services> massagelist = new ArrayList<Services>();
                    //massagelist.addAll(Arrays.asList(massageService));

                    // Set our custom array adapter as the ListView's adapter.

                    listAdapter = new massageArrayAdapter(this,massagelist);

                    mainListView.setAdapter(listAdapter);



                    check.setOnClickListener(new View.OnClickListener()
                    {


                        @Override
                        public void onClick(View view)
                        {
                            for (int i = 0; i < listAdapter.getCount(); i++)
                            {
                                Services massage = listAdapter.getItem(i);
                                if (massage.isChecked())
                                {
                                    Toast.makeText(getApplicationContext(),
                                            massage.getName() + " is Checked!!",
                                            Toast.LENGTH_SHORT).show();
                                    Log.v("My Custom Tag", massage.getName());

                                }
                            }

                        }
                    });


                }
        //arrayadapter class//

                private static class massageArrayAdapter extends ArrayAdapter<Services>
                {

                    private LayoutInflater inflater;

                    public massageArrayAdapter(Context context, List<Services> massagelist)
                    {
                        super(context, R.layout.customlist_massage, R.id.massagetextview, massagelist);
                        // Cache the LayoutInflate to avoid asking for a new one each time.
                        inflater = LayoutInflater.from((Context) context);
                    }

                    @Override
                    public View getView(int position, View convertView, ViewGroup parent)
                    {
                        // services to display
                        Services massage = (Services) this.getItem(position);

                        // The child views in each row.
                        CheckBox checkBox;
                        TextView textView;

                        // Create a new row view
                        if (convertView == null)
                        {
                            convertView = inflater.inflate(R.layout.customlist_massage,null );

                            // Find the child views.
                            textView = (TextView) convertView
                                    .findViewById(R.id.massagetextview);
                            checkBox = (CheckBox) `enter code here`convertView.findViewById(R.id.checkBox4);

                            // Optimization: Tag the row with it's child views, so we don't
                            // have to
                            // call findViewById() later when we reuse the row.
                            convertView.setTag(new ViewHolder(textView, checkBox));

                            // If CheckBox is toggled, update the planet it is tagged with.
                            checkBox.setOnClickListener(new View.OnClickListener()
                            {
                                public void onClick(View v)
                                {
                                    CheckBox cb = (CheckBox) v;
                                    Services massage = (Services) cb.getTag();
                                    massage.setChecked(cb.isChecked());
                                }
                            });
                        }
                        // Reuse existing row view
                        else
                        {
                            // Because we use a ViewHolder, we avoid having to call
                            // findViewById().
                            ViewHolder viewHolder = (ViewHolder) convertView
                                    .getTag();
                            checkBox = viewHolder.getCheckBox();
                            textView = viewHolder.getTextView();
                        }

                        // Tag the CheckBox with the service it is displaying,       
                        // access the planet in onClick() when the CheckBox is     toggled.         
                        checkBox.setTag(massage);

                        // Display planet data
                        checkBox.setChecked(massage.isChecked());
                        textView.setText(massage.getName());

                        return convertView;
                    }

                }

            }

我的服务类用于存储服务

  public class Services
      {
       private String name = "";
       private boolean checked = false;
       private String categotry_id="";

        public Services()
        {
        }

        public Services(String name)
        {
            this.name = name;
        }

        public Services(String name, boolean checked,String categotry_id)
    {
        this.name = name;
        this.categotry_id = categotry_id;
        this.checked = checked;
    }
        public Services(String name,String categotry_id)
        {
            this.name = name;
            this.categotry_id = categotry_id;

        }
        public String getCategory_id(String id)
        {
            return categotry_id;
        }

        public void setcategotry_id(String categotry_id)
        {
            this.name = categotry_id;
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public boolean isChecked()
        {
            return checked;
        }

        public void setChecked(boolean checked)
        {
            this.checked = checked;
        }

        public String toString()
        {
            return name;
        }

        public void toggleChecked()
        {
            checked = !checked;
        }
    }

查看用于存储视图的持有者类

            public class ViewHolder
            {
                private CheckBox checkBox;
                private TextView textView;

                public ViewHolder()
                {
                }

                public ViewHolder(TextView textView, CheckBox checkBox)
                {
                    this.checkBox = checkBox;
                    this.textView = textView;
                }

                public CheckBox getCheckBox()
                {
                    return checkBox;
                }

                public void setCheckBox(CheckBox checkBox)
                {
                    this.checkBox = checkBox;
                }

                public TextView getTextView()
                {
                    return textView;
                }

                public void setTextView(TextView textView)
                {
                    this.textView = textView;
                }
            } 

我的xml类列表视图在哪里

     <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#3F51B5">
    <ListView
        android:id="@+id/listView4"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:background="#fff"
        android:layout_weight="1" >
    </ListView>

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="313dp"
        android:text="Continue" />

</LinearLayout>

请帮我解决这个问题,我需要在明天之前提交。

2 个答案:

答案 0 :(得分:0)

使用此

convertView = inflater.inflate(R.layout.customlist_massage,null ,false );

而不是这个

convertView = inflater.inflate(R.layout.customlist_massage,null );

答案 1 :(得分:0)

为什么要扩展适配器类ArrayAdapter。 如果您想在listview上显示某些内容,则必须将其扩展到BaseAdapter或Recyclerview适配器。 您在ArrayAdapter中没有任何getCount()方法,请参阅此示例::

public class Adapter extends BaseAdapter {
    LayoutInflater li;
    ArrayList<String> title, details;
    Context con;
    DataBaseHelper db;
    TextView txttitle, txtdetails, txtDate;
    Intent in;
    public static int i = 1;
    View view;

    public Adapter (Context cont, ArrayList<String> news_id, ArrayList<String> news) {
        this.title = news_id;
        this.details = news;
        this.con = cont;
        li = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }


    @Override
    public int getCount() {
        System.out.println("title.size() " + title.size());
        return title.size();
    }

    @Override
    public Object getItem(int arg0) {
        return arg0;
    }

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

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {

        if (arg1 == null) {
            arg1 = li.inflate(R.layout.adapter_daycare, null);

            txttitle = (TextView) arg1.findViewById(R.id.title);
            txtdetails = (TextView) arg1.findViewById(R.id.details);
            txtDate = (TextView) arg1.findViewById(R.id.txtdate);
            view = (View) arg1.findViewById(R.id.view);


        }


        String date = new SimpleDateFormat("dd/MM/yyyy").format(new Date());

        System.out.println(" formattedDate " + date);


        txttitle.setText(title.get(arg0).toString());
        txtdetails.setText(details.get(arg0).toString());
        txtDate.setText(date);
        return arg1;

    }
}