简体   繁体   English

如何为 ListView SecondActivity 创建 arrayList

[英]how to create arrayList for ListView SecondActivity

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.我在strings.xml 中创建了一个字符串数组列表,以便它可以在和适配器中使用,但是当 listItemClick 打开一个新活动时,我不希望将存储在字符串中的数组设置为新的描述性文本. 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 .我的row.xml显示一个 Image,一个 TextView 作为Title ,另一个 TextView 作为description Now i have used this arrangement to generate constructor and getters for my Model.java现在我已经使用这种安排为我的 Model.java 生成构造函数和 getter

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.现在,如果我单击 ListView 项目列表,它会打开一个NewActivity.java ,将存储在string.xml 中的数据作为 arrayList 提供,它只是标题描述文本。 But I want another text different from that discription to show in that NewActivity.java as brand-new-descrption text.但我希望与该描述不同的另一个文本在 NewActivity.java 中显示为全新的描述文本。 That is what I'm struggling to do now.这就是我现在正在努力做的事情。

Thanks谢谢

this is the MainActivity.java code这是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; } }

This is the ListViewAdapter.java这是 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这是 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>

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


        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Android 中 FirstActivity 的数据填充 SecondActivity 上的 ListView? - How can I populate a ListView on a SecondActivity with data from FirstActivity in Android? 如何将ToggleButton传输到SecondActivity - How to transport ToggleButton to SecondActivity 如何在SecondActivity onItemClick中设置文本 - How to setText in SecondActivity onItemClick 将ArrayList从SecondActivity传递到Android MainActivity - Passing ArrayList from SecondActivity to MainActivity Android 如何在ListView中实现arraylist? - How to implement arraylist into the ListView? 如何从secondActivity获取开关按钮的值? - how to get value of switch button from secondActivity? 如何在不同的活动中为ListView修改ArrayList但ListView和ArrayList - How to modified an ArrayList for ListView but ListView and ArrayList in different Activity 如何将MainActivity中的值添加到SecondActivity中的列表中 - How to add values from MainActivity to list in SecondActivity 如何创建ArrayList的副本 <ArrayList<Arc> &gt;到另一个ArrayList <ArrayList<Arc> &gt; - How to create a copy of ArrayList<ArrayList<Arc>> to another ArrayList<ArrayList<Arc>> 如何从drawable创建imges的ArrayList并在片段ListView中显示它? - How do I create an ArrayList of imges from drawable and display it in fragment ListView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM