0

I have a listview in a fragment. I want to set those multiple textviews. So how should i set the custom adapter. I am new at android and need some help.

public class Tab1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.tab_1, container, false);

        ListView listView = (ListView)v.findViewById(R.id.listOfTasks);

//need to pass 2 string arrays.
//string[] s1;
//string[] s2; 

 ListAdapter taskAdapter = new TaskCustomAdapter(  //here.....  );
        listView.setAdapter(taskAdapter);

        listView.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String task = String.valueOf(parent.getItemAtPosition(position));
                        Toast.makeText(getActivity(),task,Toast.LENGTH_SHORT).show();
                    }
                }
        );

        return v;
    }

}

this is my list view with multiples textfields.

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

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/task_title"
            android:layout_gravity="left|center_vertical"
            android:layout_marginLeft="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Large Text"
            android:id="@+id/task_subtitle"
            android:layout_gravity="center" />


    </FrameLayout>

</LinearLayout>

And my present adapter is :

public class TaskCustomAdapter extends ArrayAdapter<String>{

    public TaskCustomAdapter(Context context,String[] tasks) {
        super(context,R.layout.taskfragment,tasks);

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){

        LayoutInflater task_inflater = LayoutInflater.from(getContext());
        View customView = task_inflater.inflate(R.layout.taskfragment,parent,false);

        String task = getItem(position);
        TextView title = (TextView)customView.findViewById(R.id.task_title);
        title.setText(task);

        return customView;
    }
}

thanks!

aman003
  • 15
  • 5

3 Answers3

0

You're on the right track! You've almost done it yourself. Just initialize all the components (TextViews or whatever) here, as you did with the one TextView already:

TextView title = (TextView)customView.findViewById(R.id.task_title);
TextView subtitle = (TextView)customView.findViewById(R.id.task_subtitle);
//etc for every one
title.setText(task);
subtitle.setText("whatever");

Simply use them like you normally would. Do consider using the ViewHolder pattern for this kind of thing, read more about it on the linked question.

EDIT:

Another thing you asked, how to pass 2 arrays, simply change the constructor like so:

private String[] subtitles; //or whatever

public TaskCustomAdapter(Context context,String[] tasks, String[] subs) {
    super(context,R.layout.taskfragment,tasks);
    subtitles = subs;
}

And now simply call the constructor with the additional parameter like:

ListAdapter taskAdapter = new TaskCustomAdapter(getActivity(), s1, s2);

Where s1 and s2 are the string arrays you want to pass. You can now use the second textView something like this:

subtitle.setText(subtitles[position]);
Community
  • 1
  • 1
Vucko
  • 7,371
  • 2
  • 27
  • 45
  • But how do i send these two strings[] s1 and s2 ? Adapter can have only 2 arguments which are the activity and one string only.. Please tell me how to pass both the strings to my adapter?? – aman003 Jul 19 '16 at 13:13
  • That's easy man, just add another array to the constructor argument and assign a local variable to that value. I'll edit my answer. @aman003 – Vucko Jul 19 '16 at 13:56
0
Below code put in activity oncreate::: 

//List view reference
    ListView lv=(ListView) findViewById(R.id.listView);
//Creating the custom adapter and passing the string array data
     MainListViewCustomAdapter adapter = new MainListViewCustomAdapter(this,titleList,subtitlelist)
//Setting the adapter to listview
       lv.setAdapter(adapter);
Siva
  • 91
  • 6
-1
package com.ultimate.gre.prep.adapters;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



public class MainListViewCustomAdapter extends BaseAdapter {

    private String[] mTitleListToDisplay;
    private String[] mSubTitleListToDisplay;
    private Context mContext;

    //through constructor you can send the data
    //if you want any more data ,then please add more parameters to constructor
    public MainListViewCustomAdapter(Context context,String[] titleListToDisplay,String[] subTitleListToDisplay){
        mContext =  context;
        mTitleListToDisplay = titleListToDisplay;
        mSubTitleListToDisplay =subTitleListToDisplay;
    }
    @Override
    public int getCount() {
        return mTitleListToDisplay.length();
    }

    @Override
    public Object getItem(int position) {
        return mTitleListToDisplay.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder = null;

          LayoutInflater mInflater = (LayoutInflater) mContext
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
            //For the first time it will execute
                convertView = mInflater.inflate(R.layout.main_activity_custom_row_leyout, null);
                holder = new ViewHolder();
                holder.txtTitle = (TextView) convertView.findViewById(R.id.task_title);
                holder.subTitleTextView = (TextView) convertView.findViewById(R.id.task_subtitle);
                convertView.setTag(holder);
            } else {
            //second time onwards this case will execute
                holder = (ViewHolder) convertView.getTag();
            }

            holder.titleTextView.setText(mTitleListToDisplay[position]);
            holder.subTitleTextView.setText(mSubTitleListToDisplay[position]);

            return convertView;
    }


    private class ViewHolder {
       TextView titleTextView;
        TextView subTitleTextView;
    }

}
Siva
  • 91
  • 6
  • You did not provide anything **new** that my answer did not cover, and you answered 15 minutes later. – Vucko Jul 18 '16 at 16:31
  • what you are talking....i am not given any wrong answer right..then how can u give negative marking...first you please remove that – Siva Jul 18 '16 at 16:51
  • Do you really had common sense...you please remove -ve marking.as long as its not wrong answer you dont have rights to give -ve marking – Siva Jul 18 '16 at 16:52
  • Do you really a developer?...you please remove -ve marking ASAP.I just tried to help you,but you behaves like...etc – Siva Jul 18 '16 at 16:54
  • @aman003....i am trying to help you but this "Vucko" guy behaves like very wrost. – Siva Jul 18 '16 at 17:14
  • Siva . How to pass both the string[] to my adapter?? The adapters parameters only allow 2 arguments ,activity and a string – aman003 Jul 19 '16 at 13:18
  • in above code we are using custom adaptor. Please let me know if you want any thing.i will try – Siva Jul 19 '16 at 15:21
  • If my code helped you,then please accept my answers .Thank you – Siva Jul 20 '16 at 10:05