让我们说我可以在我的适配器中添加播放器名称作为输入,然后在列表视图中显示它们。现在我想显示用户刚刚在我的第二个活动中添加的这些名称,但不是列表视图。但是单品。我该怎么做?
在这里,我创建适配器并添加播放器名称:
public class ZaidejaiActivity extends ActionBarActivity implements View.OnClickListener{
public Button mBtnIstrinti;
public Button mBtnPrideti;
public Button mBtnPradeti;
public EditText mYrasytiVarda;
public ListView mZaidejai;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidejai);
mBtnPradeti = (Button)findViewById(R.id.žaistiBtn);
mBtnPradeti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pradetiZaidima = new Intent(v.getContext(), ZaidimasActivity.class);
startActivity(pradetiZaidima);
}
});
mBtnPrideti = (Button)findViewById(R.id.pridėtiBtn);
mBtnPrideti.setOnClickListener(this);
mYrasytiVarda = (EditText)findViewById(R.id.VardoYrasymoBtn);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
// set the mZaidejai variable to your list in the xml
mZaidejai=(ListView)findViewById(R.id.sarašas);
mZaidejai.setAdapter(adapter);
@Override
public void onClick(View v) {
String input = mYrasytiVarda.getText().toString();
if(input.length() > 0)
{
// add string to the adapter, not the listview
adapter.add(input);
// no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
}else{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Klaida:");
alertDialog.setMessage("Blogai yrašytas vardas");
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.show();
}
}
这里我想显示添加的输入,作为单个值。
public class ZaidimasActivity extends ZaidejaiActivity {
ArrayList<String> listas = getIntent().getStringArrayListExtra("list");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidimas);
}
如何在上一个活动中从适配器获取上下文以及如何显示它们?
答案 0 :(得分:0)
您要做的是将列表作为捆绑包传递给新活动。为适配器类创建一个getter方法来获取列表。
mBtnPradeti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent pradetiZaidima = new Intent(v.getContext(),
ZaidimasActivity.class);
pradetiZaidima.putExtra("playerList", adapter.getList());
startActivity(pradetiZaidima);
}
});
然后在您发布的活动中
public class ZaidimasActivity extends ZaidejaiActivity {
ArrayList<String> listas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zaidimas);
listas.getIntent().getExtras().getStringArrayList("playerList");
}