Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-在全班使用和更改数组_Java_Android_Arrays - Fatal编程技术网

Java Android-在全班使用和更改数组

Java Android-在全班使用和更改数组,java,android,arrays,Java,Android,Arrays,我花了大约两个小时试图解决这个问题。因此,我很高兴得到每一个可能的答案。 我有一个自定义的ListViewAdapter,它从数组中获取他的内容。但是我有19个数组,我想根据用户单击的日期(顶部有一个日历,上面有19个日期)更改内容(因此是数组)。 这是一张照片: 因此,默认情况下,每次打开应用程序时,都会显示“date_1”数组中的内容(日历中的25)。当您单击其他日期时,将显示属于该日期的数组内容(例如26.->date_2、27.date_3等)。 每个数组都必须从资源中获取负载,并保存

我花了大约两个小时试图解决这个问题。因此,我很高兴得到每一个可能的答案。 我有一个自定义的ListViewAdapter,它从数组中获取他的内容。但是我有19个数组,我想根据用户单击的日期(顶部有一个日历,上面有19个日期)更改内容(因此是数组)。 这是一张照片:

因此,默认情况下,每次打开应用程序时,都会显示“date_1”数组中的内容(日历中的25)。当您单击其他日期时,将显示属于该日期的数组内容(例如26.->date_2、27.date_3等)。 每个数组都必须从资源中获取负载,并保存在单个数组变量中

所以我的问题是: 我无法更改数组。这是我的密码:

// this is the Array Variable:

   String[] day_chosen;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.calender);
          [...]
            prepareArrayLits();
            lview3 = (ListView) findViewById(R.id.listView1);

            adapter = new ListViewCustomAdapter(this, itemList);
            lview3.setAdapter(adapter);

            lview3.setOnItemClickListener(this);

        }

        public void onItemClick1(AdapterView<?> arg0, View arg1, int position, long id) {
            // TODO Auto-generated method stub

        }

        /* Method used to prepare the ArrayList,
         */

        public void prepareArrayLits()
        {
            // this is where the Arrays get loaded from the resource:
             day_chosen = getResources().getStringArray(R.array.date_1);

//this Array is not relvant:
            String[] sports = getResources().getStringArray(R.array.sports_array);


            itemList = new ArrayList<Object>();
             for (int counter = 0; counter < day_chosen.length; counter+=4){


                 int Intparser = Integer.parseInt(day_chosen[counter+1]);


                 AddObjectToList(imgid(Intparser),  sports[Intparser],day_chosen[counter],day_chosen[counter+3], backgroundbox(Intparser)) ;
             }


        }

        // Add one item into the Array List
        public void AddObjectToList(int image, String title, String desc, String time, int backbox)
        {

            bean = new ItemBean();
            bean.setBackBox(backbox);
            bean.setTime(time);
            bean.setDescription(desc);
            bean.setImage(image);
            bean.setTitle(title);
            itemList.add(bean);
        }

       [...]
        }

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            // TODO Auto-generated method stub

        }

// the onClick Events for every date:
        public void onClick(View v) {
            // TODO Auto-generated method stub




              switch(v.getId()){

              case R.id.d_1 :
// I tried to change the Array from here(But it didn't work):
                  day_chosen = getResources().getStringArray(R.array.date_1);


                  break;

              case R.id.d_2 :
                  day_chosen = getResources().getStringArray(R.array.date_2);

                  break;
              [...]

        }



    }
