Reputation: 14565
I'm working on a project which communicate with web server and download some photos. I want these photos to be shown on a listview after the download,but it's throwing me a NullPointerException
. Here is the code I'm using :
private ArrayList <HashMap<String, Object>> items;
private static final String NAMEKEY = "bookname";
private static final String INFOKEY = "bookprice";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mystampii_layout);
}
});
ListView listView = (ListView)findViewById(R.id.mystampii_listview);
items = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hm;
final Bitmap b = BitmapFactory.decodeFile("/sdcard/MediaCard2a44bb1f782925923195ee546e7ef395.jpg", null);
ImageView brum = (ImageView) findViewById(R.id.main_img);
brum.setImageBitmap(b);
hm = new HashMap<String, Object>();
hm.put(NAMEKEY, "Moto GP 2010");
hm.put(INFOKEY, "98 Stampii");
items.add(hm);
// Define SimpleAdapter and Map the values with Row view R.layout.listbox
SimpleAdapter adapter = new SimpleAdapter(this, items, R.layout.main_listview,
new String[]{NAMEKEY,INFOKEY}, new int[]{R.id.main_img,R.id.main_name, R.id.main_info});
listView.setAdapter(adapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
}
I know, why I'm getting NullPointerException : because my ImageView brum
is placed in R.layout.main_listview
and I'm setting as Content on this activity R.layout.mystampii_layout
. I really can get the idea how to set the Image from SDCard to a ImageView from another layout.
Any suggestions how can I do that, or how can I show the downloaded image from SDCard in listview.
Thanks really for any kind of help!!!
Upvotes: 0
Views: 8890
Reputation: 14565
Create your custom Adapter like this :
import java.util.ArrayList;
import com.stampii.stampii.R;
import com.stampii.stampii.comm.rpc.CollectionRPCPacket;
import com.stampii.stampii.comm.rpc.UserDatabaseHelper;
import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
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 MyArrayAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] names;
CollectionRPCPacket rpcPacket;
Cursor cursor;
String title;
public MyArrayAdapter(Activity context, String[] names) {
super(context, R.layout.main_listview, names);
this.context = context;
this.names = names;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public ImageView imageView;
public TextView textView,textView2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder will buffer the assess to the individual fields of the row
// layout
ViewHolder holder;
// Recycle existing view if passed as parameter
// This will save memory and time on Android
// This only works if the base layout for all classes are the same
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.main_listview, null, true);
holder = new ViewHolder();
holder.textView = (TextView) rowView.findViewById(R.id.main_name);
holder.textView2 = (TextView) rowView.findViewById(R.id.main_info);
holder.imageView = (ImageView) rowView.findViewById(R.id.main_img);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
final Bitmap b = BitmapFactory.decodeFile("/sdcard/MediaCard2a44bb1f782925923195ee546e7ef395.jpg", null);
holder.textView.setText();
holder.textView2.setText("");
holder.imageView.setImageBitmap(b);
return rowView;
}
}
And you can use it like this on your activity :
ListView lv1 = (ListView)findViewById(R.id.listViewOne);
String[] names = new String[] { "ONE", "TWO", "THREE"};
lv1.setAdapter(new MyArrayAdapter(this, names));
Hope it helps!
Upvotes: 0
Reputation: 157447
Best way to achieve what you need, is to implement you custom adapter and in Adapter.getView() inflate R.layout.main_listview
Upvotes: 1
Reputation: 22066
Try simple code ::
ImageView image = (ImageView) findViewById(R.id.test_image);
FileInputStream in;
BufferedInputStream buf;
try {
in = new FileInputStream("/sdcard/test2.png");
buf = new BufferedInputStream(in);
Bitmap bMap = BitmapFactory.decodeStream(buf);
image.setImageBitmap(bMap);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}
} catch (Exception e) {
Log.e("Error reading file", e.toString());
}
Upvotes: 1