我正在使用 gridview 和布局 inflater 来显示带有文本的图标。
它应该是这样的:
我的文本文件包含以下数据:
Profile 0|Profile 1|Profile 2
我正在使用以下代码:
public View getView(final int position, View convertView, ViewGroup parent) {
View v;
//String text;
final ImageView picturesView;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"string.txt");
//StringBuilder stext = new StringBuilder();
TextView tv = (TextView)v.findViewById(R.id.icon_text);
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] columns = line.split("\\|");
for (String name : columns){
tv.setText(name);
}
//stext.append(line);
//stext.append('\n');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
tv.setTextSize(12);
tv.setTextColor(Color.BLACK);
}
else {
v = convertView;
}
}
我想用字符串值设置 TextView
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText();
我试图通过 Toast.makeText(SDCardImagesActivity.this, ""+columns.length, Toast.LENGTH_LONG).show();
检查列长度,但它显示 1(5 次)
我很困惑如何在 tv.setText 中使用列值以及列数组是否正确。
最佳答案
查看您引用的代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if(convertView==null){
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.icon, null);
TextView tv = (TextView)v.findViewById(R.id.icon_text);
tv.setText("Profile "+position);
ImageView iv = (ImageView)v.findViewById(R.id.icon_image);
iv.setImageResource(R.drawable.icon);
}
else
{
v = convertView;
}
return v;
}
代替这一行...
tv.setText("Profile "+position);
...使用以下...
tv.setText(columns[position]);
关于android - 如何从 SD 卡中检索文本并相应地设置 TextView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6174746/