如何在布局的文本字段中显示.txt文件中的内容?

时间:2015-11-27 07:01:55

标签: android android-layout android-fragments

listView.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> listView, View view,
                                    int position, long id) {
                // Get the cursor, positioned to the corresponding row in the result set
                Cursor cursor = (Cursor) listView.getItemAtPosition(position);

                // Get the state's capital from this row in the database.
                String carcompany =
                        cursor.getString(cursor.getColumnIndexOrThrow("carcompany"));
                /*Toast.makeText(getApplicationContext(),
                        countryCode, Toast.LENGTH_SHORT).show();*/
                /*Intent intent = new Intent(AndroidListViewCursorAdaptorActivity.this, MainPage.class);
                startActivity(intent);*/
                switch (carcompany) {
                    case "Benz": Intent intentBez = new Intent(AndroidListViewCursorAdaptorActivity.this,MainPage.class);
                        AndroidListViewCursorAdaptorActivity.this.startActivity(intentBez);
                        break;
                    case "Ford":
                        Intent intentFord = new Intent(AndroidListViewCursorAdaptorActivity.this, AudiPage.class);
                        AndroidListViewCursorAdaptorActivity.this.startActivity(intentFord);
                        break;
                    case "Audi":
                        Intent intentAudi= new Intent(AndroidListViewCursorAdaptorActivity.this, AudiPage.class);
                        AndroidListViewCursorAdaptorActivity.this.startActivity(intentAudi);
                        break;
                }


}

我使用实例carcompany.so获取值,每个切换案例都调用new activity。因此,我希望使用资产文件夹形式的.txt文件来获取值,而不是这种方法。

1 个答案:

答案 0 :(得分:0)

\试试这个

                StringBuilder text = new StringBuilder();

                try {
                    BufferedReader br = new BufferedReader(new FileReader(assetpath+"Carcompany.txt"));
                    String line;

                    while ((line = br.readLine()) != null) {
                        text.append(line);
             Toast.makeText(getApplicationContext(), text.toString(),Toast.LENGTH_LONG).show();
                    }
                    br.close();
                }
                catch (IOException e) {
                    //You'll need to add proper error handling here
                }