java如何将其他类的列表项放入数组,然后在textview中设置文本
如何将MainActivity java类中mylistofquestion(第2行)的值放入slideadapter java类中的字符串titleqw(第7行)
所以我可以将它从slideadapter java文件中的数组(buttom的第7行)传递到TextView到settext
主要活动:
public class MainActivity extends AppCompatActivity {
public List<String> mylistofquestion = new ArrayList<>();
public List<String> mylistofanswer = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
slideadapter slideadapter = new slideadapter(this);
viewPager.setAdapter(slideadapter);
myjsonrequest();
}
//requesting for data
public void myjsonrequest() {
//url to make request
String Url = "https://learncodeonline.in/api/安卓/datastructure.json";
JsonObjectRequest myjsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.i("response", "is " + response);
try {
JSONArray jsonArrayofmine = response.getJSONArray("questions");
Log.i("jsonarray", "is " + jsonArrayofmine);
for (int i = 0; i < jsonArrayofmine.length(); i++) {
//taking quesstion and answer
JSONObject jsonObjectinarray = jsonArrayofmine.getJSONObject(i);
String question = jsonObjectinarray.getString("question");
String answer = jsonObjectinarray.getString("Answer");
mylistofquestion.add(i, question);
mylistofanswer.add(i, answer);
}
Log.i("mylistofquestion", "is " + mylistofquestion);
Log.i("mylistofanswer", "is " + mylistofanswer);
for (int i1 = 0; i1 < mylistofquestion.size(); i1++) {
Log.i("Value of element " + i1, mylistofquestion.get(i1));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//show snackbar
}
}
);
MySingleton.getInstance(getApplicationContext()).addToRequestque(myjsonObjectRequest);
}
}
幻灯片:
public class slideadapter extends PagerAdapter {
Context context;
LayoutInflater inflater;
//list of image
int images[] = {R.drawable.bulbblue, R.drawable.bulbgreen, R.drawable.circle, R.drawable.cross};
//lisst of title
// put the value of mylistofquestion from MainActivity to String titleqw
public String[] titleqw = {"aaasdasdas", "yuyui", "third", "fourth"};
//list of desscription
public String[] descriptionqw = {"one", "two", "three", "four"};
//list of background color
public int[] backgroundColor = {
Color.rgb(55, 55, 55),
Color.rgb(5, 5, 35),
Color.rgb(55, 55, 25),
Color.rgb(85, 155, 5)
};
public slideadapter(Context context) {
this.context = context;
}
/**
* Return the number of views available.
*/
@Override
public int getCount() {
return backgroundColor.length;
}
/**
* Determines whether a page View is associated with a specific key object
* as returned by {@link #, int)}. This method is
* required for a PagerAdapter to function properly.
*
* @param view Page View to check for association with <code>object</code>
* @param object Object to check for association with <code>view</code>
* @return true if <code>view</code> is associated with the key object <code>object</code>
*/
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return (view == (LinearLayout) object);
}
/**
* Remove a page for the given position. The adapter is responsible
* for removing the view from its container, although it only must ensure
* this is done by the time it returns from {@link #finishUpdate(ViewGroup)}.
*
* @param container The containing View from which the page will be removed.
* @param position The page position to be removed.
* @param object The same object that was returned by
* {@link #instantiateItem(View, int)}.
*/
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView((LinearLayout) object);
}
/**
* Create the page for the given position. The adapter is responsible
* for adding the view to the container given here, although it only
* must ensure this is done by the time it returns from
* {@link #finishUpdate(ViewGroup)}.
*
* @param container The containing View in which the page will be shown.
* @param position The page position to be instantiated.
* @return Returns an Object representing the new page. This does not
* need to be a View, but can be some other container of the page.
*/
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.slide, container, false);
LinearLayout layoutslide = (LinearLayout) view.findViewById(R.id.linearlayout);
ImageView imgslide = (ImageView) view.findViewById(R.id.imageviewofus);
TextView txttitle = (TextView) view.findViewById(R.id.texta);
TextView description = (TextView) view.findViewById(R.id.textb);
layoutslide.setBackgroundColor(backgroundColor[position]);
imgslide.setImageResource(images[position]);
//get List items in txttitle
txttitle.setText(titleqw[position]);
description.setText(descriptionqw[position]);
container.addView(view);
return view;
}
}
在上面的这一行中,我想得到txttitle中的arraylist项
# 1 楼答案
步骤1:从Mainactivity中剪切这两行
步骤2:在方法myjsonrequest(如下所示)中的for循环之后粘贴这两行,并进行一些修改
步骤3:在slideadapter中将构造函数更改为
步骤4:在slideadapter内实例化Item方法 更换这条线
下面是一个新的
# 2 楼答案
您可以尝试将slideadapter放入主活动中,它实际上应该是“slideadapter”。班我想你还得和其他人一起工作