单击动态创建的列表框

时间:2014-10-13 16:27:57

标签: java android json listbox onclick

我有一个问题,我遵循了一些教程,我终于设法创建了一个dinamical ListBox,但我有一个问题,我不知道如何设置OnClick上创建的项目,这是我的片段:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListAdapter(this, serverList);
    listView.setAdapter(adapter);

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



    // Creating volley request obj
    JsonArrayRequest serverReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    // Parsing json
                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            server elemento = new server();
                            elemento.setTitle(obj.getString("title"));
                            elemento.setThumbnailUrl(obj.getString("image"));
                            elemento.setStatus(obj.getString("status"));
                            elemento.setIp(obj.getString("ip"));

                            // adding movie to movies array
                            serverList.add(elemento);

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

                    }

                    // notifying list adapter about data changes
                    // so that it renders the list view with updated data
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }
            });

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

这是我的列表适配器:

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

    public CustomListAdapter(Activity activity, List<server> serverItems) {
        this.activity = activity;
        this.serverItems = serverItems;
    }

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

    @Override
    public Object getItem(int location) {
        return serverItems.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();
        NetworkImageView thumbNail = (NetworkImageView) convertView
                .findViewById(R.id.thumbnail);
        TextView title = (TextView) convertView.findViewById(R.id.title);

        TextView ip = (TextView) convertView.findViewById(R.id.ip);
        TextView status = (TextView) convertView.findViewById(R.id.status);

        // getting data for the row
        server m = serverItems.get(position);

        // thumbnail image
        thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);

        // title
        title.setText(m.getTitle());

        // ip
        ip.setText("Ip: " + String.valueOf(m.getIp()));



        // status
        status.setText(String.valueOf(m.getStatus()));

        return convertView;
    }

}

我真的不知道在哪里以及如何添加列表框中每个项目的OnClick事件,因为它应该使用json适配器所获取的数据获取的动态信息,有人可以帮助我吗? 谢谢,Matteo。

1 个答案:

答案 0 :(得分:0)

如果我是对的,您无需在每个项目中添加setOnClickListener

使用listview.setOnItemClickListener它将处理每个&amp;的点击每个元素

将其添加到

下面
  

listview.setAdapter()

以下是一个示例:

listview.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parentView, View childView, int position, long id) 
{
/   /here you have the position so you can do whatever you want with this item


 }
 });