Ok so i sucsessfully gotten my names from my database, and now i've stored them in the array result[], and at the end of the code i've written return result; now my question, how do i get the names from my result[] to appear in a listview? note: i do not want to have the connecting to mysql database on the same class as my listview one.
How would i go about doing this?
I also wish to know how i would go about making a custom listview with the names and a picture on the side. Also if you could tell me how i would go about adding pictures to listviews aswell, that'd be great :) i'm going to be using bmp, retriving them from database as byte[] and then convert them to bmp.
note: fairly new to java and android development
You can find a lot of examples on internet.
A simple example would be:
ListView listView = (ListView) findViewById(R.id.listOne);
String[] name={"HELLO","THIS","IS","NABIN"};
String[] phone={"12","24","36","48"};
ArrayList<HashMap<String,String>> obj= new ArrayList<HashMap<String,String>>();
for(int i=0;i<name.length;i++){
HashMap<String,String> toFill = new HashMap<String,String>();
toFill.put("name", name[i]);
toFill.put("phone", phone[i]);
obj.add(toFill);
}
//to define adapter
ListAdapter adapter = new SimpleAdapter(MainActivity.this, obj, R.layout.contact, new String[] {"name", "phone"},new int[] {R.id.etName,R.id.etPhone});
listView.setAdapter(adapter);
Edited:
For images to display:(Considering your images are in drawable folder)
You have to make a custom adapter
And for imageView of custom adapter do the following:
String imageName = ......// get text from your array which would be the name of image.
int resID = getResources().getIdentifier(imageName , "drawable", getPackageName());
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageResource(resId);
Related
I am trying to put random items from an array and insert them into a listview, at the moment the listview is just coming up blank. Ideally I would like to be able to retrieve what position from the array the list item is if it is clicked on too
String[] levelOneListList = new String[] {
"Daisy", "Rock", "Tree", "Dandelion", "Grass"
};
Random r=new Random();
int randomNumber=r.nextInt(levelOneListList.length);
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_checked, randomNumber);
The listview is now empty on the emulator
Try to take a look at this page. It's a good Tutorial on how to use Listviews with ArrayAdapter.
https://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView
I am creating an app where I will be loading some images and data from a database, it should look like this:
Image______Name of user
Image______Name of user
Image______Name of user
etc..
I tried to create it just with a dummy image and some text to figure out how it works.
I create a LinearLayout, ImageView and a TextView, I add those two to the LinearLayout, and than I add that LinearLayout to a RelativeLayout.
The problem is, that all the images and text are placed in the same place, on top of each other. How can I change it so it is in the format I need?
relativeLayout = (RelativeLayout) findViewById(R.id.rel);
for(int i = 0; i< 30; i++)
{
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
TextView hello = new TextView(this);
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.mipmap.ic_launcher);
String hi = "Hey";
if(i == 0){hi = "Hello0";}
if(i == 2){hi = "Hello2";}
if(i == 3){hi = "Hello3";}
if(i == 4){hi = "Hello4";}
hello.setText(hi);
layout.addView(imageView);
layout.addView(hello);
relativeLayout.addView(layout);
}
I am using a for to loop it a few times just for test.
Instead of RecyclerView, add the items in a LinearLayout. You can also set position where to add the new item in the LinearLayout.
I would suggest you do instead is:
create a model object for the user details(name and picture)
Use ListView or RecyclerView with a simple adapter add items to an
ArrayList of model object and notify the adapter when data is
changed.
This way you'll be reusing the views, and that'll improve the performance much better.
for examples, you can take a look at these sample projects.
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/ListView
https://github.com/lokeshsaini94/SimpleAndroidExamples/tree/master/RecyclerView
I am new to Android App Development. I am try to view my database using a Textview in my activity.
Here is my setText() java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewlogs);
TextView tv = (TextView) findViewById(R.id.tvSqlinfo);
LogsDB info = new LogsDB(this);
info.open();
ArrayList<String> data = info.getData();
info.close();
tv.setText(data);
}
I seem to be getting and error at tv.setText(data);. Stating "The method setText(CharSequence) in the type TextView is not applicable for the arguments (ArrayList)" Then when i do the recommended fix it changes
tv.setText(data)
to
tv.setText((CharSequence) data);
Then when I test the application I get an error stating that it cannot be cast.
What do I need to change to be able to view my database in the textview?
Any advice and help would be greatly appreciated.
Thanks
If you want to keep it simple you can use
tv.setText(data.toString());
instead of
tv.setText(data);
It will show something like this:
([field1],[field2],...)
You probably want to take each String out of the ArrayList and add them to a single String object then add that to your TextView. Something like
String text = "These are my database Strings ";
for (int i=0; i<data.size(), i++)
{
text = text.concat(data.get(i)); // might want to add a space, ",", or some other separator
}
tv.setText(text);
and you can separate the Strings however you want them to be displayed.
Currently the list when populated is starting with the view # the bottom of the list. Is there a way using listAdapters to force it to the top of the list?
Currently the orientation scrolls to the bottom on create. Is there a way to pin the screen to the top when it creates? http://imgur.com/wGTEy in this example you see that entry 1 on create is shoved upwards to make room for six... Instead I want it to populate like this. http://imgur.com/6Lg6e... entry 1 is the top of the list and 6 is pushed off to the bottom for the scroll.
If you look at the picture above you will notice it starts at the bottom of the list instead of at the top. Any Ideas?
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
setListAdapter(mAdapter);
registerForContextMenu(getListView());
populateFields();
private void populateFields() {
if (mRowId != null) {
Cursor note = mDbHelper.fetchDaily(mRowId);
startManagingCursor(note);
String body = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_DBODY));
mAdapter.clear();
if (!(body.trim().equals(""))){
String bodysplit[] = body.split(",");
for (int i = 0; i < bodysplit.length; i++) {
mAdapter.add(bodysplit[i].trim());
}
}
}
}
**edited to fix != string error.
You want the items later in the list to be at the top of the ListView? If so, check out this questions: Is it possible to make a ListView populate from the bottom?
You are completely changing the adapter, so the scroll position is lost in the process... You can use:
ListView listView = getListView();
int position = listView.getFirstVisiblePosition();
if (!(body.trim().equals(""))){
String bodysplit[] = body.split(",");
for (int i = 0; i < bodysplit.length; i++) {
mAdapter.add(bodysplit[i].trim());
}
}
listView.setSelection(position);
But this is not perfect as it is, if a row is added before position the index will be off. If your list contains unique values you can use ArrayAdapter#getPosition(), to find the new index.
While I still recommend using a CursorAdapter, because it handles large table data better, I want to address a point on efficiency with your ArrayAdapter code.
By using adapter.clear() and adapter.add() you are asking the ListView to redraw itself on every step... potentially dozens or hundreds of times. Instead you should work with the ArrayList directly and then ask the ListView to redraw once itself with ArrayAdapter#notifyDataSetChanged() after the loop completes.
I'm trying to create a listview with a static background image (i.e. not contained within the individual list cells, but fills the display and does not move when list is scrolled). I've found a couple references here and am trying to implement, but no joy. The first is;
Set a background for a listview
In particular, I'm trying this;
//set background to Drawable
listView.setBackgroundDrawable(myDrawable);
To create the "myDrawable" variable, I'm using a suggestion from;
How can I access an android drawable by a variable
Again, the particular code I'm trying is;
String icon="logo" + cnt;
int resID = getResources().getIdentifier(icon, "drawable", getPackageName());
logo.setImageResource(resID);
Here is the code I created;
ListView lv = getListView();
lv.setTextFilterEnabled(true);
String bg="football_turf_subtle";
int resID = getResources().getIdentifier(bg, "drawable", getPackageName());
myDrawable.setImageResource(resID);
lv.setBackgroundDrawable(myDrawable);
The problem I'm running into in my code is "myDrawable cannot be resolved"? If it's not obvious, I'm new to Android/Java.
Thanks in advance for any help!
Just do lv.setBackgroundResource(R.drawable.football_turf_subtle).