How to set the arraylist values to listview in android? - android

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;
}
}
}

Related

How to get the float value from BaseAdapter class to activity calling BaseAdapter

I am working on an application where I insert fooditem, foodtype and foodcost into database.
And I'm able to display all these in list view using BaseAdapter. Up to this, it's working fine. I want to set sum of foodcost in Textview which is declared in my Activity class. But I am getting sum of foodcost equal to 0.0. which I calculate in BaseAdapter class.
My code Of Activity and BaseAdapter as shown.
public class ViewList extends Activity {
private List<DataArray> list;
Context mContext;
public TextView sum;
private ViewAdapter mViewAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mContext= this.getApplicationContext();
Intent mIntent= getIntent();
int DMY=(int) mIntent.getIntExtra("DMY",1111);
String TYPE=mIntent.getStringExtra("TYPE");
switch(DMY){
case 000:
list=makeQuery(null,null,null,TYPE,DMY);
break;
case 001:
String YEAR=mIntent.getStringExtra("YEAR");
list=makeQuery(null,null,YEAR,TYPE,DMY);
break;
case 011:
String YEAR1=mIntent.getStringExtra("YEAR");
String MONTH=mIntent.getStringExtra("MONTH");
list=makeQuery(null,MONTH,YEAR1,TYPE,DMY);
break;
case 111:
String YEAR2=mIntent.getStringExtra("YEAR");
String MONTH1=mIntent.getStringExtra("MONTH");
String DATE=mIntent.getStringExtra("DATE");
list=makeQuery(DATE,MONTH1,YEAR2,TYPE,DMY);
break;
default:
}
if(!list.isEmpty()){
setContentView(R.layout.view_list);
ListView mListViw = (ListView) findViewById(R.id.listView_expenditure);
sum= (TextView)findViewById(R.id.textView_sum);
mViewAdapter= new ViewAdapter(ViewList.this,list);
mListViw.setAdapter(mViewAdapter);
// I am calling getsumTotal() method to get sum of foodcost. but its return 0.0
sum.setText(String.valueOf(mViewAdapter.getSunTotal()));
}else{
ShowDialog("No record(s) found in Database");
}
}
BaseAdapter class.
public class ViewAdapter extends BaseAdapter {
public float sunTotal=0;
private static LayoutInflater mInflater;
private List<DataArray> list;
private DataArray myDataArray;
private Activity mActivity;
public ViewAdapter(Activity mActivity, List<DataArray> list) {
// TODO Auto-generated constructor stub
this.list=list;
mInflater=(LayoutInflater)mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.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 mView=convertView;
if(convertView==null)
mView=mInflater.inflate(R.layout.view_helper,parent,false);
myDataArray=list.get(position);
TextView itemName = (TextView) mView.findViewById(R.id.textView_item_name);
itemName.setText(myDataArray.getItem());
TextView itemprice = (TextView) mView.findViewById(R.id.textView_price);
String price=myDataArray.getCost();
itemprice.setText(price);
float mFloat = Float.parseFloat(price);
sunTotal=sunTotal+mFloat;// calculating sum of foodcost.
Log.d("Sum",""+sunTotal);
TextView itemDate = (TextView) mView.findViewById(R.id.textView_date);
String date=myDataArray.getDate()+" "+myDataArray.getMonth();
itemDate.setText(date);
return mView ;
}
// get method to get the total of foodcost.which i have used in activity class to get the total foodcost
public float getSunTotal() {
return sunTotal;
}
}
Thanks in advance.
Maybe you will add a getArray() method in the Adapter Class to get Data from the Adapter, and calculate the data in the Activity.
You could use the LocalBroadcastManager to broadcast the event from your adapter to your Activity:
http://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

How to Color List-items (Strings) in a ListActivity without using XMLs

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;
}
}

How to create a listview for displaying the data in android?

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.

How to Display two dimensional Array in ListView?

