Я читаю TXT-файлы из Asset
Папка, передавая строку, которая находится в предыдущем элементе списка действий.Чтение .txt-файла из папки Asset
Рассмотрим Recyclerview
размер элемента в 5
- Android - Пункт 1
- IOS - Пункт 2
- BlackBerry - Item 3
- Symbian - Пункт 4
- Bada - поз. 5
Когда я нажимаю Android, строка «Android» передается через Intent и Назначается переменной Песня.
InputStream inputstream = getResources().getAssets().open(***Song*** + ".txt");
и считывает файл Android.txt из папки активов. Вызывается только первый элемент. Когда я нажимаю другие элементы в списке, intent
Строка передается следующему действию, но не открывает соответствующий .txt-файл из папки. Он прыгнул, чтобы поймать.
Вот мой код.
Intent i = getIntent();
Song = i.getExtras().getString("Title");
textView.setText(Song);
System.out.println(".....1");
try {
System.out.println(".....2");
InputStream inputstream = getResources().getAssets().open(Song + ".txt");
System.out.println(".....3");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputstream));
String m = reader.toString();
StringBuilder total = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
total.append(line);
}
System.out.println(".....4");
m = total.toString();
System.out.println(".....5" + m);
textView.setText(m);
textView.setTextColor(Color.BLACK);
System.out.println(".....6");
} catch (IOException ex) {
System.out.println(".....7" + ex);
}
System.out.println ---> Печать всех в 1-ом пункте нажмите
За исключением нажатия 1-го пункта, печать только в следующем LogCat.
System.out: ..... 1
System.out: ..... 2
адаптер класса в котором я нажимаю RecyclerView
элемент,
public class SongAdapter extends RecyclerView.Adapter<SongAdapter.ViewHolder> {
Context con;
ArrayList<String> songHeading = new ArrayList<>();
SharedPreferences pref;
String MyPREFERENCES = "Preference";
public SongAdapter(Context context, ArrayList<String> heading) {
this.con = context;
this.songHeading = heading;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.song_list_item, parent, false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
pref = con.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
holder.txtSong.setText(songHeading.get(position));
holder.Clicker.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(view.getContext(), MainActivity.class);
i.putExtra("Title", songHeading.get(position));
view.getContext().startActivity(i);
}
});
}
@Override
public int getItemCount() {
return songHeading.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public TextView txtSong;
LinearLayout Clicker;
public ViewHolder(View itemView) {
super(itemView);
mView = itemView;
txtSong = (TextView) itemView.findViewById(R.id.textView);
Clicker = (LinearLayout) itemView.findViewById(R.id.Clicker);
}
}
}
UPDATE:
После изменения кода, как на следующий получаю ошибку в Verbose
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song_list_item2);
context = this;
pref = this.context.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
textView = (TextView) findViewById(R.id.textView);
Intent i = getIntent();
Song = i.getExtras().getString("Title");
textView.setText(Song);
System.out.println(".....1");
textView.setText(loadJSONFromAsset());
}
private String loadJSONFromAsset() {
String json = null;
try {
InputStream is = getAssets().open(Song + ".txt");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}
в hitesh.androidjsonapp .MainActivity.loadJSONFromAsset (MainActivity.java:40) указал эту линию
InputStream is = getAssets().open(Song + ".txt");
Почему вы не используете ресурс массива строк? –
Мне нужно прочитать более 1000 файлов из папки Assert. –
Вы не упомянули об этом. –