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

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;

Passing Images between Activities in Android

in First Activity: Intent intent=new Intent(FirstClass.this, SecondClass.class); Bundle bundle=new Bundle(); bundle.putInt("image",R.drawable.ic_launcher); intent.putExtras(bundle); startActivity(intent); in Second Acticity: Bundle bundle=this.getIntent().getExtras(); int pic=bundle.getInt("image"); v.setImageResource(pic); another method: in First Activity: Drawable drawable=imgv.getDrawable(); Bitmap bitmap= ((BitmapDrawable)drawable).getBitmap(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] b = baos.toByteArray(); Intent intent=new Intent(Passimage.this,myclass.class); intent.putExtra("picture", b); startActivity(intent); in Second Acticity: Bundle extras = getIntent().getExtras(); byte[] b = extras.getByteArray("picture"); Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.lengt

Show and Resume Android Soft-Keyboard

Code to show keyboard: InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(yourEditText,InputMethodManager.SHOW_IMPLICIT); Code resume keyboard : InputMethodManager imm = (InputMethodManager)gettSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);