I m developing a app that downloads list of files from ftp server. the file names are downloaded using array. how can i pass this array to main activity and display it in a list view.i have a very little knowledge in this domain so kind of help is appreciated.thanking you in advance
FTPFile[] ftpFiles = mFTPClient.listFiles("/public_html/");
int length = ftpFiles.length;
for (int i = 0; i < length; i++) {
String name = ftpFiles[i].getName();
boolean isFile = ftpFiles[i].isFile();
if (isFile) {
Log.i(TAG, "File : " + name);
}
else {
Log.i(TAG, "Directory : " + name);
}
You need to use ListView widget. The simplest way of displaying a list of items in a list view would be to use predefined ArrayAdapter class and an existing Android layout for the rows. Please check this tutorial for detailed explanation how to use ListView.
http://www.vogella.com/articles/AndroidListView/article.html
Related
I have dynamic listView linked with JSONArray, in the adapter i'm trying to read the storage to check if the pdf file is downloaded already the Button text will be View and will call View method but if the the file not there the Button text will be Download and will call Download method.
I have been trying this code but it's not work
String path2 = Environment.getExternalStorageDirectory() + "/materialpdffolder/";
File path = new File(path2);
File list[] = path.listFiles();
for( int i=0; i < materialList.size() ; i++)
{
for( int x=0; x <= list.length ; x++) {
if (list[x].getName() != null) {
if (list[x].getName().equals(materialList.get(position).getMaterial_file_name().toString())) {
DVmaterial.setText("View(" + materialList.get(position).getMaterial_file_name().toString() +")" );
}}else
{
DVmaterial.setText("Download(" + materialList.get(position).getMaterial_file_name().toString() + ")");
}
}
}
The problem is when there’s no file, the application is stack and give error and not give any results for list[x].get Name() and when I use materiallist.size() for for looping it is doesn’tgive a correct answer
Try to change with this code
if (list[x].getName() != null) {
if (list[x].getName().equals(materialList.get(position).getMaterial_file_name().toString())) {
DVmaterial.setText("View(" + materialList.get(position).getMaterial_file_name().toString() +")" );
}else
{
DVmaterial.setText("Download(" + materialList.get(position).getMaterial_file_name().toString() + ")");
}
}
Remove one bracket from }}else and put it end of else
I'm quite new to SQLite databases and need help using an external text file to create an initial database to store a bunch of items that can be selected and added to a second database. To put this all in context, I want to have an initial database that stores a list of grocery store items. Each item will have two traits with them, a NAME and a TYPE. So there are items such as( NAME Bread, TYPE Bakery. NAME Grape Juice, TYPE Beverage. NAME Toothbrush, TYPE Toiletries. )
I need to go from something like this:
GROCERYSTORE.TXT
-- -- -- -- -- --
NAME - TYPE
Bread - Bakery
Rolls - Bakery
Juice - Beverages
Soda - Beverages
to this:
dBHelper.insert("Bread", "Bakery");
or:
dBHelper.insert(name, type);
for each item in the text file.
First of all, I would like to know how to format this text file, or any other type of file that could be read and inserted into a database if a text file is not the easiest way to go about this.
Second, how would I go about reading this text file and inserting it into the database?
I'm thinking I would use a buffered reader and inputstream within a for loop to go and insert each item into the database. I'm just not sure how to parse each line to give each item a name and a type to be inserted into the database, and how to format a text file to be read.
Any help would be greatly appreciated as I have found very little about using external files to import data into an sql database that pertains to my situation.
In my apps that have a static or default database I use an xml file for my data sort of like this:
default_grocerystore.xml stored in assets folder:
<default_grocerystore>
<grocery>
<name>Bread</name>
<type>Bakery</type>
</grocery>
<grocery>
<name>Rolls</name>
<type>Bakery</type>
</grocery>
<grocery>
<name>Juice</name>
<type>Beverages</type>
</grocery>
<grocery>
<name>Soda</name>
<type>Beverages</type>
</grocery>
</default_grocerystore>
Then in the onCreate in my database class I create my database tables I populate them with the xml like this:
private void populateGrocery(SQLiteDatabase db) {
ArrayList<GroceryObj> groceryArrayList;
groceryArrayList = buildGroceryArrayList();
String insertStmt = null;
for (int i = 0; i < groceryArrayList.size(); i++) {
insertStmt = "INSERT INTO " + GROCERY_TABLE + " ("
+ GROCERY_KEY_NAME + ", " + GROCERY_KEY_TYPE + ") "
+ "VALUES (\""
+ groceryArrayList.get(i).getName()
+ "\", \""+groceryArrayList.get(i).getType()+"\");";
db.execSQL(insertStmt);
}
private ArrayList<GroceryObj> buildGroceryArrayList() {
ArrayList<GroceryObj> aL = new ArrayList<GroceryObj>();
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream raw = context.getAssets().open(
"default_grocerys.xml");
Document dom = builder.parse(raw);
Element root = dom.getDocumentElement();
NodeList groceryItems = root.getElementsByTagName("grocery");
for (int i = 0; i < groceryItems.getLength(); i++) {
String name= null;
String type= null;
Node item = groceryItems.item(i);
NodeList groceryItem = item.getChildNodes();
for (int j = 0; j < groceryItem.getLength(); j++) {
Node nodeItem= groceryItem.item(j);
String nodeName= noteItem.getNodeName();
if (nodeName.equalsIgnoreCase("name")) {
name= nodeItem.getFirstChild().getNodeValue();
} else if (nodeName.equalsIgnoreCase("type")) {
type= nodeItem.getFirstChild().getNodeValue();
}
}
aL.add(new GroceryObj(name, type));
}
} catch (Exception e) {
e.printStackTrace();
}
return aL;
}
}
how can i display all the files (.pdf, .doc, .ppt, .mp4, etc...) inside a folder in sdcard? i want to display their filenames.
here's what i've got:
File files = new File(Environment.getExternalStorageDirectory() + "/Downloads/Files/");
File[] list = files.listFiles();
int lists = files.listFiles().length;
String qwe = list.toString();
Log.d(LOG_TAG, "list: " + lists);
Log.d(LOG_TAG, "names: " + qwe + "\n"); //i want to show their filenames
for(int i=0; i < questionfiles.listFiles().length; i++){
if(list[i].isHidden()){
Log.d(LOG_TAG, "hidden path files.."+list[i].getAbsolutePath());
}
}
You have to use a ListActivity with an Adapter (ArrayAdapter for istance).
see this as example
EDIT: You can print the array content in this way:
Log.i(LOG_TAG, Arrays.toString(list));
I have a list view with check boxes.I want to get all the checked items ids or data in the particular position.
Please any one help me with sample code
Thanks in advance
I found this answer from Internet
Working properly what i need
my_sel_items=new String("Selected Items");
SparseBooleanArray a = lView.getCheckedItemPositions();
for(int i = 0; i < lv_items.length ; i++)
{
if (a.valueAt(i))
{
/*
Long val = lView.getAdapter().getItemId(a.keyAt(i));
Log.v("MyData", "index=" + val.toString()
+ "item value="+lView.getAdapter().getItem(i));
list.add(lView.getAdapter().getItemId((a.keyAt(i))));
*/
my_sel_items = my_sel_items + ","
+ (String) lView.getAdapter().getItem(i);
}
}
Log.v("values",my_sel_items);
Would I be able to split a string of data starting with <NewData> with several different objects inside such as <Schedule> ending in </Schedule> and then ending over all with a </NewData>. Split that into several list views each schedule in a different list view? quite difficult to explain so any thing I've missed just say...
Umm I'll only give you a hint,
String stuff = "<Schedule> save kittens </Schedule> " +
"<Schedule> and puppies </Schedule>" ;
String [] result = stuff.split("<Schedule>");
for(int i = 0; i < result.length; i++)
{
if(result[i].length() > 0)
Log.d("TODO", " - " + result[i].substring(0, result[i].indexOf("<")));
}