将项目添加到另一个活动中的列表视图(仅添加 1 个条目)
Posted
技术标签:
【中文标题】将项目添加到另一个活动中的列表视图(仅添加 1 个条目)【英文标题】:Add Items to a Listview which is in another activity(Adds Only 1 entry) 【发布时间】:2019-07-31 18:02:18 【问题描述】:我正在尝试制作一个应用程序,我想在其中将值从 editText(MainActivity.java)
传递给放置在另一个活动中的 listView(bestFitActivity.java)
。我的代码的问题是它只将一个值传递给listView
。当我输入一个新值时,它只会替换以前的值。
MainActivity.java
public class MainActivity extends AppCompatActivity
int i = 0;
String getChunkSize;
ArrayList<String> chunkSizeArray = new ArrayList<>();
EditText chunkSize;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button chunkSave = (Button)findViewById(R.id.chunkSave);
Button processSave = (Button)findViewById(R.id.processSave);
Button bestFitDirector = (Button)findViewById(R.id.bestFitdirector);
Button worstFitDirector = (Button)findViewById(R.id.worstFitdirector);
Button firstFitDirector = (Button)findViewById(R.id.firstFitDirector);
EditText processSize = (EditText)findViewById(R.id.processSize);
chunkSize = (EditText)findViewById(R.id.chunkSize);
chunkSave.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
i++;
Log.d("Manager" , "%d" +i);
getChunkSize = chunkSize.getText().toString();
//Log.d("Manager" , "" +getChunkSize);
if(getChunkSize == null )
Toast.makeText(getBaseContext(), "Empty Input", Toast.LENGTH_SHORT).show();
);
bestFitDirector.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent myIntent = new Intent(MainActivity.this , bestFitActivity.class);
myIntent.putExtra("chunkSizeArray" , getChunkSize );
startActivity(myIntent);
);
/*worstFitDirector.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent myIntent2 = new Intent (MainActivity.this , worstFitActivity.class);
startActivity(myIntent2);
);
firstFitDirector.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent myIntent3 = new Intent (MainActivity.this , firstFitActivity.class);
startActivity(myIntent3);
);*/
bestFitActivity.java
public class bestFitActivity extends AppCompatActivity
String chunkSIZE;
ArrayList<String> chunkSizeArray = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_best_fit);
ListView blockSize = (ListView)findViewById(R.id.blockSize);
ListView finalProcessList = (ListView)findViewById(R.id.finalprocesslist);
chunkSIZE = getIntent().getExtras().getString("chunkSizeArray");
Log.d("Manager" , " "+chunkSIZE);
chunkSizeArray.add(chunkSIZE);
Log.d("Manager" , ""+chunkSIZE);
ArrayAdapter<String> adapter = new ArrayAdapter<>(bestFitActivity.this , android.R.layout.simple_expandable_list_item_1 , chunkSizeArray);
blockSize.setAdapter(adapter);
adapter.notifyDataSetChanged();
//((EditText)findViewById(R.id.chunkSize)).setText(" ");
//EditText tv = (EditText) findviewbyID(R.id.)
PS:这段代码不完整
【问题讨论】:
正确格式化代码 您通过 getChunkSize 变量仅发送一个输入。您必须将所有输入的值存储到 chunkSizeArray,并通过意图发送该变量。查看 SO 的此链接以将 ArrayLists 发送到另一个活动:***.com/questions/4030115/… 【参考方案1】:每次单击按钮时,都会创建一个新的活动,其中包含一个新的空 chunkSize ArrayList,仅使用单个值。
您可以在每次单击按钮时添加到 MainActivity 中的 chunkSizeArray,然后在各个 intentExtras 中发送一个计数器和数组的值。
然后从 bestFitActivity 中的所有 Intent Extra 中检索并构建 ArrayList。
你可以这样做
public class MainActivity extends AppCompatActivity
public static final String INTENTKEY_ARRAYELEMENTS = "arrayelemets";
public static final String INTENYKEY_ELEMENTX = "element_";
private static ArrayList<String> chunkSizeArray = new ArrayList<>();
Button bestFitDirector;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bestFitDirector = (Button)findViewById(R.id.bestFitdirector);
bestFitDirector.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
chunkSizeArray.add("Test" + String.valueOf(chunkSizeArray.size() + 1));
Intent myitent = new Intent(MainActivity.this,bestFitActivity.class);
myitent.putExtra(INTENTKEY_ARRAYELEMENTS, chunkSizeArray.size());
for (int i=0; i < chunkSizeArray.size(); i++)
myitent.putExtra(INTENYKEY_ELEMENTX + String.valueOf(i),chunkSizeArray.get(i));
startActivity(myitent);
);
和
public class bestFitActivity extends AppCompatActivity
ArrayList<String> chunkSizeArray = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_best_fit);
Intent mypassedIntent = getIntent();
int elements = mypassedIntent.getIntExtra(MainActivity.INTENTKEY_ARRAYELEMENTS,0);
for (int i = 0; i < elements; i++)
chunkSizeArray.add(mypassedIntent.getStringExtra(MainActivity.INTENYKEY_ELEMENTX + String.valueOf(i)));
Toast.makeText(this,"The number of strings in the chukSizeArray is " + String.valueOf(chunkSizeArray.size()),Toast.LENGTH_SHORT).show();
for (String s: chunkSizeArray)
Log.d("chunkSizeArray Value",s);
点击5次后(点击返回返回MainActivity)日志为
2019-03-10 17:19:51.687 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:19:56.362 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:19:56.362 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:19:59.877 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test3
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test3
2019-03-10 17:20:03.007 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test4
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test1
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test2
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test3
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test4
2019-03-10 17:20:06.266 11271-11271/com.example.bestfitapp D/chunkSizeArray Value: Test5
添加间隙以拆分点击次数
更简单的方法是使用 putStringArrayListExtra 和 getStringArrayListExtra 方法,它们基本上代表您执行循环。
所以主要活动有
public static final String INTENTKEY_ARRAY = "bestfitarray";
在 onClick 监听器中你可以有:-
myitent.putStringArrayListExtra(INTENTKEY_ARRAY,chunkSizeArray);
随着
myitent.putStringArrayListExtra(INTENTKEY_ARRAY,chunkSizeArray);
在 bestFitActivity 中。
【讨论】:
【参考方案2】:你可以做一个简单的 hack
更改ArrayList<String> chunkSizeArray = new ArrayList<>();
静态如private static ArrayList<String> chunkSizeArray = new ArrayList<>();
【讨论】:
很高兴听到它快乐的编码:D以上是关于将项目添加到另一个活动中的列表视图(仅添加 1 个条目)的主要内容,如果未能解决你的问题,请参考以下文章
将多个项目添加到列表视图,每个项目从按钮单击的另一个意图接收