我目前正在编写“net.superlinux.tcltktutorials”,您现在可以在 Play 商店中找到它。现在在应用程序中,我想使用使用语言环境的标准方式,而不是使用语言环境的方式。我必须添加阿拉伯语/res/vlaues-ar,并且我有英语/res/values 的默认值。该应用程序是关于基于广告的 YouTube 播放列表 TCL/Tk 编程语言教程。现在播放列表可以是阿拉伯语和英语。我注意到的是,如果您在默认的 /res/values 36 个条目和 /res/values-ar 35 个条目中用于相同的播放列表,这将导致 ResourceNotFound 异常。您所要做的就是在列表底部将缺少的条目添加为空,以使其在英语和阿拉伯语中的数字相等,即使英语播放列表的数字较少。
这是我添加到列表活动中形成的播放列表的方法,也是将资源用作 xml 内置数据的巧妙方法:
package net.superlinux.tcltktutorials;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class ListOfVideos extends ListActivity {
List<String> model = new ArrayList<String>();
ArrayAdapter<String> adapter = null;
List<String> filename = new ArrayList<String>();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
adapter = new ArrayAdapter<String> (this, R.layout.list_item, model);
load_playlist();
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String selected_youtube_video=filename.get(position);
try {Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"+selected_youtube_video));
startActivity(i);
}
catch(Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v="+selected_youtube_video+"&loop=1&autoplay=1")));
e.printStackTrace();
}
}
void load_playlist()
{
int display_id=0;
int file_id=0;
//loop forever until nothing has to be added to the ListView or stop if the list item
// to be added does not exist.
for (int i=0;;i++){
display_id=getResources().getIdentifier("display_"+i, "string", getPackageName());
if (display_id!=0 && getString(display_id).length()!=0)
adapter.add(getString(display_id));
else {
Log.e("string id not found or empty","R.string.display_"+i );
return;
}
file_id=getResources().getIdentifier("file_"+i, "string", getPackageName());
if (file_id!=0 && getString(file_id).length()!=0){
filename.add(getString(file_id));
}
else {
Log.e("string id not found or empty","R.string.file_"+i );
return;
}
}
}
}