1

In my application there are list view, after getting the list, when a particular item is clicked it crashes. Please help I am new in android. The code the causes error I have marked it with special character "hash"

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewClass extends ListActivity {
TextView clickedView;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // storing string resources into Array
    String[] adobe_products =     getResources().getStringArray(R.array.adobe_products);

    // Binding resources Array to ListAdapter
    this.setListAdapter(new ArrayAdapter<String>(this,   R.layout.list_item, R.id.label, adobe_products));


    ListView lv = getListView();
 // listening to single list item on click
    lv.setOnItemClickListener(new OnItemClickListener() {



      public void onItemClick(AdapterView<?> parent, View args1, int position, long id) {
          // selected item 
         ################################################ 
         String product = ((TextView) args1).getText().toString();
         ################################################ 


          // Launching new Activity on selecting single List Item
          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          // sending data to new activity
          i.putExtra("product", product);
          startActivity(i); 




      }
    });       
}
}
settaratici
  • 310
  • 1
  • 7
  • 18
  • 2
    What is the error? Since I don't know the error, I guess it might be your casting to TextView. – neo Dec 24 '15 at 17:48
  • try this http://stackoverflow.com/a/17989959/5202007 or this may help http://stackoverflow.com/a/13405692/5202007 – Mohammad Tauqir Dec 24 '15 at 18:21
  • If you want proper help from me or anyone here please post your log cat error message so that we can know actual reason behind the crash. – vjs3 Dec 24 '15 at 19:47

2 Answers2

2

Replace that line with

String product = ((TextView) args1.findViewById(R.id.label)).getText().toString();
frogatto
  • 28,539
  • 11
  • 83
  • 129
  • @SandipMondal I'm glad I could help you. Please mark my answer as [accepted](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). – frogatto Dec 28 '15 at 07:21
  • Sorry I am new in here and my reputation is below 15, so I am no eligible to mark your answer. Once I reach 15, I am surely going to mark your answer as accepted.Thank You Frogatto... – Sandip Mondal Dec 28 '15 at 07:38
  • @SandipMondal No, please just tick my answer. (the tick below the votes buttons) – frogatto Dec 28 '15 at 07:40
0

actually you have array elements, why do you try to get value from textview. You can get the selected item text with adobe_products[position].

lv.setOnItemClickListener(new OnItemClickListener() {

      public void onItemClick(AdapterView<?> parent, View args1, int position, long id) {
          // selected item 
         ################################################ 
         String product = adobe_products[position] ;//Editted area
         ################################################ 

          // Launching new Activity on selecting single List Item
          Intent i = new Intent(getApplicationContext(), SingleListItem.class);
          // sending data to new activity
          i.putExtra("product", product);
          startActivity(i); 
      }
    });       
settaratici
  • 310
  • 1
  • 7
  • 18