-1

This is my class and this is where i display the data to a list adaptor R.id.listView1 so all i need is a custom adaptor please i'm new to android and all other tutorials are out of my league. This is just a small help i require form the community as this help me a great amount.

import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;


public class View_Data extends AppCompatActivity {


    public ArrayList<String> datax = new ArrayList<String>(); //used to store data from db

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_data);
        final String LOG_TAG = View_Data.class.getSimpleName();
        Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/AABOHI.TTF"); // this is the custom font i want to use

        try {
            String table = "questions";
            String[] columnsToReturn = {"question_id", "question", "def_font"};
            SQLiteDatabase mydatabase = openOrCreateDatabase("Data",MODE_PRIVATE,null);
            Typeface font = Typeface.createFromAsset(getAssets(), "fonts/AABOHI.TTF");

            Cursor dbCursor = mydatabase.query(table, null,null, null, null, null, null);

            dbCursor.moveToFirst();

            String temp ;

            int i = 0;
            ListView listView1 = (ListView) findViewById(R.id.listView1);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, datax);
            listView1.setAdapter(adapter);



            while (!dbCursor.isAfterLast()) {
                Log.v(LOG_TAG, String.valueOf(dbCursor.getString(0)));
                Log.v(LOG_TAG, String.valueOf(dbCursor.getString(1)));

                temp = " Question   : " + String.valueOf(dbCursor.getString(1)) ;//This is what i want in a custom font



                datax.add(temp);

                dbCursor.moveToNext();
                i++;
            }

            dbCursor.close();


            for (String row : datax) {

                Log.v(LOG_TAG, row);

            }


        } catch (Exception e) {
            Log.v(LOG_TAG, e.toString());
        }

        ListView listView1 = (ListView) findViewById(R.id.listView1);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, datax);
        listView1.setAdapter(adapter);



    }
}

1 Answers1

1

Please see the screenshotYou can go for base adapter for this type of requirement wherein you can have the facility to modify a lot of things. To know and implement base adapter go through the links :

How to customize listview using baseadapter

http://abhiandroid.com/ui/baseadapter-tutorial-example.html

You can easily customize the base adapter for your required data and font.

Create a base adapter as below : 


public class CustomAdapter extends BaseAdapter {
    Context context;
    ArrayList<String> arrayList;
    LayoutInflater inflter;
    Typeface font;

    public CustomAdapter(Context applicationContext,  ArrayList<String> arrayList) {
        this.context = applicationContext;
        this.arrayList = arrayList;
        inflter = (LayoutInflater.from(applicationContext));
        font = Typeface.createFromAsset(context.getAssets(), "fonts/Questrial-Regular.ttf");
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.activity_item, null);
        TextView tv = (TextView) view.findViewById(R.id.tv);
        tv.setText(arrayList.get(i));
        tv.setTypeface(font);
        return view;
    }
}

Main activity

public class MainActivity extends AppCompatActivity {
    ArrayList<String> simpleStringArrayList;
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        simpleStringArrayList = new ArrayList<>();
        simpleStringArrayList.add("aaa");
        simpleStringArrayList.add("bbb");
        simpleStringArrayList.add("ccc");
        simpleStringArrayList.add("ddd");
        simpleStringArrayList.add("eee");
        simpleStringArrayList.add("fff");
        simpleStringArrayList.add("ggg");

        listView = (ListView) findViewById(R.id.listView);
        listView= (ListView) findViewById(R.id.listView);
        CustomAdapter customAdapter = new CustomAdapter(this, simpleStringArrayList);
        listView.setAdapter(customAdapter);
    }
}

activity_item

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

activity_main

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.dell1.myapplication.MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       />
</RelativeLayout>
Community
  • 1
  • 1
nishith kumar
  • 913
  • 7
  • 19
  • in your line : TextView tv = (TextView) view.findViewById(R.id.tv); What is R.id.tv supposed to point to i dont have a text view in my xml but a listview can u just change to listview and then apply the font ? resource id for the lisview is : R.id.listView1 and how do i use Set TypeFace ? – dummystreamer Sep 23 '16 at 06:13
  • You have to create a separate layout (activity_item)which will contain only a textview (tv). This layout will be loaded in the Custom adapter in the getView method. From your activity class after loading your arraylist with the datafrom your database, pass this arraylist as a parameter to the CustumAdapter. – nishith kumar Sep 23 '16 at 06:30
  • i did everything you said but the screen shows blank the verbose log shows the data when i display but the view does not. \ – dummystreamer Sep 23 '16 at 07:17
  • custom adapter class : https://gist.github.com/anonymous/e1c81f0fb3234691d4a1e4656fbcdd4b viewData class : https://gist.github.com/anonymous/95bae76dc7304c7598c7eddd39b6a8f0 – dummystreamer Sep 23 '16 at 07:20
  • I have added my all the classes along with the screenshot – nishith kumar Sep 23 '16 at 08:02
  • First get the data from your DB into your arrayList. Then load your Custom adapter with this arraylist and then set your listview with the adapter. – nishith kumar Sep 23 '16 at 08:09
  • Thank you sir , im so happy as this was my first experience asking from the community and had an inevitable fear of being trolled for being a novice but you sir have been a great help ... i feel so happy ... thanks a ton Sir :) – dummystreamer Sep 23 '16 at 08:42