0

Can you explain How to display another textview in this code? I'm using viewpager so i just print only one textview i want one more textview. please anybody can you help me. thanks in advance.

this is my code

This is my Mainactivity class

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set title for the GridView
        setTitle("GridView");
        // Get the view from grid_view.xml
        setContentView(R.layout.grid_view);

        // Set the images from ImageAdapter.java to GridView
        GridView gridview = (GridView) findViewById(R.id.gridview);
         //gridview.setAdapter(new ImageAdapter(this));  //commented line 05052018 
        gridview.setAdapter(new ChangesofAdapter(this)); //new line 05052018
        // Listening to GridView item click
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

                // Launch ImageViewPager.java on selecting GridView Item
                Intent i = new Intent(getApplicationContext(), ImageViewPager.class);

                // Show a simple toast message for the item position
                Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();

                // Send the click position to ImageViewPager.java using intent
                i.putExtra("id", position);

                // Start ImageViewPager
                startActivity(i);
            }
        });
    }
}

this is my gridview class -> ChangesofAdapter.class

public class ChangesofAdapter extends BaseAdapter {
    private Context mContext;
    public ChangesofAdapter(Context c) {
        mContext = c;
    }
    public int getCount() {
        return getDataContacts().length;
    }
    public Object getItem(int position) {
        return getDataContacts()[position];
    }
    public long getItemId(int position) {
        return 0;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView imageView;
        if (convertView == null) {  // If it's not recycled, initialize some attributes
            imageView = new TextView(mContext.getApplicationContext());
            imageView.setTextColor(Color.GREEN);
            imageView.setGravity(Gravity.LEFT); // new line 05052018
            imageView.setTextSize(30);
        } else {
            imageView = (TextView) convertView;
        }
        String abc =getDataContacts()[position];
        imageView.setText(abc); // new line 05052018
        return imageView;
    }
    public String[] getContacts(){
        DBUtils utils = new DBUtils(mContext.getApplicationContext());
        ArrayList<String> names = new ArrayList<String>();
        new DBUtils((mContext.getApplicationContext()));
        try {
            DBUtils.createDatabase();
        } catch (IOException e) {
            Log.w(" Create Db "+e.toString(),"===");
        }
        DBUtils.openDatabase();
        Cursor cursor = utils.getResult("select * from Cflviewpagerdata order by title");
        cursor.moveToFirst();
        while(!cursor.isAfterLast()) {
            names.add(cursor.getString(cursor.getColumnIndex("view")));
            cursor.moveToNext();
        }
        cursor.close();
        DBUtils.closeDataBase();
        return names.toArray(new String[names.size()]);
    }
    public String[] getDataContacts(){
        DBUtils utils = new DBUtils(mContext.getApplicationContext());
        ArrayList<String> names = new ArrayList<String>();
        new DBUtils((mContext.getApplicationContext()));
        try {
            DBUtils.createDatabase();
        } catch (IOException e) {
            Log.w(" Create Db "+e.toString(),"===");
        }
        DBUtils.openDatabase();
        Cursor cursor = utils.getResult("select * from Cflviewpagerdata order by title");
        cursor.moveToFirst();
        while(!cursor.isAfterLast()) {
            names.add(cursor.getString(cursor.getColumnIndex("title")));
            cursor.moveToNext();
        }
        cursor.close();
        DBUtils.closeDataBase();
        return names.toArray(new String[names.size()]);
    }
}

