Dominik Erbsland wrote:
> I have a ListActivity which is being filled via an internal class
> "OrderAdapter" - while the list is being populated I set various
> OnClickListeners on the TextView elements which should open the same
> view with different parameter values per line. My problem I have is
> that no matter which entry in the list I click I will always get to a
> view with the same parameter ID (it's always the ID of the last line
> in the list) - even though the output of the ID varies if I output it
> to the TextView.
> 
> Here is the code of the internal class which populates the list in the
> ListActivity:
> [code]
>       private class OrderAdapter extends ArrayAdapter<Cheat> {
> 
>               private ArrayList<Cheat> items;
>               private Cheat cheat;
> 
>               public OrderAdapter(Context context, int textViewResourceId,
> ArrayList<Cheat> items) {
>                       super(context, textViewResourceId, items);
>                       this.explicitIntent = new Intent[items.size()];
>                       this.items = items;
>               }
> 
>               public View getView(int position, View convertView, ViewGroup
> parent) {
>                       View v = convertView;
>                       if (v == null) {
>                               LayoutInflater vi = (LayoutInflater)
> getSystemService(Context.LAYOUT_INFLATER_SERVICE);
>                               v = vi.inflate(R.layout.fulltext_search, null);
>                       }
> 
>                       try {
>                               cheat = items.get(position);
>                               if (cheat != null) {
> 
>                                       TextView tvGameName = (TextView) 
> v.findViewById(R.id.tvGameName);
>                                       TextView tvSystem = (TextView) 
> v.findViewById(R.id.tvSystem);
>                                       TextView tvCheatTitle = (TextView)
> v.findViewById(R.id.tvCheatTitle);
>                                       TextView tvCheatText = (TextView)
> v.findViewById(R.id.tvCheatText);
> 
>                                       if (tvGameName != null) {
>                                               
> tvGameName.setText(cheat.getGameName());
>                                               
> tvGameName.setOnClickListener(new OnClickListener() {
> 
>                                                       public void 
> onClick(View v) {
>                                                               Intent 
> explicitIntent = new Intent(FulltextSearchResult.this,
> CheatView.class);
>                                                               
> explicitIntent.putExtra("cheatId", cheat.getCheatId());
>                                                               
> explicitIntent.putExtra("cheatTitle", cheat.getCheatTitle());
>                                                               
> explicitIntent.putExtra("gameId", cheat.getGameId());
>                                                               
> explicitIntent.putExtra("gameName", cheat.getGameName());
>                                                               
> explicitIntent.putExtra("systemId", cheat.getSystemId());
>                                                               
> explicitIntent.putExtra("systemName", cheat.getSystemName());
>                                                               
> startActivity(explicitIntent);
>                                                       }
> 
>                                               });
>                                       }
> 
>                                       if (tvSystem != null) {
>                                               tvSystem.setText("(" + 
> cheat.getSystemName() + ")");
>                                               tvSystem.setOnClickListener(new 
> OnClickListener() {
> 
>                                                       public void 
> onClick(View v) {
>                                                               Intent 
> explicitIntent = new Intent(FulltextSearchResult.this,
> CheatView.class);
>                                                               
> explicitIntent.putExtra("cheatId", cheat.getCheatId());
>                                                               
> explicitIntent.putExtra("cheatTitle", cheat.getCheatTitle());
>                                                               
> explicitIntent.putExtra("gameId", cheat.getGameId());
>                                                               
> explicitIntent.putExtra("gameName", cheat.getGameName());
>                                                               
> explicitIntent.putExtra("systemId", cheat.getSystemId());
>                                                               
> explicitIntent.putExtra("systemName", cheat.getSystemName());
>                                                               
> startActivity(explicitIntent);
>                                                       }
> 
>                                               });
>                                       }
> 
>                                       if (tvCheatTitle != null) {
>                                               String styledTitle = 
> cheat.getCheatTitle();
>                                               
> tvCheatTitle.setText(Html.fromHtml(styledTitle),
> TextView.BufferType.SPANNABLE);
>                                               
> tvCheatTitle.setOnClickListener(new OnClickListener() {
> 
>                                                       public void 
> onClick(View v) {
>                                                               Intent 
> explicitIntent = new Intent(FulltextSearchResult.this,
> CheatView.class);
>                                                               
> explicitIntent.putExtra("cheatId", cheat.getCheatId());
>                                                               
> explicitIntent.putExtra("cheatTitle", cheat.getCheatTitle());
>                                                               
> explicitIntent.putExtra("gameId", cheat.getGameId());
>                                                               
> explicitIntent.putExtra("gameName", cheat.getGameName());
>                                                               
> explicitIntent.putExtra("systemId", cheat.getSystemId());
>                                                               
> explicitIntent.putExtra("systemName", cheat.getSystemName());
>                                                               
> startActivity(explicitIntent);
>                                                       }
> 
>                                               });
>                                       }
> 
>                                       if (tvCheatText != null) {
>                                               String styledText = 
> cheat.getCheatText();
>                                               styledText = 
> styledText.replaceAll("</th><th>", " - ");
>                                               styledText = 
> styledText.replaceAll("</td><td>", " - ");
>                                               styledText = 
> styledText.replaceAll("</tr>", "<br>");
> 
>                                               
> tvCheatText.setText(Html.fromHtml(styledText),
> TextView.BufferType.SPANNABLE);
>                                               
> tvCheatText.setOnClickListener(new OnClickListener() {
> 
>                                                       public void 
> onClick(View v) {
>                                                               Intent 
> explicitIntent = new Intent(FulltextSearchResult.this,
> CheatView.class);
>                                                               
> explicitIntent.putExtra("cheatId", cheat.getCheatId());
>                                                               
> explicitIntent.putExtra("cheatTitle", cheat.getCheatTitle());
>                                                               
> explicitIntent.putExtra("gameId", cheat.getGameId());
>                                                               
> explicitIntent.putExtra("gameName", cheat.getGameName());
>                                                               
> explicitIntent.putExtra("systemId", cheat.getSystemId());
>                                                               
> explicitIntent.putExtra("systemName", cheat.getSystemName());
>                                                               
> startActivity(explicitIntent);
>                                                       }
> 
>                                               });
>                                       }
> 
>                               }
> 
>                       } catch (Exception e) {
>                               Log.e("FulltextSearchResult.getView ERROR:", 
> e.getMessage());
>                       }
>                       return v;
>               }
>       }
> [/code]
> 
> Any ideas why it always opens the view with the same ID even though
> the TextView "tvCheatTitle" displays a different value in every line?

Because you are not creating the actual Intent object until the TextView
is clicked, at which point your "cheat" value is the last one shown.

Also, why are you putting individual click listeners on individual
TextViews, rather than having a click listener for the ListView as a whole?

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

Android Consulting: http://commonsware.com/consulting

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to