how to create arrayList for ListView SecondActivity

Joseph :

I created a List of String Array in the strings.xml so it can then be used in and Adapter, but I don't want the array stored in the string to be set as new-descriptive text when a listItemClick opens to a new activity. Please help me. I want to create another list of array as full-description which should be different from the one tied to the row.

Let me describe what I mean furthermore.

My row.xml shows an Image, TextView as Title, & another TextView as description. Now i have used this arrangement to generate constructor and getters for my Model.java

Now, if I click on a list of ListView items, it opens to a NewActivity.java there by feeding its data stored in string.xml as arrayList which is simply Title & description texts. But I want another text different from that discription to show in that NewActivity.java as brand-new-descrption text. That is what I'm struggling to do now.

Thanks

this is the MainActivity.java code

public class MainActivity extends AppCompatActivity implements SearchView.OnQueryTextListener {
    private String TAG = "MainActivity ----- ; " ;
    // Store instance variables

    private int page;
    private ConsentForm form;

    ListView listView;
    ListViewAdapter adapter;
    List<String> title, description2,description;
    int[] icon;
    int [] soundfile;
    ArrayList<Model> arrayList = new ArrayList<Model>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Redeemed Songs");

        //listview soundfile file for songs in position
        soundfile= new int[] {R.raw.song_1,R.raw.song_2,R.raw.song_3, R.raw.song_4,R.raw.song_5,R.raw.song_6,R.raw.song_7,R.raw.song_8,};



        title = Arrays.asList(getResources().getStringArray(R.array.array_titles));

        description = Arrays.asList(getResources().getStringArray(R.array.array_lyrics));
        description2 = Arrays.asList(getResources().getStringArray(R.array.array_lyrics2));

        icon = new int[]{ R.drawable.song, R.drawable.song, R.drawable.song, R.drawable.song,R.drawable.song,R.drawable.song, R.drawable.song,};

        listView = findViewById(R.id.list);

        for (int i = 0; i< title.size(); i++){
            Model model =new Model(title.get(i), description.get(i),description2.get(i), icon[i]);
            //bind all strings in an array
            arrayList.add(model);
        }

        //pass result to listview class
        adapter = new ListViewAdapter(this, arrayList);

        //bind the adapter to the listview class
        listView.setAdapter(adapter);



    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);

        MenuItem myActionMenuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView)myActionMenuItem.getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                if (TextUtils.isEmpty(s)){
                    adapter.filter("");
                    listView.clearTextFilter();
                }
                else {
                    adapter.filter(s);
                }
                return true;
            }
        });

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id==R.id.action_settings){
            Toast.makeText(this, "Settings Selected", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, Main2Activity.class));
            return true;
            //do your funtionality here

        }
        else if (id==R.id.action_howtouse){
                Toast.makeText(this, "How-To", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, Main3Activity.class));
            return true;
                //do your funtionality here


        }else if (id==R.id.action_favorites){
                Toast.makeText(this, "Favorites Selected", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, Main3Activity.class));
            return true;
                //do your funtionality here


        }
        else if (id==R.id.action_developers){
            Toast.makeText(this, "About", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, Main4Activity.class));
            return true;
            //do your funtionality here

        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
}

This is the ListViewAdapter.java

public class ListViewAdapter extends BaseAdapter{

    //Variables
    Context mContext;
    LayoutInflater inflater;
    List<Model> modellist;
    ArrayList<Model> arrayList;
    int [] soundfile;

    //Constructor
    public ListViewAdapter(Context context, List<Model> modellist) {
        mContext = context;
        this.modellist = modellist;
        inflater = LayoutInflater.from(mContext);
        this.arrayList = new ArrayList<Model>();
        this.arrayList.addAll(modellist);
    }

    public class ViewHolder{
        TextView mTitleTv, mDescTv;
        ImageView mIconTv;
        MediaPlayer mediaPlayer;
    }

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

    @Override
    public Object getItem(int i) {
        return modellist.get(i);
    }

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

    @Override
    public View getView(final int i, View view, ViewGroup parent) {
        final ViewHolder holder;
        if (view==null){
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.row, null);

            //locate the views in row.xml
            holder.mTitleTv = (TextView) view.findViewById(R.id.mainTitle);
            holder.mDescTv = (TextView) view.findViewById(R.id.mainDesc);
            holder.mIconTv = view.findViewById(R.id.mainIcon);

            view.setTag(holder);
        }
        else {
            holder = (ViewHolder)view.getTag();
        }
        //set the result into textview
        holder.mTitleTv.setText(modellist.get(i).getTitle());
        holder.mDescTv.setText(modellist.get(i).getDesc());
        //Set the result in imagview
        holder.mIconTv.setImageResource(modellist.get(i).getIcon());

        //listview soundfile file for songs in position
        soundfile= new int[] {R.raw.song_1,R.raw.song_2,R.raw.song_3, R.raw.song_4,R.raw.song_5,R.raw.song_6,R.raw.song_7,R.raw.song_8,};

        //listview item clicks
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //code later
                Intent intent = new Intent(mContext, NewActivity.class);
                intent.putExtra("position", getViewTypeCount());
                intent.putExtra("actionBarTitle", modellist.get(i).getTitle());
                intent.putExtra("contentTv", modellist.get(i).getDesc());
                mContext.startActivity(intent);


            }
        });


        return view;
    }

    //filter
    public void filter(String charText){
        charText = charText.toLowerCase(Locale.getDefault());
        modellist.clear();
        if (charText.length()==0){
            modellist.addAll(arrayList);
        }
        else {
            for (Model model : arrayList){
                if (model.getTitle().toLowerCase(Locale.getDefault()).contains(charText)){
                    modellist.add(model);
                }
            }
        }
        notifyDataSetChanged();
    }


}

This is the String.xml

<resources>
    <string name="app_name">Redeemed Songs</string>





    <string-array name="array_lyrics">
        <item>This song lyrics 1</item>
        <item>This song lyrics 2</item>
    </string-array>

    <string-array name="array_lyrics2">
        <item>This brand new description for song lyrics 1</item>
        <item>This brand new description for song lyrics 1</item>
    </string-array>

    <string-array name="array_titles">
        <item>Song 001</item>
        <item>Song 002</item>
    </string-array>




</resources>

rahul khurana :

Add it to your Model as attribute

class Model{

your variable
.
.
.
String brandNewDesc;


public String getBrandNewDesc(){

return brandNewDesc;
}
public void setBrandNewDesc(String description){

brandNewDesc =description;
}

}

and use

   //listview item clicks
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //code later
            Intent intent = new Intent(mContext, NewActivity.class);
            intent.putExtra("position", getViewTypeCount());
            intent.putExtra("actionBarTitle", modellist.get(i).getTitle());
            intent.putExtra("contentTv", modellist.get(i).getDesc());
            **intent.putExtra("brandNewDesc", modellist.get(i).getBrandNewDesc());**

            mContext.startActivity(intent);


        }
    });

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=8671&siteId=1
Recommended