3
   listView.setOnItemClickListener(new OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {


                if(position == 1 ){
                    Toast.makeText(getApplicationContext(),
                    "Click ListItem Number " + position,
 Toast.LENGTH_LONG)
                    .show();
                }else{
                     }

i have a list view from a custom adapter that works fine and when i click an item it gets the correct position. how can i exract a field views text out of the position. ie

 holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);

i want to target the desc field out of the position listem that i clicked.. i know you can use hash mapping but is there a way to just target a field view on item click by its list position? I know this is "webby" but I am sure java has the same flexibility

  public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;
    RowItem rowItem = getItem(position);

    LayoutInflater mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);
        holder = new ViewHolder();
        holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
        holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
        convertView.setTag(holder);
    } else
        holder = (ViewHolder) convertView.getTag();

    holder.txtDesc.setText(rowItem.getDesc());
    holder.txtTitle.setText(rowItem.getTitle());


    return convertView;
}       

3 Answers 3

2

how can i exract a field views text out of the position. ie

holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);

Inside onItemClick() you can use:

TextView textview = (TextView) view.findViewById(R.id.desc);
// Now use textView.getText().toString() to get the description

Since convertView from the Adapter is the same object as view in the Listener.

Sign up to request clarification or add additional context in comments.

3 Comments

This will work, but grabbing the data back out of the presentation layer is sub-optimal, and won't work when you aren't listing a simple array of Strings.
"grabbing the data back out of the presentation layer is sub-optimal" Yup, but only by milliseconds, I posted two alternates in the comments below your question. "and won't work when you aren't listing a simple array of Strings" The question is about displaying Strings.
Thanks, I just wanted to leave that there in case someone else comes across this solution.
0

Can't you just call adapter.getItem(position)?

10 Comments

wont all that do is get the position in the list?
Are you using an ArrayAdapter to put an array of Objects into your listView? If so, then you can call yourAdapter.getItem(position), where position is the argument that camein on the click handler function. This will give you the original Object you passed in to the adapter.
yes I am listView.getItemAtPosition(i).toString(); i was trying this but I need just a specific section not the whole text
That's only part of the question. Are you using an ArrayAdapter?
yes i made a custom one because i have a few lines of text so i am converting textviews
|
0
  1. If you need only text current item use BaseAdapter and you can get text following way:

    (String) adapter.getItem(position);

  2. If you need TextView object current item you can get TextView following way: listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
    ViewHolder holder = (ViewHolder)view;
    TextView txtDescTV = holder.txtDesc;
    }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.