gpt4 book ai didi

android - 更新传递给适配器的列表对象以创建 GridView

转载 作者:搜寻专家 更新时间:2023-11-01 07:51:34 25 4
gpt4 key购买 nike

我正在尝试更新已传递给适配器的列表对象的内容,以便创建 GridView。这是代码:

 public class GameActivity extends Activity {
List<String> nums;
GridView sudoku_grid;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_temp);
sudoku_grid=(GridView)findViewById(R.id.sudoku_grid);
nums= Arrays.asList("1","2","3","5","7","8","2","3","1","9","3","3","1");
adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,nums);
sudoku_grid.setAdapter(adapter);
}

public void trial(View view)
{
Runnable r=new Runnable() {
@Override
public void run() {
nums.add("7");
adapter.notifyDataSetChanged();
}};
new Thread(r).start();
}

每次单击按钮时都会执行 trial()。一旦 trial() 中的线程启动,它不会更新内容,而是会启动上一个 Activity 。谁能告诉我哪里出错了?

最佳答案

您需要通过 nums对象再次到适配器。创建 classextends ArrayAdapter<String>如下:

class MyArrayAdapter extends ArrayAdapter<String>{

List<String> nums;

public MyArrayAdapter(Context context,int itemID,List<String> nums){
this.nums=nums;
}

@ovverride
public int getCount() {
return nums.size();
}

public void changeObjects(List<String> nums){
this.nums=nums;
this.notifyDataSetChanged();
}

public View getView(int position, View convertView, ViewGroup parent){
//.... Implement own code
}

}

如果还有其他问题或疑问,请告诉我。

关于android - 更新传递给适配器的列表对象以创建 GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34352278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com