自定义列表视图与按钮

时间:2016-01-12 09:50:53

标签: java listview button android-listview

PatientList.java

 `public class PatientList extends FragmentActivity implements SwipeRefreshLayout.OnRefreshListener {
// Log tag
private static final String TAG = PatientList.class.getSimpleName();
// initially offset will be 0, later will be updated while parsing the json
private int offSet = 0;

// Movies json url
private static final String url = "http://192.168.0.100/test/apps.php";
private ProgressDialog pDialog;
private List<Patient> patientList = new ArrayList<Patient>();
private ListView listView;
private CustomListAdapter adapter;

private SwipeRefreshLayout swipeRefreshLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.patientlist_listview);

    listView = (ListView) findViewById(R.id.list);
    swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);

    adapter = new CustomListAdapter(this, patientList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    // changing action bar color
    getActionBar().setBackgroundDrawable(
            new ColorDrawable(Color.parseColor("#00BBD3")));

    swipeRefreshLayout.setOnRefreshListener(this);

    /**
     * Showing Swipe Refresh animation on activity create
     * As animation won't start on onCreate, post runnable is used
     */
    swipeRefreshLayout.post(new Runnable() {
                                @Override
                                public void run() {
                                    swipeRefreshLayout.setRefreshing(true);
                                    fetchPatients();
                                }
                            }
    );

    listView.setAdapter(adapter);

    // Click event for single list row
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(PatientList.this, "row " + position + " was pressed", Toast.LENGTH_LONG).show();
            switch (position) {
                case 0:
                    TextView c =(TextView)view.findViewById(R.id.title);
                    String item = c.getText().toString();
                    Log.d("id",item);
                    break;

                case 1:
                    break;
            }

        }

    });

}

/**
 * This method is called when swipe refresh is pulled down
 */
@Override
public void onRefresh() {
    fetchPatients();
}
/**
 * Fetching movies json by making http call
 */
private void fetchPatients() {

    // showing refresh animation before making http call
    swipeRefreshLayout.setRefreshing(true);

    // appending offset to url
    //String url = URL_TOP_250 + offSet;

    // Creating volley request obj
    JsonArrayRequest movieReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();
                    Log.d("id", "hi") ;
                    // Parsing json
                    // reset the list
                    patientList.clear();
                    adapter.notifyDataSetChanged();

                    for (int i = 0; i < response.length();i++) {
                        try {
                            Log.d("id","hi1") ;
                            Log.d("i","i:"+i);

                            Patient patient = new Patient();

                            Log.d("length", "length:" + response.length());
                            JSONObject objid= response.getJSONObject(i);
                            //get id
                            patient.setTitle(objid.getString("id"));

                            //Log.d("i","i:"+i);
                            //obj= response.getJSONObject(i+1);
                            //get image url second item
                            JSONObject objimage= response.getJSONObject(++i);
                            patient.setThumbnailUrl(objimage.getString("image"));
                            //Log.d("i","i:"+i);


                            //patient.setTitle(obj.getString("id"));
                            //patient.setThumbnailUrl(obj.getString("image"));


                            // Log.d("id",obj.getString("id")) ;
                            //  Log.d("image",obj.getString("image")) ;

                            // adding movie to movies array
                            if(i%2==1)
                                patientList.add(patient);

                            // updating offset value to highest value
                            //if (i >= offSet)
                            //  offSet = i;

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

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();

                    // stopping swipe refresh
                    swipeRefreshLayout.setRefreshing(false);

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            hidePDialog();
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();

            // stopping swipe refresh
            swipeRefreshLayout.setRefreshing(false);

        }
    });

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(movieReq);
}

@Override
public void onDestroy() {
    super.onDestroy();
    hidePDialog();
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

CustomListAdapter.java

public class CustomListAdapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<Patient> patientItems;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();

public CustomListAdapter(Activity activity, List<Patient> movieItems) {
    this.activity = activity;
    this.patientItems = movieItems;
}

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

@Override
public Object getItem(int location) {
    return patientItems.get(location);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);

    if (imageLoader == null)
        imageLoader = AppController.getInstance().getImageLoader();
    CirculaireNetworkImageView thumbNail = (CirculaireNetworkImageView) convertView
            .findViewById(R.id.thumbnail);

    TextView title = (TextView) convertView.findViewById(R.id.title);
    Button viewBtn = (Button)convertView.findViewById(R.id.view_btn);

    // getting movie data for the row
    Patient m = patientItems.get(position);

    // thumbnail image
    thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
    // title
    title.setText(m.getTitle());
    viewBtn.setTag(position);
    viewBtn.setOnClickListener(new OnItemClickListener(position));
    return convertView;
}

private class OnItemClickListener implements View.OnClickListener {
    private int mPosition;

    OnItemClickListener(int position) {
        mPosition = position;
    }

    @Override
    public void onClick(View arg0) {
        Patient p=patientItems.get(mPosition);
        String Title=p.getTitle();
        Log.d("Title at row"+mPosition,Title);
    }
}
}

单击listView后我没有得到任何响应,但按钮可以单击并显示我想要的数据。我不知道问题出在哪里。请给我一些帮助。谢谢。

0 个答案:

没有答案