即時創建一個網站的應用程序,bayfiles.net 我有一個工作列表視圖接收JSON數據。這是工作正常的文件名和大小,現在我也有一個ImageView。我想確定它應該放在圖標架中的哪個圖標。這取決於文件名的結尾。例如以「.png」結尾的文件名應該顯示帶有「png」文字的圖標。怎麼做?根據文件擴展名在ArrayList中設置ImageView圖像
這是我的工作積極性:
public class FilesActivity extends SherlockActivity {
EditText editsearch;
fileObjectAdapter adapter;
String httpurl, sessionId, filetype;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dblist);
ActionBar abs = getSupportActionBar();
abs.setDisplayHomeAsUpEnabled(false);
abs.setTitle("BAYFILES");
//abs.setIcon(R.drawable.abs_logo);
abs.setDisplayShowCustomEnabled(true);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
View customNav = LayoutInflater.from(this).inflate(R.layout.actionbar, null);
abs.setCustomView(customNav, lp);
abs.setDisplayShowCustomEnabled(true);
abs.setDisplayOptions(abs.DISPLAY_SHOW_HOME|abs.DISPLAY_SHOW_CUSTOM);
abs.setDisplayShowTitleEnabled(true);
Crouton.makeText(this, "You have been signed in!", Style.CONFIRM).show();
ImageButton btnSearch = (ImageButton) findViewById(R.id.action_search);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("Button Click","position");
}
});
Context context = getApplicationContext();
AppPrefs appPrefs = new AppPrefs(context);
sessionId = appPrefs.getSessionId();
newRequest("http://api.bayfiles.net/v1/account/files?session=" + sessionId);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.menu, (com.actionbarsherlock.view.Menu) menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId())
{
case R.id.refresh:
newRequest("http://api.bayfiles.net/v1/account/files?session=" + sessionId);
return true;
case R.id.settings:
Intent startNewActivityOpen = new Intent(FilesActivity.this, AccountActivity.class);
startActivityForResult(startNewActivityOpen, 0);
return true;
case R.id.upload:
Crouton.makeText(this, "Coming soon!", Style.INFO).show();
return true;
case R.id.link:
Crouton.makeText(this, "Add link from BayFiles", Style.INFO).show();
return true;
case R.id.folder:
Crouton.makeText(this, "Not available yet!", Style.INFO).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
switch(item.getItemId()){
case R.id.info:
Crouton.makeText(this, "Coming soon!", Style.INFO).show();
Log.d("log_tag", "Info");
break;
case R.id.delete:
new RequestTask().execute(httpurl);
newRequest("http://api.bayfiles.net/v1/account/files?session=" + sessionId);
break;
}
return super.onContextItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflate = getMenuInflater();
inflate.inflate(R.menu.context_menu, menu);
}
public static String readableFileSize(long size) {
if(size <= 0) return "0";
final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
int digitGroups = (int) (Math.log10(size)/Math.log10(1024));
return new DecimalFormat("#,##0.#").format(size/Math.pow(1024, digitGroups)) + " " + units[digitGroups];
}
public void newRequest(String url){
String response = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
ResponseHandler <String> resonseHandler = new BasicResponseHandler();
HttpPost postMethod = new HttpPost(url);
try {
JSONObject json = new JSONObject();
postMethod.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
postMethod.setHeader("Content-Type", "application/json");
response = httpClient.execute(postMethod,resonseHandler);
JSONObject request = new JSONObject(response);
ArrayList<fileObject> objectList = new ArrayList<fileObject>();
for (Iterator<?> keyIterator = request.keys(); keyIterator.hasNext();) {
String key = (String) keyIterator.next();
JSONObject object = request.optJSONObject(key);
if (object != null) {
long l = Long.parseLong(object.getString("size"));
String size = readableFileSize(l);
String[] filetype = object.getString("filename").split("\\.");
Log.d("log_tag", filetype[1]);
fileObject obj = new fileObject();
obj.setFileId(key);
obj.setFileName(object.getString("filename"));
obj.setSize(size);
obj.setInfoToken(object.getString("infoToken"));
obj.setDeleteToken(object.getString("deleteToken"));
obj.setSha1(object.getString("sha1"));
obj.setFileType(filetype[1]);
objectList.add(obj);
}
}
final ListView lv1 = (ListView) findViewById(R.id.listobjects);
lv1.setAdapter(new fileObjectAdapter(this, objectList));
lv1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
Object o = lv1.getItemAtPosition(position);
fileObject fullObject = (fileObject) o;
Log.d("log_tag", "Filename: " + fullObject.getFileName());
httpurl = "http://bayfiles.net/file/" + fullObject.getFileId() + "/" + fullObject.getInfoToken() + "/" + fullObject.getFileName();
}
});
lv1.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position, long id) {
registerForContextMenu(lv1);
openContextMenu(lv1);
Object o = lv1.getItemAtPosition(position);
fileObject fullObject = (fileObject) o;
httpurl = "http://api.bayfiles.net/v1/file/delete/" + fullObject.getFileId() + "/" + fullObject.getDeleteToken();
return true;
}
});
}
catch(Exception e)
{
e.printStackTrace();
Log.d("log_tag", "Error: " + e.toString());
}
}
}
我的文件對象:
package com.spxc.bayfiles.adapter;
public class fileObject {
private String fileName;
private String size;
private String infoToken;
private String deleteToken;
private String sha1;
private String fileId;
private String fileType;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public String getInfoToken() {
return infoToken;
}
public void setInfoToken(String infoToken) {
this.infoToken = infoToken;
}
public String getDeleteToken() {
return deleteToken;
}
public void setDeleteToken(String deleteToken) {
this.deleteToken = deleteToken;
}
public String getSha1() {
return sha1;
}
public void setSha1(String sha1) {
this.sha1 = sha1;
}
public String getFileId() {
return fileId;
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
public String getFileType(){
return fileType;
}
public void setFileType(String fileType){
this.fileType = fileType;
}
}
而且我adaper:
public class fileObjectAdapter extends BaseAdapter {
private static ArrayList<fileObject> searchArrayList;
private LayoutInflater mInflater;
public fileObjectAdapter(Context context, ArrayList<fileObject> results) {
searchArrayList = results;
mInflater = LayoutInflater.from(context);
}
public int getCount() {
return searchArrayList.size();
}
public Object getItem(int position) {
return searchArrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.dbitems, null);
holder = new ViewHolder();
holder.txtFileName = (TextView) convertView.findViewById(R.id.size);
holder.txtSize = (TextView) convertView.findViewById(R.id.filename);
holder.imgIcon = (ImageView) convertView.findViewById(R.id.typeimg);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtFileName.setText(searchArrayList.get(position).getFileName());
holder.txtSize.setText(searchArrayList.get(position).getSize());
//holder.imgIcon.setImageResource(SET DRAWABLE ACCORDING TO ENDING);
return convertView;
}
static class ViewHolder {
TextView txtFileName;
TextView txtSize;
ImageView imgIcon;
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
searchArrayList.clear();
if (charText.length() == 0) {
searchArrayList.addAll(searchArrayList);
}
else
{
for (fileObject wp : searchArrayList)
{
if (wp.getFileName().toLowerCase(Locale.getDefault()).contains(charText))
{
searchArrayList.add(wp);
}
}
}
notifyDataSetChanged();
}
}
正如你所看到的,我得到了完整的文件名和分裂它分成兩塊。文件名和擴展名。然後我會做一個if語句來檢查它是哪個文件擴展名。然後我希望它根據文件擴展名更改列表項中的圖標。如何才能做到這一點?
任何幫助非常感謝!
'switch(fileType)'這裏太簡單了嗎? – Geobits
我這麼認爲是因爲我必須確定適配器 –
中的圖像,您可以在資源文件夾中預先製作圖標,例如jpg_icon,png_icon等(然後您將設置圖像可繪製資源 - http:// stackoverflow.com/questions/7799752/android-java-set-image-resource),或者您可以使用TextView並根據圖像類型動態設置文本。 – droideckar