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
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" >CustomAdapterClass : 'custlistview '</RelativeLayout>
public class custlistview extends BaseAdapter { ArrayList<String> data = null; Context c = null; String path = null; public custlistview(ArrayList<String> 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
Post a Comment