//这是数组变量:
字符串[]所选日期;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局日历);
[...]
制备耳石();
lview3=(ListView)findViewById(R.id.listView1);
adapter=新ListViewCustomAdapter(此,itemList);
lview3.setAdapter(适配器);
lview3.setOnItemClickListener(此);
}
公共视图arg1(AdapterView arg0,视图arg1,整型位置,长id){
//TODO自动生成的方法存根
}
/*用于准备ArrayList的方法,
*/
公共图书馆
{
//这是从资源加载阵列的位置:
所选日期=getResources().getStringArray(R.array.date_1);
//此阵列不相关:
String[]sports=getResources().getStringArray(R.array.sports\u数组);
itemList=新的ArrayList();
用于(整数计数器=0;计数器<天长度;计数器+=4){
int Intparser=Integer.parseInt(所选日期[计数器+1]);
AddObjectToList(imgid(Intparser)、sports[Intparser]、day_Selected[counter]、day_Selected[counter+3]、backgroundbox(Intparser));
}
}
//将一项添加到数组列表中
public void AddObjectToList(整型图像、字符串标题、字符串描述、字符串时间、整型backbox)
{
bean=newitembean();
受挫盒(backbox);
设置时间(time);
setDescription(desc);
setImage(image);
setTitle(title);
添加(bean);
}
[...]
}
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
//TODO自动生成的方法存根
}
//每个日期的onClick事件:
公共void onClick(视图v){
//TODO自动生成的方法存根
开关(v.getId()){
案例R.id.d_1:
//我试图从此处更改阵列(但无效):
所选日期=getResources().getStringArray(R.array.date_1);
打破
案例R.id.d_2:
所选日期=getResources().getStringArray(R.array.date_2);
打破
[...]
}
}
我试过什么:

  • 我建立了一个全球课程,但没有成功

  • 我试着设置接球手和二传手。但也不起作用


我不知道怎么解决这个问题。非常感谢所有可能的解决方案。

我不知道您是如何实现适配器的,但您是否调用了
您的适配器。当列表内容发生更改时,notifyDataSetChanged()
?我不知道您是如何实现适配器的,但您是否调用了
您的适配器。notifyDataSetChanged()
当您的列表内容更改时?

所以我猜当您单击顶部的日期时,下面的内容会更改。有多种方法可以做到这一点。要解决当前代码中的问题,请执行以下操作: 根据您显示的代码,我假设您在某个地方有一行代码,看起来像:
newmyhappyadapter(…itemList,…)
在这里传递itemList引用,可能是某种
ArrayAdapter
。 如果是这样,您需要在switch语句之后添加以下内容:

 public void onClick(View v) {
     switch(v.getId()){

          case R.id.d_1 :
              // I tried to change the Array from here(But it didn't work):
              day_chosen = getResources().getStringArray(R.array.date_1);
              break;

          case R.id.d_2 :
              day_chosen = getResources().getStringArray(R.array.date_2);
              break;
     }
 //Remove the old items from the list
 itemList.clear();
 //Force it setup the list again
 prepareArrayLits();
 //Tell the adapter to update the view
 adapter.notifyDatasetChanged();
 }
adapter.notifyDatasetChanged()
调用告诉适配器刷新
列表视图


顺便说一句,我不建议你继续沿着这条路走下去。我建议您将其转换为一个
ViewPager
,其中每个日期都是一个
选项卡
,下面的内容是一个
列表片段
。您可以阅读有关
ViewPager
和支持jar的信息。

所以我猜当您单击顶部的日期时,下面的内容会发生变化。有多种方法可以做到这一点。要解决当前代码中的问题,请执行以下操作: 根据您显示的代码,我假设您在某个地方有一行代码,看起来像:
newmyhappyadapter(…itemList,…)
在这里传递itemList引用,可能是某种
ArrayAdapter
。 如果是这样,您需要在switch语句之后添加以下内容:

 public void onClick(View v) {
     switch(v.getId()){

          case R.id.d_1 :
              // I tried to change the Array from here(But it didn't work):
              day_chosen = getResources().getStringArray(R.array.date_1);
              break;

          case R.id.d_2 :
              day_chosen = getResources().getStringArray(R.array.date_2);
              break;
     }
 //Remove the old items from the list
 itemList.clear();
 //Force it setup the list again
 prepareArrayLits();
 //Tell the adapter to update the view
 adapter.notifyDatasetChanged();
 }
adapter.notifyDatasetChanged()
调用告诉适配器刷新
列表视图


顺便说一句,我不建议你继续沿着这条路走下去。我建议您将其转换为一个
ViewPager
,其中每个日期都是一个
选项卡
,下面的内容是一个
列表片段
。您可以阅读有关
ViewPager
和支持jar的内容。

这应该不是问题所在。问题是我无法更改数组(字符串[]day_selected;来自另一种方法e)@user1253575你一直这么说,你是不是收到了一个错误,因为我