Skip to main content

Read File Names From Assets To A ListView

Here in this blog we are explaing how to load files names from asset and load it in a listview.
Here we have saved certain html files inside assets/datas/..
Copy and Download the code...
MainActivity
 

public class MainActivity extends Activity {
	ListView list;
	String path;
	ArrayList<String> data = new ArrayList<String>();

	@Override 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		list = (ListView) findViewById(R.id.listView1);
		path = getResources().getString(R.string.directory);
		showlist();
	}
	private void showlist() {
		String[] data1 = null;
		    // *****got all resource in "am" from asset*********
		AssetManager am = getResources().getAssets();
		try {
			// ***** got all one by one in arraylist "data1" *********
			data1 = am.list(path);

		} catch (Exception e) {

		}
		for (String name : data1) {

			// ***** removing all .htm from extension**************
			String[] trimdata = null;
			trimdata = name.split("\\.");
			data.add(trimdata[0]);
		}
		list.setAdapter(new custlistview(data, MainActivity.this, path));
	} }


activity_main.xml
 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

   

</RelativeLayout>

CustomAdapterClass : 'custlistview '
 
public class custlistview extends BaseAdapter {
	ArrayList<String&gt data = null;
	Context c = null;
	String path = null;

	public custlistview(ArrayList<String&gt data, Context c, String path) {
		this.data = data;
		this.c = c;
		this.path = path;
		System.out.println("fourth" + path);
	}

	public int getCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	public View getView(int position, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		LayoutInflater inflater = (LayoutInflater) c
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View list = null;
		list = new View(c);
		//
		list = inflater.inflate(R.layout.list_view, null);
		//
		TextView txt = (TextView) list.findViewById(R.id.title);
		txt.setText((CharSequence) data.get(position));

		list.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
			  // can give click to next page
			}
		});

		return list;
	}}
Custom rows in listview: 'list_view'
 
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:textColor="#000000"
        android:textSize="18sp" 
        android:drawablePadding="8dp"/>

</RelativeLayout>
Strings.xml
 


    datas

Comments

Popular posts from this blog

Getting started with IBM worklight Mobile App In Eclipse using Ionic / Angularjs

Install Eclipse and add IBM MobileFirst Platform Studio 7.1.0 .  You can follow the following steps : Goto Help > > Eclipse Market Place >> and search for IBM MobileFirst Platform Studio 7.1.0 . and u can add and install thet plugin and get started with ionic - angular app development. You can start the new project and so can add your Environment needed and start development. After creating the sample project you can add ionic bundle folder inside your application so that you get all the necessary features of ionic development. Details and other way of installation is explained here  

Calling a script from an external file

We Can just recall the previous example of image switching to understand how to call script externally.  Let's have a look. <html> <head> </head> <body> click on the image </body> </html> Using the tag <script type="text/javascript" src="functions.js"> </script> we can call the script externally. Always write script in file with extension ".js". functions.js: function imageswitch(){ var image = document.getElementById('switchimage'); if(image.src.match("one")) { image.src = "flower_two.jpg"; } else { image.src = "flower_one.jpg"; } } Note: Make sure that the both html and js file locations are the same or else defime them specifically

Spannable String in Android - URL Span ,Clickable Span, Rich-Style Formatting of Textview .....

See more Android Tutorials here....... Faster Loading images in GridViews or ListViews Spannable brings lots of possibility to TextView, includes displaying various appearance of a Text and onClick callbak. The SpannableString class allows you to easily format certain pieces which are called spans of a string, by applying CharacterStyle ie,color, font, ormake it a link . Here is an example where, explained how to use spannable string to give font size, color, linking a text via clickable span and through URL Span and to strike through the text. Lets go through the example : import android.os.Bundle; import android.text.SpannableString; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.text.style.ForegroundColorSpan; import android.text.style.RelativeSizeSpan; import android.text.style.StrikethroughSpan; import android.text.style.URLSpan; import android.view.View; import android.widget.TextView; import android.widget.Toast; ...