我正在尝试使用android studio制作我的第一个应用程序。在主要活动中,我尝试使用RecyclerView以网格样式创建菜单。我希望每个菜单选项都有标题,描述和图像。
目前我在测试时菜单上只有3个选项。当我调试我的应用程序它有点工作但不像我预期的那样。在它加载时,它显示我的arrayList中的3个选项的标题,但不显示描述或图像。我已检查说明和图像字段是否正确填充在我的arrayList中。我不确定为什么它只显示标题?以下是我的代码。
代码 - XML
activity_main
(function($){
$('#xmlFile').unbind('change')
.bind('change', function(e){
if(this.files.length > 0){
var file = this.files[0];
if(file.type == 'text/xml'){
setTimeout(function(){
console.log('Reader');
var reader = new FileReader();
reader.addEventListener('load', function(){
console.log(reader.result);
parser = new DOMParser();
xmlDoc = parser.parseFromString(reader.result, file.type);
console.log(xmlDoc);
});
reader.readAsDataURL(file);
}, 100);
}
// $('#txtFileInfo').text('Type: '+ file.type);
}
});
})($);
menu_layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mark.spanishapp.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/esp_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
代码 - Java
MainActivity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/title"
android:textStyle="bold"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/menuImg"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
MenuEsp
package com.example.mark.spanishapp;
import android.database.sqlite.SQLiteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private final static String TAG = "MainActivity";
DBHandler dbHandler = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHandler = new DBHandler(this);
try {
dbHandler.createDataBase();
}catch (IOException ioe){
throw new Error("unable to create database");
}
try{
dbHandler.openDataBase();
}catch (SQLException sqle)
{
Log.e(TAG, sqle.getMessage());
}
RecyclerView recyclerView = (RecyclerView)findViewById(R.id.esp_menu);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 2);
recyclerView.setLayoutManager(layoutManager);
ArrayList<MenuEsp> menuList = dbHandler.Get_MenuList();
MyAdapter adapter = new MyAdapter(getApplicationContext(), menuList);
recyclerView.setAdapter(adapter);
}
}
MyAdapter
package com.example.mark.spanishapp;
public class MenuEsp {
public String getMenu() {
return menu;
}
public void setMenu(String menu) {
this.menu = menu;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
private String menu;
private String description;
private String imageName;
}
答案 0 :(得分:1)
对onBindViewHolder()
课程中的Adapter
方法进行更改
int id = this.context.getResources().getIdentifier(menuList.get(position).getImageName(), "drawable", this.context.getPackageName());
holder.img.setImageResource(id);
holder.descirption.setText(menuList.get(position).getDescription());
答案 1 :(得分:1)
在onBindViewholder
中设置描述值
即
holder.descirption.setText(menuList.get(position).getDescription());
下, 从
更改代码int id = this.context.getResources().getIdentifier(menuList.get(position).getImageName(), "drawable", this.context.getPackageName());
holder.img.setImageResource(id);
要
holder.img.setImageResource(Integer.parseInt(menuList.get(position).getImageName()));