0

I have an activity with a ListView in that ListView I had written a certain question. Now I just want that by clicking on each question respectively it will retrieve some selected data from the text file that I had created and shown that answer to some other activity.

Anyone please help me and provide me the code.

public class MainActivity4 extends AppCompatActivity implements AdapterView.OnItemClickListener {

  String[] name={"Tell me about yourself ?"
        ,"Describe yourself in one word ?",
        "Why did you leave your last job ?",
        "What is your strength"};

  ListView lv;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity4_main);

    lv=(ListView)findViewById(R.id.listView);
    ArrayAdapter adapter =new ArrayAdapter(this,android.R.layout.simple_list_item_1,name);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(this);
  }

  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(getApplicationContext(), parent.getItemAtPosition(position)+"", Toast.LENGTH_LONG).show();

  }
}
ישו אוהב אותך
  • 28,609
  • 11
  • 78
  • 96
Braj Ankit
  • 333
  • 1
  • 2
  • 19
  • 3
    Welcome to stack overflow :-) Please look at How to [ask](http://stackoverflow.com/help/how-to-ask) . This will help to get useful answers – Nisarg Jul 21 '16 at 08:33
  • Please provide some code what have you tried so far ?? – Nisarg Jul 21 '16 at 08:47
  • In the above code i have simply used Toast now I want , when i click on the first question it will get answer from a text file that i had created and show answer to some other activity. – Braj Ankit Jul 21 '16 at 09:00
  • Please go through [this](http://stackoverflow.com/a/2091482/3117966) – Nisarg Jul 21 '16 at 09:11
  • so for the value that i have to pass , i must have to make separate file. But i don't want to make separate answer file for each question that i had mentioned in my list . want i want is ,when i click at position 0 in my list ,it will select a required portion of the answer from the text file and so to another activity .As i had written all answer to the question in on single text file. – Braj Ankit Jul 21 '16 at 09:38
  • I dont get you what you want to achieve – Nisarg Jul 21 '16 at 11:07
  • you need to set all answers in a file, if an answer contains `newline` then you will need to use some custom line separator, then at activity create, load that file into array, where each array item is an answer to the corresponding question. i.e: `position` param in `onItemClick()` will be used to get the answer from the file lines array `answers[position]`. then pass it to 2nd activity using `Intent` – Yazan Jul 21 '16 at 12:18

0 Answers0