-4

我有 1 TextView& Buttonin 1 屏幕和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 文件,它将在列表视图中显示内容,这些内容保存在点击事件中,长按它将显示 ListView 文件。

4

1 回答 1

0

将arrayadpater与listview一起使用,并在按钮单击中调用arrayadpater.notifyondatachange() mtd,将数据添加到arraylist。

于 2013-04-16T06:51:54.703 回答