Show List File in listview - android

I'm trying to show files in list view :
It works perfectly. Now I'm trying to add TextView to this Listview so I tried
this i:
I don't want to use adapter class.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv =(TextView) findViewById(R.id.textView2);
path = Environment.getExternalStorageDirectory().getPath();
file = new File(path);
setListView();
}
public void setListView(){
String[] mFilesList = file.list();
mListView = (ListView)findViewById(R.id.listView1);
mArray = new ArrayList<String>();
fArray = new ArrayList<String>();
for(int i = 0; i<mFilesList.length; i++){
mArray.add(mFilesList[i]);
}
for (int i = 0; i < mFilesList.length; i++) {
fArray.add(mFilesList[i].length()+" files");
}
ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(getApplicationContext()
, R.layout.rows, R.id.textView1,mArray);
ArrayAdapter<String> mAdapterSecond = new ArrayAdapter<String>(getApplicationContext()
, R.layout.rows, R.id.textView2,fArray);`
mListView.setAdapter(mAdapterSecond);
mListView.setAdapter(mAdapter);
It's showing just one textview. Why?

The ListView accepts only one Adapter. Your last code line changes the ListView adapter from mAdapterSecond to mAdapter. What you need in your case is to implement your custom adapter that fill the two TextView as you wish.

Related

How to list content of directory into spinner?

I am attempting to obtain all of the files inside of a directory and display them inside of a spinner. I am trying to use the code below but can't figure out how to display it inside of the spinner.
File("sdcard/Velocity").walkTopDown().forEach { println(it) }
To get the files as
public String[] getFiles(String path) {
File directory = new File(path);
File[] files = directory.listFiles();
String arr[] = new String[files.length];
for (int i = 0; i < files.length; i++) {
fileList[i] = files[i].getName();
}
}
To show the in activity
<Spinner
android:id="#+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
To populate
public class SpinnerExample extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String[] arraySpinner = getFiles();
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s.setAdapter(adapter);
}
}

how can I color an item in listview in xamarin native android

I want to change the color of an item in a listview in Xamarin Android.
This is my code:
mlistView = FindViewById<ListView>(Resource.Id.lista_clientes);
mItems = new List<string>();
for (int i = 0; i < dt_lista.Rows.Count; i++)
{
mItems.Add(dt_lista.Rows[i][1].ToString());
}
mAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, mItems);
mlistView.Adapter = mAdapter;
private void changeTheColor(ListView listView, int position) { listView.getChildAt(position).setBackgroundColor(Color.*); }

Adding a set of buttons to ListView and then setting adapter and setting button texts

Hi guys I'm having difficulty figuring out why my buttons wont display the right text and display junk code. I am running a serverRequest then creating the buttons after the details are gotten and I surely did test I am getting the right string back from the server.
public void getCourses(User user) {
ServerRequest serverRequest = new ServerRequest(this);
serverRequest.fetchUserCoursesDataInBackground(user, new getUserCallback() {
#Override
public void doneString(String[] returnedString) {
if (returnedString == null) {
System.out.println("DONE EMPTY");
} else {
userLocalStore.storeUserCourses(returnedString);
final ListView listView = (ListView) findViewById(R.id.viewCourseList);
final ArrayList<Button> list = new ArrayList<>();
View v = getWindow().getDecorView();
for (int i = 0; i < returnedString.length; i++) {
System.out.println("This is in for loop:" +returnedString[i]);
Button button = new Button(v.getContext());
button.setText(returnedString[i]);
button.setId(i);
button.setHeight(40);
button.setWidth(100);
list.add(button);
}
if (list.isEmpty()) {
System.out.println("List is empty bro");
} else {
final ArrayAdapter adapter = new ArrayAdapter(v.getContext(), android.R.layout.simple_list_item_1, list);
listView.setAdapter(adapter);
System.out.println("Adding adapter");
}
}
}
this is the whole code. I will show you what it is displaying on the application
this is the bug
I see 3 problems in this code:
1st i assume java can mess this 1 up:
replace> final ArrayList list = new ArrayList<>();
with> final ArrayList list = new ArrayList();
2nd you are using a layout "android.R.layout.simple_list_item_1" for the ArrayAdapter which is an xml layout including a text view thus you screenshot shows 2 text views and not buttons.
3rd new ArrayAdapter() constructor takes a list of Strings. so if you...
replace> final ArrayAdapter adapter = new ArrayAdapter(v.getContext(), android.R.layout.simple_list_item_1, list);
with> final ArrayAdapter adapter = new ArrayAdapter(v.getContext(), android.R.layout.simple_list_item_1, returnedString);
you will see buttons with the text that you are printing in your loop.

android 2d array to listview

so i've got this piece of code and i want it to output attr1 and attr2 in the listview, but the current error i get is: cannot resolve constructor. Here is the piece of code:
private String[][] content = {
{"attr1", "url1"},
{"atrr2", "url2"}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ListView lv = (ListView) findViewById(R.id.lv);
for(int i = 0; i < content.length; i++) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, content[i][0]);
lv.setAdapter(adapter);
}
}
Hopefully somebody could help me, thanks in advance (sorry for the bad english)
Move the creation of the list outside your for loop like so:
ListView lv = (ListView) findViewById(R.id.lv);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
for(int i = 0; i < content.length; i++) {
adapter.add(content[i][0]);
}
lv.setAdapter(adapter);
You can get the index of the clicked item and use that information to access your array again and get the second piece of information

How to add Image to the Custom List View in Android?

I have a custom list view with image view and text view.I am loading text view data from SQLite Database and I want to add image from my drawable folder to the image view contain in list based on some condition
I am using cursor to load data to the list view.Please any one guide me how to do this
Very simple solution for that,using a simple adapter.
public class TestList extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView list = (ListView) findViewById(R.id.list);
ArrayList<Map<String, String>> arrlist = new ArrayList<Map<String, String>>();
Map<String, String> m = null;
for (int i = 0; i <=50; i++) {
m = new HashMap<String, String>();
m.put("key", String.valueOf(R.drawable.ic_launcher));
m.put("title", "Title-" + i);
m.put("subtitle", "SubTitle-" + i);
arrlist.add(m);
}
SimpleAdapter adapter = new SimpleAdapter(this, arrlist,
R.layout.sample, new String[] { "key", "title", "subtitle" },
new int[] { R.id.imageView1, R.id.title, R.id.subtitle });
list.setAdapter(adapter);
}
}
Xml layouts: main xml layout has a listview with id-list,and sample.xml is your inflating layout,it has one image,two textviews.Modify the code as your requirement.

Categories

Resources