I Have 2D Array and this 2D Array has Strings. I would like to know How to Display the Strings in ListView?how to scroll both vertically and horizontally?
String[][] board = new String[][] {{"1","10","100"},{"hi0","1hello","test"},{"test31","test32","test43"}};
It seem to be you are asking basic things, How to use ListView. please check it you will get all about ListView.
Android ListView and ListActivity
It is to display two-d array in list view.Here's my source code in which i have implemented 2-d array in list view
My Adapter class:-
public class MyArrayAdapter extends ArrayAdapter<List>{
QuickActionDemo quickActionDemo;
public Activity context;
public List<List> list;
int CAMERA_PIC_REQUEST=10;
private int selectedPos = -1;
int clickPosition,rowPosition;
Camera camera;
private static final String TAG = "CameraDemo";
public MyArrayAdapter(Activity context,List<List> list) {
super(context,R.layout.attach_pic,list);
this.context = context;
this.list = list;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position+1;
}
static class ViewHolder {
public TextView tv1,tv2,tv3;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = null;
final ViewHolder holder = new ViewHolder();
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
rowView = inflator.inflate(R.layout.attach_pic, null);
holder.tv1 = (TextView) rowView.findViewById(R.id.defectpic);
holder.tv2 = (TextView) rowView.findViewById(R.id.no_of_uploded_pics);
holder.tv3 = (TextView) rowView.findViewById(R.id.camera);
holder.tv3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// Intent in = new Intent(getContext(),QuickActionDemo.class);
// context.startActivityForResult(in,0);
}
});
rowView.setTag(holder);
List itemVal1 = (List)getItem(position);
String st1 = (String)itemVal1.get(0);
holder.tv1.setText(st1);
List itemVal2 = (List)getItem(position);
String st2 = (String)itemVal2.get(1);
holder.tv2.setText(st2);
} else {
rowView = convertView;
((ViewHolder) rowView.getTag()).tv1.setTag(list.get(position));
((ViewHolder) rowView.getTag()).tv2.setTag(list.get(position));
((ViewHolder) rowView.getTag()).tv3.setTag(list.get(position));
}
return rowView;
}
#Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return list.size();
}
}
Here's my activity class:-
public class MyActivity extends ListActivity {
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); // to hide the virtual keyboard
setContentView(R.layout.defect_pic_listview);
try{
ArrayAdapter<List> adapter = new MyArrayAdapter(this,makeList());
setListAdapter(adapter);
}
}
private List<List> makeList(){
List<List> all = new ArrayList();
String[] newArray1 = {"Defect Picture1", "2"};
List<String> newListObject1 = Arrays.asList(newArray1);
String[] newArray2 = {"Defect Picture2","1"};
List<String> newListObject2 = Arrays.asList(newArray2);
String[] newArray3 = {"Defect Picture3","4"};
List<String> newListObject3 = Arrays.asList(newArray3);
String[] newArray4 = {"Defect Picture4","1"};
List<String> newListObject4 = Arrays.asList(newArray4);
String[] newArray5 = {"Defect Picture5","3"};
List<String> newListObject5 = Arrays.asList(newArray5);
all.add(newListObject1);
all.add(newListObject2);
all.add(newListObject3);
all.add(newListObject4);
all.add(newListObject5);
return all;
}
}
Creating a model as an inner class always works well.
Good way to store any number of items.
public class ActivityClass extends Activity {
...
ArrayList<ValuesModel> listViewValues = new ArrayList<ValuesModel>();
listViewValues.add(new ValuesModel("row title", "row details"));
ListViewAdapter listAdapter = new ListViewAdapter(this, listViewValues);
((ListView) findViewById(android.R.id.list)).setAdapter(listAdapter);
...
public class ValuesModel {
private String rowTitle;
private String rowDetails;
public ValuesModel(String rowTitle, String rowDetails) {
this.rowTitle = rowTitle;
this.rowDetails = rowDetails;
}
public String getRowTitle() {
return rowTitle;
}
public String getRowDetails() {
return rowDetails();
}
}
Then inside of your list adapter,
public class ListViewAdapter extends ArrayAdapter<ActivityClass.ValuesModel> {
private ArrayList<ActivityClass.ValuesModel> mValues;
...
#Override
public View getView(int position, View convertView, ViewGroup parent) {
...
//here whenever you need to retrieve your values, just say:
// mValues.get(position).getRowTitle();
// mValues.get(position).getRowDetails();
//if you use a viewholder pattern, you can do this:
viewHolder.rowTitle = (TextView) convertView.findViewById(R.id.row_title_textview);
viewHolder.rowTitle.setText(mValues.get(position).getRowTitle());
...
}
}

how to dynamically display the single image from the sdcard

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));

Categories

Resources