I had done the program the display all the images from the sdcard dynamically. But now ,I want to display single image dynamically from the sdcard instead of display all images .
my coding is as follows
public class Gallery1Activity extends Activity {
// private ArrayList<String> imglist;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
ArrayList arr = new ArrayList();
super.onCreate(savedInstanceState);
setContentView(R.layout.gallerygrid);
GridView gv1=(GridView) this.findViewById(R.id.gridView1);
//gv1.setAdapter(new galleryImageAdapter(this));
arr = galldatabase();
gv1.setAdapter(new galleryImageAdapter(this,arr));
}
private ArrayList galldatabase() {
// TODO Auto-generated method stub
ArrayList ThumbsIDList = new ArrayList();
//Uri u=MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
/*String[] projection =new String[]{
Images.Thumbnails._ID,
Images.Thumbnails.DATA,
Images.Thumbnails.IMAGE_ID};*/
Cursor galleryimagecursor=managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,new String[]{
Images.Thumbnails._ID,
Images.Thumbnails.DATA} , null, null, null);
if(galleryimagecursor!=null&&galleryimagecursor.moveToFirst()){
String thumbsID;
String thumbsImageID;
String thumbsData;
int num=0;
do{
thumbsID=galleryimagecursor.getString(galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails._ID));
thumbsData=galleryimagecursor.getString(galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails.DATA));
Log.i("BMP","size "+thumbsID+" "+thumbsData);
num++;
/*if(thumbsImageID!= null) {*/
ThumbsIDList.add(thumbsID);
/*ThumbsImageIDList.add(galleryimagecursor.getString(thumbsImageIDcol));
ThumbsDataList.add(galleryimagecursor.getString(thumbsDataCol));
}*/
}
while(galleryimagecursor.moveToNext());
}
return ThumbsIDList;
}
}
then the adapter code follows
public class galleryImageAdapter extends BaseAdapter {
Context con;
private ArrayList<String> imgList;
private String thumbsID;
public galleryImageAdapter(Context c,ArrayList arr){
con=c;
imgList = arr;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return imgList.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if( convertView==null){
LayoutInflater li;
li = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=li.inflate(R.layout.galleryadapter,null);
final ImageView iv1=(ImageView)v.findViewById(R.id.galimage);
TextView tv1=(TextView)v.findViewById(R.id.galimagtext);
tv1.setText("Image"+position);
Log.d("imagevalue",imgList.get(position));
iv1.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""+imgList.get(position)/*galleryimagecursor.getColumnIndexOrThrow(Images.Thumbnails._ID)*//*imgList.get(position)*/));
}
else
v=convertView;
return v;
}
}
you have arraylist of thumb id of images using arr = galldatabase(); create another arraylist (newarr) which will have only 1 element. if you want to show 2nd image just copy thumb id of that image from arr arr and store it in new array list
example
ArrayList newarr = new ArrayList();
newarr.add(arr.get(random position));
Assign this list to adapter instead of assigning to original array list which contains list of all image
use
gv1.setAdapter(new galleryImageAdapter(this,newarr));
Related
I just want to get the value of clicked list item,
i.e if list contains apple,ball,cat, then clicked on apple then it gives value as apple, ,In the below code I am using inner class base adapter to display the data.
public class RecordsActivity extends Activity implements
OnItemClickListener
{
ListView listProduct;
ListAdapter adapter;
TextView UserName,Date;
ArrayList<String> billDate = new ArrayList<String>();
ArrayList<String> billNo = new ArrayList<String>();
ArrayList<String >partyName=new ArrayList<String>();
ArrayList<String> billAmount = new ArrayList<String>();
ArrayList<String >receipt=new ArrayList<String>();
ArrayList<String >balance=new ArrayList<String>();
ArrayList<String> week = new ArrayList<String>();
ArrayList<String> amount = new ArrayList<String>();
String get_name,get_date,Name;
String GetAmnt;
Dialog dialog;
AlertDialog alertDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_records);
UserName=(TextView) findViewById(R.id.txt_disuser);
Intent usr=getIntent();
get_name=usr.getStringExtra("user");
UserName.setText(get_name);
Date=(TextView) findViewById(R.id.txt_disdate);
Intent dt=getIntent();
get_date=dt.getStringExtra("date");
Date.setText(get_date);
Intent id =getIntent();
Name=id.getStringExtra("name");
listProduct=(ListView) findViewById(R.id.ListViewFilledRecrd);
listProduct.setAdapter(adapter);
/*listProduct.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
listProduct.setClickable(true);
String data = (String)listProduct.getItemAtPosition(arg2);
Toast.makeText(getApplicationContext(),data,
Toast.LENGTH_SHORT).show();
}
});*/
}
protected void onResume()
{
getAllProduct();
super.onResume();
}
public void getAllProduct(){
SQLiteDatabase db = this.getDB();
Cursor c;
//c=db.rawQuery("SELECT * FROM RECORDS", null);
c=db.rawQuery("SELECT * FROM RECORDS WHERE PARTY_NAME = '" + Name +
"' ", null);
String
BILL_DATE,BILL_NO,PARTY_NAME,BILL_AMOUNT,RECEIPT,BALANCE,WEEK,AMOUNT;
if(c.moveToFirst())
{
do
{
BILL_DATE = c.getString(1);
BILL_NO=c.getString(2);
PARTY_NAME=c.getString(3);
BILL_AMOUNT=c.getString(4);
RECEIPT=c.getString(5);
BALANCE=c.getString(6);
WEEK=c.getString(7);
AMOUNT=c.getString(8);
billDate.add(BILL_DATE);
billNo.add(BILL_NO);
partyName.add(PARTY_NAME);
billAmount.add(BILL_AMOUNT);
receipt.add("RECEIPT");
balance.add(BALANCE);
week.add(WEEK);
amount.add(AMOUNT);
} while (c.moveToNext());
}
DisplyRecords dsptProd=new
DisplyRecords(RecordsActivity.this,billDate,billNo,partyName,
billAmount,receipt,balance,week,amount);
listProduct.setAdapter(dsptProd);
listProduct.setOnItemClickListener(this);
c.close();
db.close();
}
private SQLiteDatabase getDB()
{
String DB_NAME = "odsr.db";
return openOrCreateDatabase(DB_NAME, SQLiteDatabase.OPEN_READWRITE,
null);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
}
}
class DisplyRecords extends BaseAdapter
{
Context c;
ArrayList<String > BILLDATE;
ArrayList<String>BILLNO;
ArrayList<String> PARTYNAME;
ArrayList<String>BILLAMOUNT;
ArrayList<String>RECEIPT;
ArrayList<String> BALANCE;
ArrayList<String>WEEK;
ArrayList<String>AMOUNT;
public DisplyRecords(RecordsActivity recordsactivity,ArrayList<String>
billDate, ArrayList<String> billNo, ArrayList<String>
partyName,ArrayList<String> billAmount, ArrayList<String> receipt,
ArrayList<String> balance, ArrayList<String> week,ArrayList<String> amount)
{
this.c=recordsactivity;
this.BILLDATE=billDate;
this.BILLNO=billNo;
this.PARTYNAME=partyName;
this.BILLAMOUNT=billAmount;
this.RECEIPT=receipt;
this.BALANCE=balance;
this.WEEK=week;
this.AMOUNT=amount;
//TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return BILLDATE.size();
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(final int pos, View row, ViewGroup parent)
{
View child=row;
LayoutInflater layoutinflater;
if(child==null)
{
layoutinflater=(LayoutInflater)
c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutinflater.inflate(R.layout.list_product,null);
}
TextView txt_billdate=(TextView) child.findViewById(R.id.txt_bill_date);
TextView txt_billno=(TextView) child.findViewById(R.id.txt_bill_no);
TextView txt_partyname=(TextView)
child.findViewById(R.id.txt_party_name);
TextView txt_billamt=(TextView)
child.findViewById(R.id.txt_bill_amount);
TextView txt_receipt=(TextView) child.findViewById(R.id.txt_receipt);
TextView txt_balance=(TextView) child.findViewById(R.id.txt_balance);
TextView txt_wk=(TextView) child.findViewById(R.id.txt_week);
TextView txt_amount=(TextView) child.findViewById(R.id.txt_amount);
txt_billdate.setText(BILLDATE.get(pos));
txt_billno.setText(BILLNO.get(pos));
txt_partyname.setText(PARTYNAME.get(pos));
txt_billamt.setText(BILLAMOUNT.get(pos));
txt_receipt.setText(RECEIPT.get(pos));
txt_balance.setText(BALANCE.get(pos));
txt_wk.setText(WEEK.get(pos));
txt_amount.setText(AMOUNT.get(pos));
// TODO Auto-generated method stub
return child;
}
}
A few assumptions here, your actual fields do not need to be ArrayList's since each query returns a single row item from the db, i create a master object with Strings to represent the data...
So here is how i would do it, you need to re structure your data
First i would change your data to a :
public class ParentObject {
public String BILLDATE ;
public String BILLNO ;
public String PARTYNAME ;
public String BILLAMOUNT ;
public String RECEIPT ;
public String BALANCE ;
public String WEEK ;
public String AMOUNT ;
}
UPDATE
then i would change my Activity to:
public class RecordsActivity extends Activity implements OnItemClickListener{
ListView listProduct;
ListAdapter adapter;
TextView UserName,Date;
// your new parent object ArrayList, much cleaner
List<ParentObject> parentObjects;
String get_name,get_date,Name;
String GetAmnt;
Dialog dialog;
AlertDialog alertDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_records);
UserName=(TextView) findViewById(R.id.txt_disuser);
Intent usr=getIntent();
get_name=usr.getStringExtra("user");
UserName.setText(get_name);
Date=(TextView) findViewById(R.id.txt_disdate);
Intent dt=getIntent();
get_date=dt.getStringExtra("date");
Date.setText(get_date);
Intent id =getIntent();
Name=id.getStringExtra("name");
listProduct=(ListView) findViewById(R.id.ListViewFilledRecrd);
listProduct.setAdapter(adapter);
listProduct.setOnItemClickListener(this);
}
protected void onResume(){
getAllProduct();
super.onResume();
}
public void getAllProduct(){
SQLiteDatabase db = this.getDB();
Cursor c;
//c=db.rawQuery("SELECT * FROM RECORDS", null);
c=db.rawQuery("SELECT * FROM RECORDS WHERE PARTY_NAME = '" + Name + "' ", null);
String BILL_DATE,BILL_NO,PARTY_NAME,BILL_AMOUNT,RECEIPT,BALANCE,WEEK,AMOUNT;
if(c.moveToFirst()){
// initialize the parent arrayList
parentObjects = new ArrayList<ParentObject>();
do{
// initialize a single object
ParentObject parentObject = new ParentObject();
// fill in its data
parentObject.BILL_DATE = c.getString(1);
parentObject.BILL_NO=c.getString(2);
parentObject.PARTY_NAME=c.getString(3);
parentObject.BILL_AMOUNT=c.getString(4);
parentObject.RECEIPT=c.getString(5);
parentObject.BALANCE=c.getString(6);
parentObject.WEEK=c.getString(7);
parentObject.AMOUNT=c.getString(8);
// add it to your list
parentObjects.add(parentObject);
} while (c.moveToNext());
}
// pass the List<ParentObject> into your adapter
DisplyRecords dsptProd= new DisplyRecords(RecordsActivity.this, (ArrayList<ParentObject>)parentObjects);
listProduct.setAdapter(dsptProd);
listProduct.setOnItemClickListener(this);
c.close();
db.close();
}
private SQLiteDatabase getDB(){
String DB_NAME = "odsr.db";
return openOrCreateDatabase(DB_NAME, SQLiteDatabase.OPEN_READWRITE,
null);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
// access to all your data through the parentObject
ParentObject data = (ParentObject) listProduct.getItemAtPosition(position);
// access all fields for this parent
Toast.makeText(RecordsActivity.this, "data: "+ data.BILL_DATE, + " ," + data.BILL_NO, " , etc", Toast.LENGTH_LONG).show();
}
}
and then update your Adapter to handle the List
class DisplyRecords extends BaseAdapter {
Context c;
List<ParentObject> parentObjects;
public DisplyRecords(Context context, ArrayList<ParentObject> parentObjects){
this.c=context;
this.parentObjects = parentObjects;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return parentObjects.size();
}
#Override
public Object getItem(int position) {
return parentObjects.get(position); // always return this parent
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(final int pos, View row, ViewGroup parent){
// You should implement the ViewHolder pattern, this is not doing that...
View child=row;
LayoutInflater layoutinflater;
if(child==null){
layoutinflater=(LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
child = layoutinflater.inflate(R.layout.list_product,null);
}
TextView txt_billdate=(TextView) child.findViewById(R.id.txt_bill_date);
TextView txt_billno=(TextView) child.findViewById(R.id.txt_bill_no);
TextView txt_partyname=(TextView) child.findViewById(R.id.txt_party_name);
TextView txt_billamt=(TextView) child.findViewById(R.id.txt_bill_amount);
TextView txt_receipt=(TextView) child.findViewById(R.id.txt_receipt);
TextView txt_balance=(TextView) child.findViewById(R.id.txt_balance);
TextView txt_wk=(TextView) child.findViewById(R.id.txt_week);
TextView txt_amount=(TextView) child.findViewById(R.id.txt_amount);
ParentObject parentObject = parentObjects.get(pos);
txt_billdate.setText(parentObject.BILLDATE);
txt_billno.setText(parentObject.BILLNO.);
txt_partyname.setText(parentObject.PARTYNAME);
txt_billamt.setText(parentObject.BILLAMOUNT);
txt_receipt.setText(parentObject.RECEIPT);
txt_balance.setText(parentObject.BALANCE);
txt_wk.setText(parentObject.WEEK);
txt_amount.setText(parentObject.AMOUNT);
// TODO Auto-generated method stub
return child;
}
}
I have created the Adapter class which extends the BaseAdapter and i also the class to query the DB. Now i am stock at connecting both result and the custom layout i Just don't know how to achieve it. If anybody could kindly help
Adapter Class
public class DrugAdapter extends BaseAdapter{
private static LayoutInflater lf = null;
private Activity activity;
private ArrayList<HashMap<String, String>> data;
public DrugAdapter(Activity a, ArrayList<HashMap<String, String>> d){
activity = a;
data = d;
lf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = lf.inflate(R.layout.single_list_item, null);
TextView title = (TextView)vi.findViewById(R.id.drug_title); // title
TextView description = (TextView)vi.findViewById(R.id.drug_description); // description
HashMap<String, String> drug = new HashMap<String, String>();
drug = data.get(position);
// Setting all values in list view
//title.setText());
//description.setText(song.get(CustomizedListView.KEY_ARTIST));
return vi;
}
}
the method in the DBHandler Class that returns the Result as a list from the DB
//Get all drugs from the DB
public List<Drug> getAllDrugs(){
List<Drug> drugList = new ArrayList<Drug>();
String selectQuery = "SELECT * FROM " + TABLE_DRUGS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Drug drug = new Drug();
drug.setID(Integer.parseInt(cursor.getString(0)));
drug.setName(cursor.getString(1));
drug.setDesc(cursor.getString(2));
// Adding contact to list
drugList.add(drug);
} while (cursor.moveToNext());
}
return drugList;
}
Drug Object Class
public class Drug {
int id;
String name;
String descri;
public Drug(int id, String name, String desc) {
// TODO Auto-generated constructor stub
this.id = id;
this.name = name;
descri = desc;
}
public Drug(String name, String desc){
this.name = name;
descri = desc;
}
public Drug() {
// TODO Auto-generated constructor stub
}
public int getID() {
// TODO Auto-generated method stub
return this.id;
}
public String getName() {
// TODO Auto-generated method stub
return this.name;
}
public String get_desc() {
// TODO Auto-generated method stub
return descri;
}
public void setID(int newId) {
// TODO Auto-generated method stub
this.id = newId;
}
public void setName(String newName) {
// TODO Auto-generated method stub
this.name = newName;
}
public void setDesc(String newDesc) {
descri = newDesc;
}
}
Please any idea how i am suppose to set the ListView Adapter..
i Have tried some few tricks like this
List k =db.getAllDrugs();
ArrayAdapter<String> adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, k);
ls.setAdapter(adap);
just show that the data is being read from the table.. that's where i get stuck
Code for the getView Method
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = lf.inflate(R.layout.single_list_item, null);
TextView title = (TextView)vi.findViewById(R.id.drug_title); // title
TextView description = (TextView)vi.findViewById(R.id.drug_description); // description
TextView id = (TextView) vi.findViewById(R.id.drug_id);
Drug drug = data.get(position);
// Setting all values in list view
title.setText(drug.getName());
description.setText(drug.get_desc());
id.setText("h");
return vi;
}
The last setText is flagging the error..
Start by replacing List k =db.getAllDrugs(); by List<Drug> k =db.getAllDrugs();
Then, your compiler will complain about your adapter being a String adapter. At this point replace
ArrayAdapter<String> adap = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, k);
with
DrugAdapter adap = new DrugAdapter(this, k);
Compiler will now complain that your list is not the right type. Change references to ArrayList<HashMap<String, String>> data in DrugAdapter with List<Drug> data.
Then properly implement getItem :
public Drug getItem(int position) {
return data.get(position);
}
In getView declare drug like so:
Drug drug = data.get(position);
Then do whatever you have to do to put the content of drug in the view.
You should study a bit more of ArrayList, you are missing something very simple.
You must set an adapter for your ListView, like this:
myListView.setAdapter(new DrugAdapter(getActivity(), getAllDrugs()));
Also, the second argument on your DrugAdapter class is wrong, if your getAllDrugs() returns List<Drug> object, your DrugAdapter constructor must be as the following:
public DrugAdapter(Activity a, ArrayList<Drug> drugList);
Your DrugAdapter class must be something like this:
public class DrugAdapter extends BaseAdapter{
private LayoutInflater lf = null;
private Activity activity;
private ArrayList<Drug> dl;
public DrugAdapter(Activity a, ArrayList<Drug> drugList){
activity = a;
dl = drugList;
lf = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return dl.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = lf.inflate(R.layout.single_list_item, null);
TextView title = (TextView)vi.findViewById(R.id.drug_title); // title
TextView description = (TextView)vi.findViewById(R.id.drug_description); // description
title.setText(dl.get(position).getName());
//use dl.get(position).yourMethodName() to get its value accordingly to your needs
}
}
Remember: this code is not performance enhanced, I didn't enhanced it because it is not what you're asking, try to figure out what you may do to improve its performance later, but first, try to understand about ArrayList.
It's not usually necessary for your custom adapter to inherit from BaseAdapter. There are many Adapter implementations in the framework that take care of a bunch of boilerplate. In your case, you should be using a CursorAdapter for performance reasons. Cursors are optimized for the platform and dumping them to arrays is wasteful for any non-tiny result.
public class DrugAdapter extends CursorAdapter {
public DrugAdapter(Context context, Cursor c) {
super(context, c);
}
#Override
public Object getItem(int position) {
// This allows us to get proper Drug objects from the adapter
Drug drug = new Drug();
drug.setID(Integer.parseInt(cursor.getString(0)));
drug.setName(cursor.getString(1));
drug.setDesc(cursor.getString(2));
return drug;
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// We only need to create the new view here
final LayoutInflater inflater = LayoutInflater.from(context);
return inflater.inflate(R.layout.single_list_item, parent);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
// This is where we populate the list item data
((TextView) view.findViewById(R.id.drug_title)).setText(cursor.getString(1));
((TextView) view.findViewById(R.id.drug_description)).setText(cursor.getString(2));
}
}
You'll need to pass the List which contains the data from the database to the Adapter. Then use the data from the list inside your getView() method. You can get rid of the HashMap I think.
I have a ListActivity extended class in that i want to display the names of cities and states. Cities should be in blue and states must be in red, i'm using a ListView and an ArrayAdapter to display the list. I've searched a lot but all I've got is using XMLs.
Anyone please help me.. Thanks in advance.
My code looks like this:
String cities[]={"....."};
String stated[]={"....."};
private ArrayList<String> list_places = new ArrayList<String>();
Private ArrayAdapter<String> list_adapter;
list_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,list_places);
for(int i=0;i<10;i++)
{
if(isCity())
/*Text in blue*/
list_adapter.add(cities[i]);
else
/*Text in red*/
list_adapter.add(states[i]);
}
setListAdapter(list_adapter);
try this adapter
public class ListColor extends BaseAdapter {
String[] items = { "Hello ", "hi", " how are you" };
Context mContext;
public ListColor(Context c) {
mContext = c;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return items.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
TextView tv = new TextView(mContext);
tv.setText(items[arg0]);
tv.setTextColor(Color.RED);
return tv;
}
}
In I am working on quiz application. In my project after the test is over I need to keep review button. When review button is clicked all the questions which appeared for test has to be displayed again with correct answer one color ,wrong answer one color and timeout answer other color. So I kept all the questions,answers and selected answer in an arraylist. Now in review page I need to display the question,options and explanation in a listview. Hence for that I have created a listview. But it showing null pointer exception.
Mycode:
public class testreview extends Activity {
private ListView listViewScore = null;
private ListViewAdapter listViewAdapter = null;
public static ArrayList<ArrayList<String>> ourstringList1 = Select.stringList1;
private ArrayList<ArrayList<String>> usernameArrLst = ourstringList1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
listViewScore=(ListView)findViewById(R.id.list);
usernameArrLst = new ArrayList<ArrayList<String>>();
listViewAdapter = new ListViewAdapter();
listViewScore.setAdapter(listViewAdapter);
}
class ListViewAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(usernameArrLst==null){
return 0;
}
return usernameArrLst.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return usernameArrLst.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView=view;
if(rowView==null){
LayoutInflater layoutinflate =LayoutInflater.from(testreview.this);
rowView=layoutinflate.inflate(R.layout.listrow, parent, false);
}
TextView textViewName=(TextView)rowView.findViewById(R.id.tv_case);
textViewName.setText((CharSequence) usernameArrLst.get(position));
return rowView;
}
}
}
Check the array size before giving to the list adapter by simply .size() method and try to print it on log. And I think not sure, Select is your another class having values stored in StringList1 ArrayList() then try print that also.
I need to display my arraylist data in listview. My arraylist is of type public static ArrayList<ArrayList<String>> ourstringList1. In my listadapter class I am trying to get the data from arraylist and setting it to tesxtview. But since I need arr.get(i).get(j)...I am unable to proceed further.
Please help me regarding this...
My code:
public class testreview extends Activity {
private ListView listViewScore = null;
private ListViewAdapter listViewAdapter = null;
public static ArrayList<ArrayList<String>> ourstringList1 = Select.stringList1;
private ArrayList<ArrayList<String>> usernameArrLst = ourstringList1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
listViewScore=(ListView)findViewById(R.id.list);
usernameArrLst = new ArrayList<ArrayList<String>>();
listViewAdapter = new ListViewAdapter();
listViewScore.setAdapter(listViewAdapter);
}
class ListViewAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(usernameArrLst==null){
return 0;
}
return usernameArrLst.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return usernameArrLst.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView=view;
if(rowView==null){
LayoutInflater layoutinflate =LayoutInflater.from(testreview.this);
rowView=layoutinflate.inflate(R.layout.listrow, parent, false);
}
TextView textViewName=(TextView)rowView.findViewById(R.id.tv_case);
textViewName.setText((CharSequence) usernameArrLst.get(position));
return rowView;
}
}
}
Thanks in advance
for this purpose i think you need to write your custom adapter and set to the list
Get The Idea
Hope this will help you.
Sorry, did not see you had imbricated ArrayList. To get the elements you need, use class casting to get to the inner ArrayList and iterate trough them.
ArrayList<ArrayList<String>> stringList;
stringList = ourStringList1;
for (int i = 0; i < stringList.size(); i++) {
ArrayList<String> innerStringList = (ArrayList<String>) stringList.get(i);
for (int j = 0; j < innerStringList.size(); j++) {
String value = (String) innerStringList.get(j);
// put the value in the textView
}
}
When you build your Adapter class, create a class attribute that will hold the array of ArrayList and initialize it in the constructor.
Hope this helps. If you need further explanation let me know.
sweety if u have showed us what you have tried then it should have been better.But as per my understanding ur code should look like this :
public class TestProjeectActivity extends Activity {
private ListView listViewScore = null;
private ListViewAdapter listViewAdapter = null;
private String[] usernameArr = null;
private ArrayList<String> usernameArrLst = null;
//private Helper helper = null;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listViewScore=(ListView)findViewById(R.id.listViewScore);
//helper = new Helper(TestProjeectActivity.this);
//usernameArr = helper.getUserName();
usernameArr = new String[]{"Alan","Bob"};
usernameArrLst = new ArrayList<String>(Arrays.asList(usernameArr));//Changed line
listViewAdapter = new ListViewAdapter();
listViewScore.setAdapter(listViewAdapter);
}
class ListViewAdapter extends BaseAdapter{
#Override
public int getCount() {
// TODO Auto-generated method stub
if(usernameArrLst==null){
return 0;
}
return usernameArr.size();
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return usernameArrLst.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View view, ViewGroup parent) {
// TODO Auto-generated method stub
View rowView=view;
if(rowView==null){
LayoutInflater layoutinflate =LayoutInflater.from(TestProjeectActivity.this);
rowView=layoutinflate.inflate(R.layout.listviewtext, parent, false);
}
TextView textViewName=(TextView)rowView.findViewById(R.id.textViewName);
textViewName.setText(usernameArr.get(position));
return rowView;
}
}
}