我正在学习android,我正在尝试创建一个应用程序,它将以recyclelerView的形式显示* .txt文件中的所有单词。我发现的大部分教程都是对要显示的元素(图像,字符串等)进行硬编码。但是,我的.txt文件有很多单词,我不能在代码中键入所有这些(这需要几天)。请帮忙。
对于我的应用我有2个片段。第一个片段用于列出所有单词,第二个片段用于在选择单词时显示带有单词的句子。
首先我创建了一个包含数据的类(如果我没错!)
public class showData {
String title;
String content;
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}}
然后我创建了一个viewAdaptar类
public class viewAdapter extends RecyclerView.Adapter<viewAdapter.myViewHolder>{
private LayoutInflater inflater;
List<showData> data = Collections.emptyList();
//inflate layout for recycle view
public viewAdapter(Context context){
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public myViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.custom_view, parent,false);
myViewHolder holder = new myViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(myViewHolder holder, int position) {
showData current = data.get(position);
holder.holdtitle.setText(current.title);
holder.holdcontent.setText(current.content);
}
@Override
public int getItemCount() {
return data.size();
}
class myViewHolder extends RecyclerView.ViewHolder {
TextView holdtitle;
TextView holdcontent;
public myViewHolder(View itemView) {
super(itemView);
holdtitle = (TextView) itemView.findViewById(R.id.title);
holdcontent = (TextView)itemView.findViewById(R.id.content);
}
}}
我想在片段1上显示列表 我的fragment1类最初在ListView的帮助下显示了单词 InputStream类。
public class Fragment1 extends Fragment {
private RecyclerView recyclerView;
private viewAdapter adapterView;
ListListener sentenceActivity;
public interface ListListener{
public void displayWord(String text,InputStream res);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
sentenceActivity = (ListListener) activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString());
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//create view of the first fragment
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerView);
//open the file
final InputStream inputStream = getResources().openRawResource(R.raw.android_dev_agreement);
//get each word of the file with the help of the getWord method
//Set<String> words = getWords(inputStream);
//variable to link to the listview in the fragment1
//final ListView myList = (ListView) view.findViewById(R.id.list);
//create an array list for storing all the words processed from the file
//ArrayList<String> items = new ArrayList<>();
//add all the words into the ArrayList
//items.addAll(words);
//Convert the ArrayList into viewable items that can be added to the listview
//ArrayAdapter<String> adapter = new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_list_item_1, items);
//populate the listview
//myList.setAdapter(adapter);
//set click listener for item in the Adapter array
/*myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
try {
inputStream.reset();
} catch (IOException e) {
e.printStackTrace();
}
String text = myList.getItemAtPosition(position).toString();
sentenceActivity.displayWord(text, inputStream);
}
});*/
return view;
}
private Set<String> getWords(InputStream res){
BufferedReader br=new BufferedReader(new InputStreamReader(res));
String line;
Set<String> words = new HashSet<String>(2000);
try{
while ((line = br.readLine()) != null) {
String[] wordsOnLine = line.split("[\\p{Punct}\\s}]");
for (String w : wordsOnLine) {
if (w.trim().length() < 2)
continue;
words.add(w.toLowerCase());
}
}
}catch (IOException iox){
words = null;
}
return words;
}}
Listview的片段就是这个,它成功生成了列表视图。但现在我想将它改为RecyclerView。
请建议如何做到这一点。