Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

accessing custom objects in a listview

I'm currently building an app that has a request being sent into the web, the result then gets parsed and put into an ArrayList of objects.

this list then, populates a ListView. I want to create an onClickListener that will allow me to know which object was clicked, but I can't find the correct way to implement this.

either assign the onClick to the ListView in the original activity, or, assign the listener within the getView function in the custom adapter.

it seems to me like assigning the listeners in the getView has too much overhead.

how does it work? what is better?

code for main activity:

public class NoPicList extends Activity {
    ListView list;
    NoPicAdapter adapter;
    ProgressDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.no_pic_list);
        list = (ListView) findViewById(R.id.noPicListView);

        Bundle b = getIntent().getExtras();
        String request = b.getString("REQUEST");
        mDialog = new ProgressDialog(this);
        mDialog.setCancelable(false);
        mDialog.setMessage("Lodaing Data");
        mDialog.show();

        new GetNewsAndCalendar().execute(request);
    }

    @Override
    protected void onPause() {
        mDialog.dismiss();
        super.onPause();
    }

    class GetNewsAndCalendar extends
        AsyncTask<String, Void, ArrayList<Message>> {

        @Override
        protected ArrayList<Message> doInBackground(String... params) {
            String url = params[0];
            DOMFeedParser parser = new DOMFeedParser(url);
            return parser.parse();
        }

        @Override
        protected void onPostExecute(ArrayList<Message> result) {
            adapter = new NoPicAdapter(NoPicList.this, result);
            list.setAdapter(adapter);
            //FIRST OPTION TO INSERT onClickListener
            mDialog.dismiss();
        }
    }    //end of GetNewsAndCalendar
}

code for list adapter:

public class NoPicAdapter extends BaseAdapter {

    private ArrayList<Message> data;
    private Activity mActivity;
    private LayoutInflater inflater = null;

    public NoPicAdapter(Activity a, ArrayList<Message> result) {
        mActivity = a;
        data = result;
        inflater = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

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

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null)
            vi = LayoutInflater.from(mActivity).inflate(R.layout.no_pic_list_item,
                null);

        TextView title = (TextView) vi.findViewById(R.id.noPicTitle);
        TextView subtitle = (TextView) vi.findViewById(R.id.noPicSubtitle);

        title.setText(data.get(position).getTitle());
        subtitle.setText(data.get(position).getDate());
    // SECOND PLACE TO INSERT THE onClickListener
        return vi;
    }
}
like image 422
thepoosh Avatar asked Apr 23 '12 06:04

thepoosh


1 Answers

As per my concern, assign the onClick to the ListView in the original activity is good idea, rather then assigning it to in getView() of your custom adapter. Because in getView() of your adapter it always create a new View.onClick()'s object..

list.setOnItemClickListener(new OnItemClickListener()
 {
  @Override
  public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
  {
   adapter.get(position);  
   // Get data from your adapter,   the above code of line give the custom adapter's object of   current position of selected list item     
  }   
 });  
like image 152
user370305 Avatar answered Sep 18 '22 12:09

user370305