I am reading .txt files from Asset
Folder by Passing a String which is in the previous activity list item.
Let us consider,Recyclerview
item size in 5.
- Android -- Item 1
- IOS -- Item 2
- BlackBerry -- Item 3
- Symbian -- Item 4
- Bada -- Item 5
When I click Android,the "Android" String is passed through an Intent and Assigned to the variable of Song.
InputStream inputstream = getResources().getAssets().open(***Song*** + ".txt");
and reads the Android.txt file from the Asset Folder. Only the first item is called. When I click other items in the list, intent
String passed to next activity, but did not open the respective .txt file from folder. It jumped to catch.
Here is my code.
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 ---> Prints all in the 1st item click
Except clicking of 1st item, print only the following in LogCat.
System.out: .....1
System.out: .....2
Adapter Class at where I click the RecyclerView
item,
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:
After changing code like the following getting Error in 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;
}
at hitesh.androidjsonapp.MainActivity.loadJSONFromAsset(MainActivity.java:40) pointed this line
InputStream is = getAssets().open(Song + ".txt");