我有1 TextView
&在1个屏幕中Button
在另一个屏幕中ListView
,我想在点击按钮时将数据发送到列表视图。
那怎么可能,有必要采取数据库或数组有可能吗?
以下是我所做的代码。
MainActivity.java
public class MainActivity extends Activity {
TextView tx ;
int tv;
int counter = 0;
String[] planets;
private ArrayList<String> mArray;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
planets = getResources().getStringArray(R.array.Jokes_array);
//final Button button = (Button) findViewById(R.id.button11);
tx = (TextView)findViewById(R.id.textViewapp1);
tx.setText(planets[counter]);
Button button = (Button)findViewById(R.id.btnsms);
Button btnFavr = (Button)findViewById(R.id.btnfav);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//startActivity(new Intent(IcontrolActivity.this,Settings.class));
/* Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
//String shareBody = (String) getText(R.id.textViewapp1);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));*/
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.putExtra(Intent.EXTRA_TEXT, ((TextView) findViewById(R.id.textViewapp1)).getText());
sharingIntent.setType("text/plain");
startActivity(sharingIntent);
// TODO Auto-generated method stub
}
});
btnFavr.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Toast toast = Toast.makeText(MainActivity.this, R.string.messageWordAddedToFarvourite, Toast.LENGTH_SHORT);
toast.show();
/* SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString(tx,getString(R.id.textViewapp1));
editor.commit();*/
//ArrayList<String> arryList = new ArrayList<String>();
mArray.add(tx.getText().toString());
}
});
btnFavr.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Intent intent = new Intent(getApplicationContext(), FavActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
return false;
}
});
}
还有另一个我正在使用的Java文件,它将在List视图中显示保存在click事件上的内容,长按它将显示ListView文件。
答案 0 :(得分:0)
将arrayadpater与listview一起使用,并在按钮单击中调用arrayadpater.notifyondatachange()mtd,将数据添加到arraylist。