androidlistviewexternal-data-source

Android Studio List View


I would like to set up my code where when I click a line in my list view it returns text from a text file (from notepad or Access). Is this possible or will it need to be a different format.

This is what code I have so far, I believe it should be just one or two lines of code that will pull from a file. Here is my Main Activity Code: `

Toolbar toolbar;
ListView listView;


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

    toolbar=(Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle(getResources().getString(R.string.app_name));
    listView=(ListView) findViewById(R.id.listView);

    ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(MainActivity.this,
            android.R.layout.simple_list_item_1,
            getResources().getStringArray(R.array.Materials));

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Intent intent= new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtra("MaterialName", listView.getItemAtPosition(i).toString());
            startActivity(intent);
        }
    });
    listView.setAdapter(mAdapter);
}

Here is my Second Activity Code:

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

    mToolbar = (Toolbar) findViewById(R.id.toolbar1);
    material = (TextView) findViewById(R.id.textView);

    Bundle bundle = getIntent().getExtras();
    if(bundle != null) {
        mToolbar.setTitle(bundle.getString("MaterialName"));
        if(mToolbar.getTitle().toString().equalsIgnoreCase("4140")){
            //how do I get it to return the information from a text file
        }
    }

}`

After looking through the links in the Answers my Second Activity now looks like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    BufferedReader reader = null;

   try {
       reader = new BufferedReader(
               new InputStreamReader(getAssets().open("4140.txt")));

       String mLine;
       while ((mLine = reader.readLine()) != null) {
           text.append(mLine);
           text.append('\n');
       }
   } catch (IOException e) {
       Toast.makeText(getApplicationContext(), "Error reading file!", Toast.LENGTH_LONG).show();
       e.printStackTrace();
   } finally {
       if (reader != null) {
       try {
           reader.close();
       } catch (IOException e) {

       }
    }

    TextView output= (TextView) findViewById(R.id.toolbar1);
    output.setText((CharSequence)text);
   }

}

}

When I run the emulator, my Main Activity comes up correctly. Main Activity of my project

When I click on list value of 4140 Instead of bringing up the Second Activity with a tool bar and the textview with the text from my file it stops working.

Also I seem to have left out is that I want to link the text file to a specific list value. When I did this with images, when I clicked a list value my toolbar on the second activity repeated that list value and then depending on what my toolbar said it pulled in the correct image from my drawable file. It seems I am missing that part that will link each list value to its specific and correct text file.


Solution

  • In the second Activity you should use a Reader to get the txt file string.

    Check this q&a about reading a txt and outputing as a textview.

    About opening an Access database, that's a big topic that will involve a reader and maybe a way to run queries on it. This other question addressed the thing.

    P.S.: Sorry for adding just links, but I think they did best explaining each point.