This code is represents while click grid view items this page will call this page is have viewpager of grid view items this class is viewpager -> ImageViewpager.class

    public class ImageViewPager extends Activity implements OnInitListener {
    // Declare Variable
    int position;
    Bundle img = new Bundle();
    ViewPager vp;
    private TextToSpeech mTts;
    List<TextView> images;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set title for the ViewPager
        setTitle("ViewPager");
        // Get the view from view_pager.xml
        setContentView(R.layout.view_pager);
        mTts = new TextToSpeech(this, this);
        // Retrieve data from MainActivity on item click event
        Intent p = getIntent();
        position = p.getExtras().getInt("id");  
        ChangesofAdapter imageAdapter = new ChangesofAdapter(this); //new line 05052018
        images = new ArrayList<TextView>(); //new line 05052018

        // Retrieve all the images
        for (int i = 0; i < imageAdapter.getCount(); i++) {
            TextView imageView = new TextView(this); //new line 05052018
            String abc = imageAdapter.getContacts()[i];
            imageView.setText(imageAdapter.getContacts()[i]);  //new line 05052018
            imageView.setTextColor(Color.BLUE);
            imageView.setGravity(Gravity.CENTER_HORIZONTAL);//new line 05052018
            img.putString("CURRENT_POSITION", abc);
            images.add(imageView);
        }
        vp=(ViewPager)findViewById(R.id.pager);         //new line 06052018
        vp.setAdapter(new ViewPagerAdapter(images));    //new line 06052018
        vp.setOffscreenPageLimit(0);
        vp.setCurrentItem(position);                    //new line 06052018
    }

    public class ViewPagerAdapter extends PagerAdapter {
        private List<TextView> images;  
        public ViewPagerAdapter(List<TextView> images) {
            this.images = images;
        }   
        @Override
        public Object instantiateItem(final ViewGroup container, int position) {
            final TextView imageView = images.get(position);                              //commented line on date 06052018
            container.addView(imageView);
            imageView.setTextSize(120);
            vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                public void onPageSelected(int pos) {
                 int  currentposition = pos;
                 mTts.speak(images.get(currentposition).getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
                }
                public void onPageScrolled(int arg0, float arg1, int arg2) {                
                }
                public void onPageScrollStateChanged(int arg0) {
                }
            });     
            return imageView;
        }
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }   
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return images.size();
        }
        @Override
        public boolean isViewFromObject(View view, Object o) {
            // TODO Auto-generated method stub
            return view == o;
        }
    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (mTts != null) {
            mTts.shutdown();
        }
    }
    @Override
    protected void onDestroy() {
        //Close the Text to Speech Library
        if(mTts != null) {
            mTts.stop();
            mTts.shutdown();
        }
        super.onDestroy();
    }
    public void onInit(int arg0) {
        if(arg0 == TextToSpeech.SUCCESS){
            mTts.speak(images.get(position).getText().toString(), TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

This is my application look

This is my Gridview look and while click item number seven this item will open viewpager that is second look.

enter image description here

This is my viewpager look in this look i want more textview.

how to display another textview below item(seven) or above item(seven)

enter image description here

This is my code can any one help me. i'm new one of Android programmer learner.

Please........

Thanks in advance.

1 Answers1

0

you have 2 optional way

like i see you use one of them so you can continue with it which is

FIRST WAY : Programmatically

imageView = new TextView(mContext.getApplicationContext());
        imageView.setTextColor(Color.GREEN);
        imageView.setGravity(Gravity.LEFT); // new line 05052018
        imageView.setTextSize(30);

so you can add more ANYOBJECT with it here but put them into container

check this out

https://stackoverflow.com/a/26133978/6998825

SEC WAY : MODULE AND REFF

this way you will create XML layout and inflate it in your baseadapter so you can use it's element

row_module.xml FOR EX

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

<ImageView
    android:id="@+id/table_IMG"
    android:layout_width="120dp"
    android:layout_height="120dp" />
<TextView
    android:id="@+id/table_num"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textSize="20dp"
    android:textColor="#000000"
    android:layout_margin="40dp"/>
<TextView
    android:id="@+id/table_ID"
    android:layout_width="0dp"
    android:layout_height="0dp" />
</RelativeLayout>

in your adapter something like this

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.table_model, null);

    TextView TV_ID = (TextView)vi.findViewById(R.id.table_ID); // ID
    ImageView bgImg =(ImageView)vi.findViewById(R.id.table_IMG); // IMG
    TextView TV_details = (TextView)vi.findViewById(R.id.table_num); // details

    HashMap<String, String> arraylist_type = new HashMap<String, String>();
    arraylist_type = data.get(position);

    // Setting all values in listview
    TV_ID.setText(arraylist_type.get("ID"));

    Glide.with(activity).load(arraylist_type.get("IMG")).into(bgImg);

    TV_details.setText(arraylist_type.get("details"));

    return vi;
}
Hassan Badawi
  • 302
  • 1
  • 10