如何为 ListView SecondActivity 创建 arrayList

how to create arrayList for ListView SecondActivity

我在 strings.xml 中创建了一个字符串数组列表,因此它可以在 Adapter 中使用,但我不想将数组存储在当 listItemClick 打开新 activity 时,要设置为新描述文本的字符串。 请帮我。我想创建另一个数组列表作为完整描述,它应该与绑定到行的列表不同。

让我进一步描述一下我的意思。

我的 row.xml 显示图像,TextView 作为 Title,另一个 TextView 作为 description。 现在我已经使用这种安排为我的 Model.java

生成构造函数和 getter

现在,如果我单击一个 ListView 项目列表,它会打开一个 NewActivity.java 并提供存储在 [= 中的数据75=] 作为 arrayList,它只是 Title & description 文本。但是我想要另一个不同于 discription 的文本在 NewActivity.java 中显示为全新的描述文本。这就是我现在正在努力做的事情。

谢谢

这是MainActivity.java代码

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;
    }
}

这是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();
    }


}

这是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>

将其作为属性添加到模型中

class Model{

your variable
.
.
.
String brandNewDesc;


public String getBrandNewDesc(){

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

brandNewDesc =description;
}

}

并使用

   //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);


        }
    });