添加新对象时覆盖Listview - Android

时间:2014-03-04 21:46:46

标签: java android listview android-listview

当我使用putExtras()从另一个活动添加对象时,Listview会一直覆盖每个新对象。该列表永远不会列出超过1个项目。

我应该怎么做才能在创建新事件时将新对象添加到列表视图中的新位置?

主要活动

public class MainActivity extends FragmentActivity implements OnClickListener {

ListView listView;
int lastIndex = -1;
ArrayList<Event> lstEvents = new ArrayList<Event>();

// detail view
TextView tvTitle, tvTime, tvDate;
ImageView img;
View vw_master;

boolean _isBack = true;

ImageButton add;

String title;
String date;
String time;

static final int PICK_CONTACT_REQUEST = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // Create the adapter to convert the array to views
    EventAdapter adapter = new EventAdapter(this, lstEvents);

    // attach adapter to a list view
    listView = (ListView) findViewById(R.id.listViewFragment);
    listView.setAdapter(adapter);

    // // get detail controls
    tvTitle = (TextView) findViewById(R.id.textViewTitle);
    tvDate = (TextView) findViewById(R.id.textViewDate);
    tvTime = (TextView) findViewById(R.id.textViewTime);

    add = (ImageButton) findViewById(R.id.add);
    add.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.add:
        Intent intent = new Intent(this, CreateActivity.class);
        startActivityForResult(intent, RESULT_OK);
        break;
    }
}

protected void onActivityResult(int requestCode, Intent data) {
    if (requestCode == 1001) {
        // do something with data recieved
        Intent intent = getIntent();

        if (intent.getExtras() == null) {
            // Do first time stuff here
        } else {
            // Do stuff with intent data here
            Bundle b = getIntent().getExtras();
            title = b.getString("TITLE");
            time = b.getString("TIME");
            date = b.getString("DATE");

            Event newEvent = new Event();
            newEvent.set_date(date);
            newEvent.set_title(title);
            newEvent.set_time(time);

            lstEvents.add(newEvent);

            // Create the adapter to convert the array to views
            EventAdapter adapter = new EventAdapter(this, lstEvents);

            adapter.addAll(lstEvents);
            adapter.notifyDataSetChanged();

        }
    }

}

}

适配器

public class EventAdapter extends ArrayAdapter<Event> {

// View lookup cache
private static class ViewHolder {
    TextView title;
    TextView time;
    TextView date;
}

public EventAdapter(Context context, List<Event> objects) {
    super(context, R.layout.date_detail);
    // TODO Auto-generated constructor stub
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Get the data item for this position
    Event event = getItem(position);
    // Check if an existing view is being reused, otherwise inflate the view
    ViewHolder viewHolder;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        convertView = inflater.inflate(R.layout.date_detail, null);
        viewHolder.title = (TextView) convertView
                .findViewById(R.id.textViewTitle);
        viewHolder.time = (TextView) convertView
                .findViewById(R.id.textViewTime);
        viewHolder.date = (TextView) convertView
                .findViewById(R.id.textViewDate);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    // Populate the data into the template view using the data object
    viewHolder.title.setText(event._title);
    viewHolder.time.setText(event._time);
    viewHolder.date.setText(event._date);
    // Return the completed view to render on screen
    return convertView;
}

}

CreateActivity

public class CreateActivity extends Activity implements OnClickListener {

EditText etTitle;
Button btDate;
Button btTime;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    // /onclicklistener
    findViewById(R.id.btn_confirm).setOnClickListener(this);

    // edittexts and buttons
    btDate = (Button) findViewById(R.id.btn_date);
    etTitle = (EditText) findViewById(R.id.editTextTitle);
    btTime = (Button) findViewById(R.id.btn_time);
}

// Will be called via the onClick attribute
// of the buttons in main.xml
public void onClick(View view) {
    switch (view.getId()) {
    case R.id.btn_confirm:
         String title = etTitle.getText().toString();
         String time = btTime.getText().toString();
         String date = btDate.getText().toString();

        Log.e("LOG", title);
        Log.e("LOG", time);
        Log.e("LOG", date);



        Bundle newBundle = new Bundle();
        newBundle.putString("TITLE", title);
        newBundle.putString("TIME", time);
        newBundle.putString("DATE", date);



        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtras(newBundle);
        setResult(1001, intent);
        finish();
        break;
    }

}

public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getFragmentManager(), "timePicker");
}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "datePicker");
}

}

2 个答案:

答案 0 :(得分:0)

您正在使用startActivity中的CreateActivity,这是错误的...因此以下代码无效。

    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.putExtras(newBundle);
    startActivity(intent);
    break;

您的MainActivity需要使用startActivityForResult()启动CreateActivity。并且您的CreateActivity必须使用MainActivity函数将结果返回给setResult(...),并将Intent传递给您的Extras。查看documentation here

答案 1 :(得分:0)

您不想向List添加数据您可以将Date直接添加到适配器变量您将添加这样的数据..

   adapter.add(Eventovalues);