Skip to content
Snippets Groups Projects
Commit 0be18a27 authored by Maxime Graulich's avatar Maxime Graulich
Browse files

Android: add icon in models list

parent 01725456
No related branches found
No related tags found
No related merge requests found
package org.geuz.onelab;
import java.io.File;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
class Model {
private String _name, _summary;
private File _file;
private Bitmap _bitmap;
public Model(String name, String summary, File file){
_name = name;
_summary = summary;
_file = file;
}
public String getName() {
return _name;
}
public String getSummary() {
return _summary;
}
public File getFile() {
return _file;
}
public Bitmap getBitmap() {
return _bitmap;
}
public void setBitmap(File f) {
_bitmap = BitmapFactory.decodeFile(f.toString());
}
}
package org.geuz.onelab;
import java.util.List;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ModelArrayAdapter extends ArrayAdapter<Model> {
private List<Model> _models;
public ModelArrayAdapter(Context c) {
super(c, R.layout.model);
_models = new ArrayList<Model>();
}
@Override
public void add(Model model) {
super.add(model);
_models.add(model);
}
public Model getModel(int pos) {
return _models.get(pos);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) parent.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.model, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.title);
TextView description = (TextView) rowView.findViewById(R.id.description);
ImageView icon = (ImageView) rowView.findViewById(R.id.icon);
if(_models.get(position).getName() != null) title.setText(_models.get(position).getName());
if(_models.get(position).getSummary() != null) description.setText(_models.get(position).getSummary());
if(_models.get(position).getBitmap() != null) icon.setImageBitmap(_models.get(position).getBitmap());
else icon.setImageResource(R.drawable.ic_launcher);
icon.setPadding(10, 10, 10, 10);
return rowView;
}
}
......@@ -15,7 +15,6 @@ import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
......@@ -30,20 +29,17 @@ import android.widget.AdapterView.OnItemClickListener;
public class ModelList extends Activity {
private Models modelList;
private ModelArrayAdapter _modelArrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.modelList = new Models();
_modelArrayAdapter = new ModelArrayAdapter(this);
try {
this.getModels();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
......@@ -60,18 +56,17 @@ public class ModelList extends Activity {
SDFileChooser f = new SDFileChooser();
}
});
list.addFooterView(loadSD);
list.setAdapter( new ModeleArrayAdapter(this, modelList));
//TODO list.addFooterView(loadSD);
list.setAdapter(_modelArrayAdapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
int model = (position < modelList.size())? position : 0;
Model m = _modelArrayAdapter.getModel(position);
Intent intent = new Intent(ModelList.this, MainActivity.class);
intent.putExtra("model", model);
intent.putExtra("file", modelList.getFile(model));
intent.putExtra("name", modelList.getName(model));
intent.putExtra("model", position);
intent.putExtra("file", m.getFile());
intent.putExtra("name", m.getName());
startActivity(intent);
}
});
......@@ -116,6 +111,7 @@ public class ModelList extends Activity {
String title = null;
String summary = null;
String file = null;
String bitmap = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) continue;
String name = parser.getName();
......@@ -138,8 +134,19 @@ public class ModelList extends Activity {
parser.nextTag();
}
}
else if(name.equals("preview")) {
if (parser.next() == XmlPullParser.TEXT) {
bitmap = parser.getText();
parser.nextTag();
}
}
else {
skipTag(parser);
}
}
modelList.addModel(title, summary, dir+"/"+file);
Model newModel = new Model(title, summary, new File(dir+"/"+file));
if(bitmap != null) newModel.setBitmap(new File(dir+"/"+bitmap));
_modelArrayAdapter.add(newModel);
}
private void skipTag(XmlPullParser parser) throws XmlPullParserException, IOException {
if (parser.getEventType() != XmlPullParser.START_TAG) {
......
package org.geuz.onelab;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ModeleArrayAdapter extends ArrayAdapter<String> {
private Context context;
private String[] titres;
private String[] descriptions;
public ModeleArrayAdapter(Context context, String[] titres) {
super(context, R.layout.model, titres);
this.context = context;
this.titres = titres;
}
public ModeleArrayAdapter(Context context, String[] titres, String[] descriptions) {
this(context,titres);
this.descriptions = descriptions;
}
public ModeleArrayAdapter(Context context, Models models) {
super(context, R.layout.model, models.getNames());
this.context = context;
titres = new String[models.size()];
descriptions = new String[models.size()];
for(int i=0;i < models.size();i++) {
titres[i] = models.getName(i);
descriptions[i] = models.getSummary(i);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.model, parent, false);
TextView Title = (TextView) rowView.findViewById(R.id.title);
TextView Description = (TextView) rowView.findViewById(R.id.description);
ImageView Icon = (ImageView) rowView.findViewById(R.id.icon);
if(this.titres != null) Title.setText(this.titres[position]);
if(this.descriptions != null) Description.setText(this.descriptions[position]);
if(Icon != null) Icon.setImageResource(R.drawable.ic_launcher);
Icon.setPadding(10, 10, 10, 10);
return rowView;
}
}
package org.geuz.onelab;
import java.util.ArrayList;
import java.util.List;
class Models {
private List<String> _name;
private List<String> _summary;
private List<String> _file;
public Models(){
_name = new ArrayList<String>();
_summary = new ArrayList<String>();
_file = new ArrayList<String>();
}
public int size() {
return _name.size();
}
public String getName(int pos) {
return _name.get(pos);
}
public String[] getNames() {
String[] ret = new String[_name.size()];
ret = _name.toArray(ret);
return ret;
}
public String getSummary(int pos) {
return _summary.get(pos);
}
public String getFile(int pos) {
return _file.get(pos);
}
public void addModel(String name, String summary, String file){
_name.add(name);
_file.add(file);
_summary.add(summary);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment